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 :
Customer experience | Sales, Customer Insights,...
Suggested Answer

Get gridContext always null

(0) ShareShare
ReportReport
Posted on by

Hi,

I have an editable grid and I cannot get gridContext using JS.

I have tries with OnChange and OnRecordSelect events, but its always null. 

grid name is correct

Client API grid context in model-driven apps - Power Apps | Microsoft Learn

this.SelectRows = function (eContext) {
	
	var formContext = eContext.getFormContext();
	var gridContext = formContext.getControl("logs");
	
	if (gridContext === null) {
         setTimeout(LogOptimisationAudit.SelectRows(eContext), 3000);
         return;
     }
	
	 var myRows = gridContext.getGrid().getRows();
}

I have the same question (0)
  • Lucas H Profile Picture
    283 on at

    A few things that will fix your issue:

    1) You're using setTimeout incorrectly. Arguments for the function go AFTER the delay. The function reference should just be the function name.

    	if (gridContext === null) {
             setTimeout(LogOptimisationAudit.SelectRows, 3000, eContext);
             return;
         }

    2) If all you're trying to do is wait until the grid is loaded to try and get the gridContext again, I would try using an arrow function within your setTimeout. I've also removed the return statement from your condition. If your condition ran it would stop the this.SelectRows function and never get to setting your myRows variable.

        var formContext = eContext.getFormContext();
    	var gridContext = formContext.getControl("logs");
    	
    	if (gridContext === null) {
             setTimeout(() => {
             gridContext = formContext.getControl("logs");
             }, 3000);
         }

    3) Depending on where this grid is in your form and what you're trying to do with your grid data, you can call this function on form load or on tab state change to get the grid rows immediately. Otherwise, using onRecordSelect would also work well since you know the grid is already loaded.

    4) Lastly, consider using let instead of var for more standard code practices. No biggy tho!

    Hope this helps!

  • Jorge_ Profile Picture
    on at

    Thanks Lucas,

    however tried your code but still unable to get grid context

    It is an editable sub grid, not sure if that makes any difference.

    pastedimage1680620490619v1.png

    Correct grid name also

    pastedimage1680620684118v2.png

  • Lucas H Profile Picture
    283 on at

    How is this function being called?

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    Try the following code:

    this.SelectRows = function (eContext) {
    	
    	var formContext = eContext.getFormContext();
    	var gridContext = formContext.getControl("logs");
    	
    	if (gridContext === null) {
             setTimeout(LogOptimisationAudit.SelectRows, 3000, eContext);
             return;
         }
    	
    	 var myRows = gridContext.getGrid().getRows();
    }

  • Suggested answer
    XM-22040801-0 Profile Picture
    11 on at

    Hello,

    Indeed, the gridContext is undefined null. You can't use it.

    If you want to know the selected record, use formContext.data.entity.

    Remember that this event is trigger only if one and only one row is selected.

    Example:

    const formCtx = eContext.getFormContext();
    
    const selectedRecord = formCtx.data.entity;
    const attributes = selectedRecord.attributes;
    
    Xrm.Navigation.openAlertDialog({ text: `${selectedRecord.getEntityName()}: ${attributes.get("fullname")?.getValue()}` });

  • Lucas H Profile Picture
    283 on at

    I don't think this is what he's trying to do - looks like he wants to get a collection of rows in a subgrid. GridContext isn't undefined, it's null, so it's not getting the grid after it's loaded or it's not being called in the right context.

  • XM-22040801-0 Profile Picture
    11 on at

    My bad, it is null, not undefined.

    I also thought that he would like to get a list, but since the recordselect and gridchange events only fire on one row/cell, he will never be able to be obtained on these events. There will always be only one row selected when the event occurs.

    In the record select event, the formContext is associated with the subgrid entity, not the main form entity. It will not work. See github.com/.../3872

     Jorge_ What do you want to do with the selected rows ?

  • Jorge_ Profile Picture
    on at

    Thanks a33ik

    Tried your code but it never reaches the myRows line.

  • Jorge_ Profile Picture
    on at

    Hi Lucas H 

    It is triggered from the Subgrid event OnRecordSelected

    pastedimage1680681622258v1.png

  • Jorge_ Profile Picture
    on at

    Hi Xavier Monin

    I'm trying to get collections of Row as I need to set some values on next row.

    That's the only way I found to so so, get all rows, and access selected row + 1

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 > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 170 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 70

#3
Jimmy Passeti Profile Picture

Jimmy Passeti 50 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans