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();
}

  • XM-22040801-0 Profile Picture
    11 on at
    RE: Get gridContext always null

    Would something like this suit you?

    Subscribe the FormLoad function to the on form load event.

    // On Form Load event handler
    this.FormLoad = function(eContext) {
        const formCtx = eContext.getFormContext();
        const gridCtrl = formCtx.getControl("logs");
        gridCtrl.addOnLoad((e) => this.GridLoad(e));
        
        // You can also try to affect this.gridCtrl to gridCtrl here and remove GridLoad
    }
    
    this.GridLoad = function(eContext) {
        const formCtx = eContext.getFormContext();
        // gridCtrl will be used by SelectRows
        this.gridCtrl = formCtx.getControl("logs");
        formCtx.ui.setFormNotification("Grid loaded", "INFO", "GridLoaded");
    }
    
    this.SelectRows = function(eContext) {
        const grid = this.gridCtrl?.getGrid();
    
        if (grid == null) {
            Xrm.Navigation.openAlertDialog({ text: `Grid not loaded` });
            return;
        }
        
        const allRows = grid.getRows();
        const selectedRows = grid.getSelectedRows();
    
        // Only one row can be selected because grid record select doesn't trigger when multiple rows are selected
        const selectedRow = selectedRows.get(0);
        const selectedId = selectedRow.data.entity.getId();
    
        const selectedRowIndex  = allRows.get()
            .findIndex(row => row.data.entity.getId() === selectedId);
        
        if (selectedRowIndex < 0) {
            Xrm.Navigation.openAlertDialog({ text: `Selected row not found` });
            return;
        }
        
        const nextRowIndex = selectedRowIndex   1;
        
        if (nextRowIndex >= allRows.getLength()) {
            Xrm.Navigation.openAlertDialog({ text: `No next row` });
            return;
        }
    
        const nextRow = allRows.get(nextRowIndex);
    
        Xrm.Navigation.openAlertDialog({
            text: `Next row: ${nextRow.data.entity.attributes.get("").getValue()}`
        });
    
        // Modify the next row
        nextRow.data.entity.attributes.get("").setValue("Other value");
    }

  • Jorge_ Profile Picture
    on at
    RE: Get gridContext always null

    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

  • Jorge_ Profile Picture
    on at
    RE: Get gridContext always null

    Hi Lucas H 

    It is triggered from the Subgrid event OnRecordSelected

    pastedimage1680681622258v1.png

  • Jorge_ Profile Picture
    on at
    RE: Get gridContext always null

    Thanks a33ik

    Tried your code but it never reaches the myRows line.

  • XM-22040801-0 Profile Picture
    11 on at
    RE: Get gridContext always null

    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 ?

  • Lucas H Profile Picture
    283 on at
    RE: Get gridContext always null

    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.

  • Suggested answer
    XM-22040801-0 Profile Picture
    11 on at
    RE: Get gridContext always null

    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()}` });

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: Get gridContext always null

    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();
    }

  • Lucas H Profile Picture
    283 on at
    RE: Get gridContext always null

    How is this function being called?

  • Jorge_ Profile Picture
    on at
    RE: Get gridContext always null

    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

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

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Daivat Vartak (v-9davar) Profile Picture

Daivat Vartak (v-9d... 225 Super User 2025 Season 1

#2
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 97

#3
Vahid Ghafarpour Profile Picture

Vahid Ghafarpour 82 Super User 2025 Season 1

Overall leaderboard

Product updates

Dynamics 365 release plans