Dynamics crm + 更新零值的插件逻辑

Dynamics crm + plugin logic to update zero values

我需要对同一实体的多个记录中的每个字段求和,并更新同一实体的值。除此之外,我还需要存储它的公式。

AttributeList = { "price ", "quantity", "contact.revenue", "opportunity.sales"}

逻辑如下

foreach (var attribute in attributeList)
{
    Decimal fieldSum = 0;
    string computedNote = string.Empty;
    foreach (var entity in mainEntityList)
    {
        if (entity.Contains(attribute))
        {
            if (entity.Attributes[attribute] != null)
            {
                string type = entity.Attributes[attribute].GetType().Name;
                Decimal attrValue = 0;

                if (type == "AliasedValue")
                {
                    AliasedValue aliasedFieldValue = (entity.GetAttributeValue<AliasedValue>(attribute));
                    attrValue = aliasedFieldValue.Value.GetType().Name == "Decimal" ? (Decimal)aliasedFieldValue.Value : (Int32)aliasedFieldValue.Value;
                }
                else
                {
                    attrValue = entity.Attributes[attribute].GetType().Name == "Decimal" ? entity.GetAttributeValue<Decimal>(attribute) : entity.GetAttributeValue<Int32>(attribute);
                }
                fieldSum += attrValue;
                computedNote += $"+{Convert.ToInt32(attrValue).ToString()}";
                
            }
        }
        else
        {
            computedNote += $"+0";
        }
    }
    Entity formula = new Entity("formula");
    if (fieldSum != 0)
    {
        if (attribute.Contains("opportunity"))
        {
            opportunity[attributeName] = fieldSum;
            entityName = Opportunity.EntityLogicalName;
            attributeName = attribute;
            recordId = Id;

        }
        else if (attribute.Contains("contact"))
        {
            contact[attributeName] = fieldSum;
            entityName = Contact.EntityLogicalName;
            attributeName = attribute;
            recordId = Id;


        }
        else
        {
            mainentity[attribute] = fieldSum;
            entityName = mainEntity.EntityLogicalName;
            attributeName = attribute;
            recordId = Id;

        }

      formula.Attributes["ice_entity"] = entityName;
      formula.Attributes["ice_attribute"] = attributeName;
      formula.Attributes[entityName + "id"] = new EntityReference(entityName, recordId);
      formula.Attributes["ice_computednote"] = computedNote.Remove(0, 1);
        requestsCollection.Entities.Add(formula);
    }
}

requestsCollection.Entities.Add(opportunity);
requestsCollection.Entities.Add(contact);
requestsCollection.Entities.Add(mainentity);

两条记录中的值可能如下

Record 1
Price = 500   
Quantity = 25 
Revenue = 100
Sales = 10000
Volume = 0

Record 2
Price = 200   
Quantity = 10 
Revenue = 100
Sales = -10000
Volume = 0

Record 3 (Values after calculation that are to be updated in the third entity and Formula to be stored mentioned in brackets)
Price = 700 Formula = (500+200)
Quantity = 35 Formula = (25+10)
Revenue = 200 Formula =(100+100)
Sales = 0 Formula =(10000 + (-10000))
Volume = 0 No Formula to be created

我正在检查字段和是否不等于零(以更新正值和负值),然后更新相应实体中的值。但是对于计算后变为零的值。我还需要更新它们并为其创建公式。避免默认为零的值。

如上例所示,我想更新销售字段值并创建与“10000+-10000”相同的公式记录,但不希望更新销量字段值或为其创建公式.我怎样才能将这个逻辑嵌入到我的代码中?

添加一个标志(updateFormula)以指示是否需要在相关实体中更新校验和和公式。然后,不检查 fieldSum != 0,而是检查 updateFormula is true 以更新相关记录。

attributeList = { "price", "quantity", "contact.revenue", "opportunity.sales"}
    foreach (var attribute in attributeList)
    {
        Decimal fieldSum = 0;
        string computedNote = string.Empty;
        bool updateFormula = false;
        foreach (var entity in mainEntityList)
        {
            if (entity.Contains(attribute))
            {
                if (entity.Attributes[attribute] != null)
                {
                    string type = entity.Attributes[attribute].GetType().Name;
                    Decimal attrValue = 0;
    
                    if (type == "AliasedValue")
                    {
                        AliasedValue aliasedFieldValue = (entity.GetAttributeValue<AliasedValue>(attribute));
                        attrValue = aliasedFieldValue.Value.GetType().Name == "Decimal" ? (Decimal)aliasedFieldValue.Value : (Int32)aliasedFieldValue.Value;
                    }
                    else
                    {
                        attrValue = entity.Attributes[attribute].GetType().Name == "Decimal" ? entity.GetAttributeValue<Decimal>(attribute) : entity.GetAttributeValue<Int32>(attribute);
                    }
                    fieldSum += attrValue;
                    computedNote += Convert.ToInt32(attrValue).ToString();
                    updateFormula = true;
                }
            }
            else
            {
                computedNote += 0;
            }
        }
        Entity formula = new Entity("formula");
        if (updateFormula)
        {
          // Logic to update formula and checksum
        }
    }