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

setTimeout being ignored?

(0) ShareShare
ReportReport
Posted on by

Hello all,

I'm running some JS which works in its purpose, however it doesn't appear to work consistantly and is executing before the sub-grid is populated, therefore returned 0 records, when there's some present.

Please see my JS below:

function checkRows(executionContext) {
debugger;
var formContext = executionContext.getFormContext();
var grid = formContext.getControl('Subgrid_2'); //declares subjectivities subgrid as "grid"
if (grid == null) { //if "grid" is empty
setTimeout(function() { checkRows(executionContext); }, 2500); //if the grid hasn’t loaded in time it runs this again when it has
return;
}
var filteredRecordCount = grid.getGrid().getTotalRecordCount(); //Count records
alert(filteredRecordCount)
if (filteredRecordCount == 0) { //if none then...
formContext.getControl('statuscode').removeOption(283390001); //Hide "Approved with Subjectivities" value
}
}

Ultimately, just delaying the whole script would work too.

Many thanks in advance,

I have the same question (0)
  • Suggested answer
    Nya Profile Picture
    29,060 on at

    Hi,

    Please try to add a new intended function into the JS web resource, which will be called when the sub-grid is loaded.

    function onGridLoad() {
        var functionName = "onGridLoad";
        var currentRowCount = null;
        try {
            //setting timeout beacuse subgrid take some time to load after the form is loaded
            setTimeout(function () {
                //validating to check if the sub grid is present on the form
                if (Xrm.Page != null && Xrm.Page != undefined && Xrm.Page.getControl("Subgrid_2") != null && Xrm.Page.getControl("Subgrid_2") != undefined) {
                    //stores the row count of subgrid on load event of CRM Form
                    currentRowCount = Xrm.Page.getControl("Subgrid_2").getGrid().getTotalRecordCount();
                    if (currentRowCount > _rowCount) {
                        //call the intended function which we want to call only when records are added to the grid
                        //alert(111);
                        //set current row count to the global row count
                        _rowCount = currentRowCount;
    
                    }
    
                    else if (currentRowCount < _rowCount) {
                        //call the intended function which we want to call only when records are removed from the grid
                        //alert(222);
                        //set current row count to the global row count
                        _rowCount = currentRowCount;
                    }
                }
                alert(_rowCount);
            }, 2000);
        } catch (e) {
            Xrm.Utility.alertDialog(functionName   "Error: "   (e.message || e.description));
        }
    }

    And then, adjust your original function as something like the following snippet:

    function checkRows(executionContext) {
        debugger;
        var formContext = executionContext.getFormContext();
        var grid = formContext.getControl('Subgrid_2'); //declares subjectivities subgrid as "grid"
        if (grid == null) { //if "grid" is empty
            setTimeout(function () { 
                checkRows(executionContext); 
                grid.addOnLoad(onGridLoad);
            }, 5000); //if the grid hasn’t loaded in time it runs this again when it has
            return;
        }
        var filteredRecordCount = grid.getGrid().getTotalRecordCount(); //Count records
        alert(filteredRecordCount)
        if (filteredRecordCount == 0) { //if none then...
            formContext.getControl('statuscode').removeOption(283390001); //Hide "Approved with Subjectivities" value
        }
    }

    Since I can't know exactly which entity you're triggering this JS on, you'll need to adapt your code to the actual use.

    Make sure that:

    1. addOnLoad function has been called in the original function.
    2. There is a delay between the two TimeOuts.

    Here, for example, I alerted the count of the child contacts for an account when its form is refreshed. The count can be alerted after the sub-grid Contacts is loaded.

    pastedimage1629872426154v1.png

  • Nya Profile Picture
    29,060 on at

    Hi,

    I'd like to confirm if your issue has been solved.

    If there is any further doubt, please do not hesitate to let me know.

  • Community Member Profile Picture
    on at

    Hey Nya,

    I ended up going with this:

    // JavaScript source code
    var _rowCount;

    function checkRows(executionContext) {
        var formContext = executionContext.getFormContext();
        var recordId = formContext.data.entity.getId().replace(/\{|\}/g"");

        Xrm.WebApi.retrieveMultipleRecords("red_subjectivity""?$select=red_name&$filter=_red_binder_value eq " + recordId).then(
            function success(result) {
                var subgridLength = result.entities.length;
                if (subgridLength == 0) {
                    formContext.getControl('statuscode').removeOption(283390001); //Hide "Approved with Subjectivities" value
                }
            },
            function (error) {
                alert("Failure");
                console.log(error.message);
            }
        );
    }


  • Nya Profile Picture
    29,060 on at

    Hi,

    It's great to hear that it has been solved and thanks for your sharing.

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 76

#3
Martin Dráb Profile Picture

Martin Dráb 64 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans