Dynamic CRM 2013 learning notes (12) General Plug-ins that implement the sub-Table aggregate (summary, sum) Function

Source: Internet
Author: User

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

  • Get current entity
  1: Entity entity = (Entity)context.InputParameters["Target"];
  2: Entity preEntity = (Entity)context.PreEntityImages["preEntity"];

 

  • Get Parameters
  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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.