繁体   English   中英

动态 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是否为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
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM