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

Announcements

No record found.

News and Announcements icon
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)

     }
    }

  • Suggested answer
    Torrado Profile Picture
    1,270 on at

    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

    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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the August Top 10 Community Leaders

These are the community rock stars!

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

#1
11manish Profile Picture

11manish 61 Super User 2026 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 38 Most Valuable Professional

#3
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 22 Super User 2026 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans