Dynamics crm 动态客户关系管理&x2B;更新零值的插件逻辑

Dynamics crm 动态客户关系管理&x2B;更新零值的插件逻辑,dynamics-crm,dynamics-crm-2011,dynamics-crm-2013,dynamics-crm-online,Dynamics Crm,Dynamics Crm 2011,Dynamics Crm 2013,Dynamics Crm Online,我需要对同一实体的多个记录中的每个字段进行求和,并更新同一实体上的值。除此之外,我还需要存储它的公式 AttributeList = { "price ", "quantity", "contact.revenue", "opportunity.sales"} 下面是逻辑 foreach (var attribute in attributeList) { Decimal fieldSum = 0;

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

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);
我正在检查fieldsum是否不等于零(以更新正值和负值),然后更新相应实体中的值。但是,对于计算后变为零的值。我还需要更新它们并为它们创建公式。避免默认为零的值

如上面的示例所示,我希望更新销售字段值并创建与“10000+-10000”相同的公式记录,但不希望更新体积字段值或为其创建公式。如何在代码中嵌入此逻辑?

添加一个标志(
updateFormula
)以指示是否需要在相关实体中更新校验和和公式。然后,不检查
fieldSum!=0
,选中
updateFormula
是否为
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
        }
    }
attributeList={“价格”、“数量”、“联系人.收入”、“机会.销售”}
foreach(attributeList中的var属性)
{
十进制字段和=0;
string computedNote=string.Empty;
bool updateFormula=false;
foreach(mainEntityList中的var实体)
{
if(实体包含(属性))
{
if(entity.Attributes[attribute]!=null)
{
字符串类型=实体.Attributes[属性].GetType().Name;
十进制值=0;
如果(类型==“别名值”)
{
AliasedValue aliasedFieldValue=(entity.GetAttributeValue(属性));
attrValue=aliasedFieldValue.Value.GetType().Name==“Decimal”?(Decimal)aliasedFieldValue.Value:(Int32)aliasedFieldValue.Value;
}
其他的
{
attrValue=entity.Attributes[attribute].GetType().Name==“Decimal”?entity.GetAttributeValue(属性):entity.GetAttributeValue(属性);
}
fieldSum+=属性值;
computedNote+=Convert.ToInt32(attrValue).ToString();
updateFormula=true;
}
}
其他的
{
computedNote+=0;
}
}
实体公式=新实体(“公式”);
if(updateFormula)
{
//更新公式和校验和的逻辑
}
}

嘿,谢谢你的帮助。真的很感激
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
        }
    }