Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Suggested answer

JavaScript Web Resource issue: getGrid() suddenly started failing

Posted on by Microsoft Employee

Hello, I have a few different JavaScript web resources that use the getGrid(), all of which started failing this week after I enabled the 2020 Wave 1 Updates in D365.  The error message shows: "Error occurred :TypeError: Unable to get property 'getGrid' of undefined or null reference"

An example of a line of code that is suddenly failing in each of my web resources is:

        var grid = gridCtx.getGrid();

gridCtx is a variable that I previously defined in my code.

Any ideas how I can fix this? Is this a known issue with the new D365 wave 1 update?

Thanks!

  • RE: JavaScript Web Resource issue: getGrid() suddenly started failing

    Hi tgiard,

    As Marco.P advised Xrm.Page is deprecated and you should be using formContext instead, please check the article below and make sure to update your code:

    docs.microsoft.com/.../important-changes-coming

    Here's an example of getAttribute function:

    docs.microsoft.com/.../getattribute

    Can you please clarify further on the current state of the code? When you say the code doesn't execute, which part of the code isn't working?

    You can add more logs to your code to know which part of the code is failing. Once you do that kindly share with us where the issue is happening to be able to assist you further.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: JavaScript Web Resource issue: getGrid() suddenly started failing

    @Marco.P,

    Thank you.  Are you referring to these 6 lines?:

    Xrm.Page.getAttribute("new_showduplicateerror").setValue(true);

    Xrm.Page.getControl("new_showduplicateerror").setVisible(true);

    Xrm.Page.getControl("new_showduplicateerror").setNotification("A duplicate resource was found.  Please remove this before saving.");

          } else {

    Xrm.Page.getAttribute("new_showduplicateerror").setValue(false);

    Xrm.Page.getControl("new_showduplicateerror").setVisible(false);

    Xrm.Page.getControl("new_showduplicateerror").clearNotification();

    }

    Should I just replace them to look something like the example below?:

    executionContext.getFormContext().getAttribute("new_showduplicateerror").setValue(true);

    If yes, that portion of the code is still working, so I am not sure that is the issue.

  • Marco.P Profile Picture
    Marco.P 2,405 on at
    RE: JavaScript Web Resource issue: getGrid() suddenly started failing

    Replace Xrm.Page with executionContext.getFormContext();.

    You're still using deprecated API

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: JavaScript Web Resource issue: getGrid() suddenly started failing

    @Malani,

    That removed the error, but now the code still does not run execute.  It was working prior to the Wave 1 update.  Here is my web resource code:

    function GetTotalResourceCount(executionContext) {

       console.log("function started");

       var execContext = executionContext;

       var formContext = executionContext.getFormContext();

       var resourceyescount = 0;

       try {

    var gridContext=formContext.getControl("s_qd");

    var grid = gridContext.getGrid();

          // var gridCtx = formContext._gridControl;

           //var grid = gridCtx.getGrid();

           var allRows = grid.getRows();

           var duplicatesFound = 0;

           //loop through rows and get the attribute collection

           allRows.forEach(function (row, rowIndex) {

               var thisRow = row.getData().entity;

               var thisRowId = thisRow.getId();

    var thisResource = "";

    var thisResourceName = "";

    var thisResourceID = "";

               console.log("this row id=" + thisRowId);

               var thisAttributeColl = row.getData().entity.attributes;

               thisAttributeColl.forEach(function (thisAttribute, attrIndex) {

                   var msg = "";

                   if (thisAttribute.getName() == "new_resource") {

                       thisResource = thisAttribute.getValue();

    thisResourceID = thisResource[0].id;

    thisResourceName = thisResource[0].name;

    console.log("this resource name=" + thisResourceName)

                   }

               });

               var allRows2 = formContext.getGrid().getRows();

               //loop through rows and get the attribute collection

               allRows2.forEach(function (row, rowIndex) {

                   var thatRow = row.getData().entity;

                   var thatRowId = thatRow.getId();

                   var thatAttributeColl = row.getData().entity.attributes;

    var thatResource = "";

    var thatResourceName = "";

    var thatResourceID = "";

                   thatAttributeColl.forEach(function (thatAttribute, attrIndex) {

                       if (thatAttribute.getName() == "new_resource") {

                           thatResource = thatAttribute.getValue();

    thatResourceID = thatResource[0].id;

    thatResourceName = thatResource[0].name;

                           if (thatResourceID == thisResourceID && thatRowId != thisRowId) {

                               duplicatesFound++;

                               var msg = "Duplicate resource " + thatResource;

    console.log("duplicates found= " + duplicatesFound);

                           }

                       }

                   });

               });

           });

           if (duplicatesFound > 0) {

               console.log("duplicate found");

    Xrm.Page.getAttribute("new_showduplicateerror").setValue(true);

    Xrm.Page.getControl("new_showduplicateerror").setVisible(true);

    Xrm.Page.getControl("new_showduplicateerror").setNotification("A duplicate resource was found.  Please remove this before saving.");

           } else {

    Xrm.Page.getAttribute("new_showduplicateerror").setValue(false);

    Xrm.Page.getControl("new_showduplicateerror").setVisible(false);

    Xrm.Page.getControl("new_showduplicateerror").clearNotification();

    }

       } catch (err) {

           console.log('Error occurred :' + err)

       }

    }

    Here is a separate web resource that triggers the function:

    function TriggerSalesQDResourceCount(executionContext){

    var formContext = executionContext.getFormContext();

    formContext.getControl("s_qd").addOnLoad(GetTotalResourceCount);

    }

    Any ideas how I can fix this?

    Thanks!

  • Suggested answer
    Malini Johari Profile Picture
    Malini Johari 160 on at
    RE: JavaScript Web Resource issue: getGrid() suddenly started failing

    Hi,

    Please try gridContext=formContext.getControl("controlName");

    and then gridContext.getGrid();

  • Suggested answer
    Marco.P Profile Picture
    Marco.P 2,405 on at
    RE: JavaScript Web Resource issue: getGrid() suddenly started failing

    Hi,

    How do you define gridCtx?

    Anyway, here's a link that helped me manage my transition and revision of my js code:

    www.google.com/.../

    An extract:

    Earlier

    Xrm.Page.getControl(“Contacts”).getGrid();

    Now

             gridContext.getGrid();

    //where griContext = formContext.getControl(“Contacts”);

    Hope it helps,

    Marco

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!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans