Search code examples
dynamics-crmdynamics-crm-2011dynamics-crm-2013dynamics-crm-online

Dynamics crm + plugin logic to update zero values


I need to perform the sum of each field across multiple records of the same entity and update the values on the same entity. Along with this I also need to store its formula.

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

Below is the logic

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);

Values in both records could be as follows

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

I am checking if the fieldsum is not equal to zero (to update both positive and negative values) and then updating the values in the respective entity. However for values that became zero after the calculation. I also need to update them and create formula for the same. Avoiding the values that were zero by default.

As shown in above example, I want to update sales field value and create formula record for the same as '10000+-10000' but do not want volume field value to be updated or the formula to be created for it. How can i embed this logic in my code?


Solution

  • Add a flag (updateFormula) to indicate whether checksum and formula required to update in related entities. Then, instead of checking fieldSum != 0, check updateFormula is true to update the related records.

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