The previous dynamic CRM 2013 study note (11) used JavaScript to implement the subtable aggregate (summary, summation) function, and introduced how to use js to implement the subtable aggregate function, this method requires adding the js method to each form. If there are many entities that want to implement this function, some people may find it a little troublesome and will not like this method, so I wrote a General Plug-in for the General Sub-Table aggregate function, which can be implemented only by entering different parameters when registering the plug-in.
1. First, let's look at the effect:
2. Registration Method:
We need to register the create and update Methods in the sub-table:
You must enter four parameters during registration:
The first parameter is the sub-Table field to be aggregated, the second parameter is the master key of the master table, and the third parameter is the entity name of the master table, the fourth parameter is the field that displays the total value on the master table.
When registering an update, you must add the fields associated with the primary table in the sub-table to the image to facilitate the use of the plug-in and add the filter attribute, this plug-in is triggered only when this field is used:
3. Implementation Method
1: Entity entity = (Entity)context.InputParameters["Target"];
2: Entity preEntity = (Entity)context.PreEntityImages["preEntity"];
1: public SumSubgrid(string unsecure)
2: {
3: m_config = unsecure;
4: }
- Obtain sub-tables in total
Use organizationservicecontext svccontext = new organizationservicecontext (adminservice) to read and write objects
1: var ents = svcContext.CreateQuery(entity.LogicalName).Where(e => e[parameters[1]] == preEntity[parameters[1]] && e[parameters[0]] != null).Select(e => e[parameters[0]]);
2: decimal amount = 0;
3: foreach (var ent in ents)
4: {
5: amount += Convert.ToDecimal(ent);
6: }
- Save the total value to the total field of the master table.
1: var primaryEnt = svcContext.CreateQuery(parameters[2]).Where(e => e[parameters[1]] == preEntity[parameters[1]]).FirstOrDefault();
2: primaryEnt[parameters[3]] = amount;
3: svcContext.UpdateObject(primaryEnt);
4: svcContext.SaveChanges();
Is it concise!
Dynamic CRM 2013 learning notes Series
Dynamic CRM 2013 learning notes (12) General Plug-ins that implement the sub-Table aggregate (summary, sum) Function