【问题标题】:Dynamics crm + plugin logic to update zero valuesDynamics crm + 插件逻辑更新零值
【发布时间】:2021-06-30 06:54:20
【问题描述】:

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

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”相同的公式记录,但不希望更新量字段值或为其创建公式。我怎样才能在我的代码中嵌入这个逻辑?

【问题讨论】:

    标签: dynamics-crm dynamics-crm-2011 dynamics-crm-2013 dynamics-crm-online


    【解决方案1】:

    添加一个标志 (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
            }
        }
    

    【讨论】:

    • 嘿,谢谢您的帮助。真的很感激
    猜你喜欢
    • 2019-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多