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
    XM-22040801-0 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
    Jorge_ 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
    Jorge_ on at
    RE: Get gridContext always null

    Hi Lucas H 

    It is triggered from the Subgrid event OnRecordSelected

    pastedimage1680681622258v1.png

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

    Thanks a33ik

    Tried your code but it never reaches the myRows line.

  • XM-22040801-0 Profile Picture
    XM-22040801-0 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
    Lucas H 281 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
    XM-22040801-0 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
    a33ik 84,325 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
    Lucas H 281 on at
    RE: Get gridContext always null

    How is this function being called?

  • Jorge_ Profile Picture
    Jorge_ 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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,494 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,309 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans