Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

Subgrid row calculation using javascipt

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!
  • XM-22040801-0 Profile Picture
    XM-22040801-0 11 on at
    RE: Subgrid row calculation using javascipt

    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.

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Subgrid row calculation using javascipt

    Hello,

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

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans