0

I have the below requirement to be implemented in a plugin code on an Entity say 'Entity A'-

Below is the data in 'Entity A'

Record 1 with field values

  • Price = 100
  • Quantity = 4

Record 2 with field values

  • Price = 200
  • Quantity = 2

I need to add the values of the fields and update it in a new record. Example shown below -

Record 3

  • Price = 100 + 200 = 300
  • Quantity = 4 + 2 = 6

Entity A has a button named "Perform Addition" and once clicked this will trigger the plugin code.

I need some ideas/pseudocode on how can i implement this. The example explained above is just a simpler version of my entity. In Real the entity has more than 60 fields on it , and for each of the fields i need to perform the sum and update in the third one. Also the number of records on which the addition would be performed can be beyond 2.

Hence more the no of records, more i will have to loop through each records and perform sum, I would like to know if there are simpler and better ways to writing this logic.

Just need guidance on how the logic should be written. Any help would be appreciated.

Solution :

Tried the below code and it worked as suggested by the answer -

AttributeList is the list of fields i need to perform sum on. All fields are decimal

Entity EntityA = new EntityA();
EntityA.Id = new Guid({"Guid String"});

var sourceEntityDataList = service.RetrieveMultiple(new FetchExpression(fetchXml)).Entities;

          foreach (var value in AttributeList)
            {
                EntityA[value]= sourceEntityDataList.Sum(e => e.Contains(value) ? e.GetAttributeValue<Decimal>(value) : 0);
            }

service.Update(EntityA);
MVC_Nhibernate
  • 447
  • 11
  • 30
  • What have you tried so far? Should be a place for answers to specific issues, not a place where you can ask someone else to do your job for you. – Just Do It Mar 21 '21 at 18:12

1 Answers1

0

I recently did something similar for a client. We looked into using rollup fields, but they want the results more quickly than 12 hours.

The records containing the values that we're summing are the child records and the record containing the results is the parent.

The way it works is:

  1. From the plugin's target entity, get the parent id.
  2. Retrieve the parent.
  3. Retrieve its children (via LINQ, FetchXML, or a QueryExpression).
  4. Sum the values from the children. Since you have a lot of fields to sum, you could create a list of the field names, then use a method like this (assuming they're all Money fields)
    private decimal sum(string field) => Records.Sum(b => b.GetAttributeValue<Money>(field)?.Value ?? 0);
    If the fields are different types you could also create different sum methods like sumMoney, sumInt, etc. You'd then need to separate the fields by type and pass them to their appropriate method.
  5. Populate the results on the parent Entity.
  6. Update the parent record in Dynamics.

If the information is not that time sensitive maybe look into the out-of-box rollup fields.

Aron
  • 3,877
  • 3
  • 14
  • 21
  • thank you for the help and apologies for the previous comment. This worked. I have another query on the same which i have posted here - https://stackoverflow.com/questions/66809258/dynamics-crm-plugin-code-to-store-sum-formula-across-a-entity-collection Could you please guide – MVC_Nhibernate Mar 26 '21 at 00:08