Skip to main content

Notifications

Announcements

No record found.

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)

     }
    }

  • SarahMoustafa Profile Picture
    SarahMoustafa 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.

  • Suggested answer
    Torrado Profile Picture
    Torrado 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.

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

Congratulations 2024 Spotlight Honorees!

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December!

Congratulations to our December super stars! 🥳

Get Started Blogging in the Community

Hosted or syndicated blogging is available! ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,354 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans