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

Community site session details

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

Javascript to Evaluate Whether Conditions Exist on SubGrid

(0) ShareShare
ReportReport
Posted on by 5

Hi Everyone,

I am a relatively new developer in JS Dynamics/CDS and am trying to piece together my first script.

This forum has been incredibly helpful in providing examples and other solutions and so I'm making my first

post to see if I can get help on an issue that I haven't yet seen discussed. 

I am trying to do the following:

1. Load a Java Script upon loading a record in a Model Driven App

2. I want the Java Script to evaluate the records in a sub-grid on the Form

3. Based on certain conditions, set a field on the main form accordingly.

I have so far attempted to code for 1&2 but I am not seeing any activity once it loads. Can anyone spot what I am missing?

function onLoad (executionContext) {

    var formContext = executionContext.getFormContext();
    
    formContext.data.process.addOnStageChange(onStageChange)
    
    }

    function onStageChange(executionContext) {


           function QC_WeightVerification(executionContext) {
        var formContext = executionContext.getFormContext(); // get the form Context
        var gridContext = formContext.getControl("Subgrid_Tracker"); // get the grid context
     
        // Perform operations on the subgrid

        var myRows = gridContext.getGrid().getRows();
        var myRow = myRows.get();
        var gridRowData = myRow.getData();

        var xxx_qcweightverification = formcontext.getattribute("xxx_qcweightverification")
        var xxx_qcweightallowance = formcontext.getattribute("xxx_qcweightverification")

        var qc_weight_test = gridRowData.filter(xxx_qcweightallowance >= xxx_qcweightverification)

        if(qc_weight_test.isArray(emptyArray) && empty.Array.length)

        alert(true)

        else

        alert(false)

     }
    }

I have the same question (0)
  • Suggested answer
    Torrado Profile Picture
    1,270 on at
    RE: Javascript to Evaluate Whether Conditions Exist on SubGrid

    Hello,

    You are using the right function to get the rows: getRows()

    • Are you sure that the onLoad function is triggering when loading the Form?
    • Are you sure that the onStageChange function is triggering when changing the BPF Stage?

    I suggest that you debug your code by adding debugger tag and use the browser console (F12 + F10) in order to check the result of each line.

    Hope this helps you.

    Please mark the answer as verified if helpful. That will help others in future.

  • SarahMoustafa Profile Picture
    25 on at
    RE: Javascript to Evaluate Whether Conditions Exist on SubGrid

    Hi,

    I'd recommend to add a debugger at line 15 and publish. Reload the record/form and press F12 to debug.

    Assuming that the onLoad function is registered correctly to the form load event, then on changing of the business process stage, the debugger should be triggered automatically.

    Once that happens, check "gridContext" value. If it's null then the code runs before the grid control is loaded. 

    If that's the case, try to arrange the form so that the grid control load faster or replace the get control lines with OData query instead to get the total related records and then based on that run the remaining logic.

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…

Pallavi Phade – Community Spotlight

We are honored to recognize Pallavi Phade as our Community Spotlight honoree for…

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

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 77 Super User 2025 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 66 Most Valuable Professional

#3
CA Neeraj Kumar Profile Picture

CA Neeraj Kumar 48

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans