web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Suggested Answer

Subgrid row calculation using javascipt

(0) ShareShare
ReportReport
Posted on by 92

Hello,

I hope someone can help me. I created a javascript to calculate a column in a subgrid, but it it not working. I will add the javascript code below:

function onLoadCostTable(executionContext){
    formContext = executionContext.getFormContext();
    gridControl = formContext.getControl("Subgrid_new_6");
    gridControl.addOnLoad(myFunction);
  }
 
  function myFunction(){
    let myGrid = gridControl.getGrid();
    var myRows = myGrid.getRows();
    var total=0;
    LoopedIn = new Boolean(false);
    myRows.forEach(function (row, index){
      LoopedIn = true;
        // get all attributes in a row
        var attributes = row.getData().getEntity().attributes;
        // get each attribute in a row
        attributes.forEach(function (attribute, index){
            //console.log("Name:" + attribute.getName() + " , Value:" + attribute.getValue());
            //change to your field name
            if(attribute.getName() == "hrex2193_cost"){
                total += attribute.getValue();
            }
        })
    })
      if (LoopedIn) {
        formContext.getAttribute("hrex2193_coursetotalcost").setValue(total);
      }
  }
I registered the function  "onLoadCostTable" on the on load of the form, but the rows are returning undefined and the script is not continuing.
The subgrid contains data and the view contains the field that I want to calculate. May someone lead me how to wait for the subgrid to load and then start the calculation?
Thank you!
I have the same question (0)
  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    I would highly recommend using some backend operations for doing calculations like plugin/workflow/power automation flow.

  • XM-22040801-0 Profile Picture
    11 on at

    Like a33ik said, its not a recommended way.

    You also use rollup field.

    For science, it could be done this way:

    const gridCtrlName = "Subgrid_new_6";
    const gridCostAttrName = "hrex2193_cost";
    const costAttrName = "hrex2193_coursetotalcost";
    
    function onLoadCostTable(execCtx) {
        const formCtx = execCtx.getFormContext();
        gridCtrl = formCtx.getControl(gridCtrlName);
        gridCtrl.addOnLoad(setTotalCost);
    }
    
    function setTotalCost(execCtx) {
        const formCtx = execCtx.getFormContext();
        const gridCtrl = formCtx.getControl(gridCtrlName);
        const costAttr = formCtx.getAttribute(costAttrName);
        
        const getValue = (row) => row.data.entity.attributes.get(gridCostAttrName).getValue() ?? 0;
        
        const rows = gridCtrl.getGrid().getRows().get();
        const totalCost = rows.reduce((total, row) => total   getValue(row), 0);
    
        if (totalCost !== costAttr.getValue()) {
            costAttr.setValue(totalCost);
        }
    }

    Of course, the calculation is only for the first page of the sub-grid. The cost can therefore be wrong for more than one page.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Siv Sagar Profile Picture

Siv Sagar 93 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 80

#3
Martin Dráb Profile Picture

Martin Dráb 64 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans