动态 crm + 插件逻辑来更新零值

问题描述

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

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

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

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

解决方法

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

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
        }
    }

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...