Skip to main content

Notifications

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

setTimeout being ignored?

Posted on by Microsoft Employee

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,

  • Nya Profile Picture
    Nya 29,056 on at
    RE: setTimeout being ignored?

    Hi,

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

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: setTimeout being ignored?

    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
    Nya 29,056 on at
    RE: setTimeout being ignored?

    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.

  • Suggested answer
    Nya Profile Picture
    Nya 29,056 on at
    RE: setTimeout being ignored?

    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

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!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans