We often have a requirement that, after adding or modifying a value in a sub-table, it is necessary to display its and displayed on the main sheet immediately in the main list. If implemented with a plug-in, you can achieve the sum, but the page will have to refresh to display correctly. At this point, consider the use of JS to achieve this function, and automatically refresh the page, so that the sum of the value immediately displayed on the main table.
New or modified on the child table, save and close, the system will automatically refresh the child table list to display the new child table data row, it is possible to use this child table refresh function to calculate and display the value.
The detailed implementation is described below:
1. Rewrite Subgrid refresh event, Subgrid register an event when refreshing, complete Subgrid item's cumulative calculation
1: (function (window, undefined) {
2: var prerefresh = Mscrm.GridControl.prototype.Refresh;
3: function () {
4: prerefresh.apply (this);
5: firecallback.apply (this);
6: }
7: var oneevent = null;
8: function (JSON) {
9: oneevent = json;
Ten: }
One: function firecallback () {
: if (oneevent && oneevent.controlid && oneevent.controlid = = this.get_id ()) {
: oneEvent.callback.apply (this, null);
: }
: }
:}) (window);
18:mscrm.gridcontrol.add_onrefresh ({
: controlId: "chart_btl_postmfunction () {
: sumcost ("new_sum_cost_bp", "new_cost", "new_btl_posm");
: }
22:});
The Sumcost method here is to calculate and display the value on the main table
2. Calculating and Displaying methods
function sumcost (Sumfieldnmae, Subfiledname, Subentityname) {
2: var amount = 0;
3: var key = "new_marketing_planid/id";
4: var value = Xrm.Page.data.entity.getId ();
5: //var query = "new_btl_posmset? $select =new_cost& $filter =new_marketing_planid/id eq GUID ' 123 '";
6: var query = subentityname + "Set? $select =" + subfiledname + "& $filter =" + key + " eq GUID '+ value + ' ';
7: var result = Odataretrieve (query);
8: if (result! = null && result.results.length > 0) {
9: For (var i = 0; i < result.results.length; i++) {
Ten: Amount + = number (result.results[i].new_cost);
One: }
: }
: Xrm.Page.getAttribute (Sumfieldnmae). SetValue (amount);
15:}
3. Add the JS file to the form, refresh the page, you can see the effect immediately
Dynamic CRM 2013 Learning Notes Series Rollup
Dynamic CRM 2013 Learning Notes (11) using JavaScript to find the value of a child table and to the main table