web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Suggested Answer

Javascript runs early using formContext

(0) ShareShare
ReportReport
Posted on by 11

I was trying to figure out how to retrieve values from a subgrid, I was constantly seeing 0 rows from the code.  This subgrid has values, however when using the formContext, rather than Xrm.Page the debugger indicates that the code is run prior to subgrid load.

function formOnLoad(executionContext){
var formContext = executionContext.getFormContext();
var grid = formContext.getControl("Products");
grid.addOnLoad(ShowSubGridItems);
}

function ShowSubGridItems(executionContext){
//var formContext = executionContext.getFormContext();
debugger;
//var allRows =formContext.getControl("Products").getGrid().getRows();
var allRows =Xrm.Page.getControl("Products").getGrid().getRows();
var row = allRows.get(0);
var gridEntity = row.getData();
var entity = gridEntity.getEntity();
var entityReference = entity.getEntityReference();
var primeAttributeValue = entity.getPrimaryAttributeValue();
}

As you can see I have commented out the code that has the formContext and changed the function call above, to the current Xrm.Page.  The question I have is there something that I am missing here that needs to be in place when using formContext?

  • Luis Nogueira Profile Picture
    Microsoft Employee on at

    Hi daniellee,

    Have you tried to move the code to the subgrid onload event ?

    Reference : docs.microsoft.com/.../addonload

  • daniellee Profile Picture
    11 on at

    I believe I am using the addOnLoad event, however, if I use formContext the onload event launches immediately.

  • Suggested answer
    Community Member Profile Picture
    on at

    Below I put updated code for you to try, if you cant get it running (Ive had trouble with the subgrid code before as well) you could switch to using a retrievemultiple.

    Try changing it to this:

    function formOnLoad(executionContext){

    var formContext = executionContext.getFormContext();

    var grid = formContext.getControl("Products");

    var myContactsGridOnloadFunction = function () { console.log("Contacts Subgrid OnLoad event occurred") };

       grid.addOnLoad(ShowSubGridItems);

    //grid.addOnLoad(ShowSubGridItems);

    }

    function ShowSubGridItems(executionContext){

    //var formContext = executionContext.getFormContext();

    debugger;

    //var allRows =formContext.getControl("Products").getGrid().getRows();

    var allRows =Xrm.Page.getControl("Products").getGrid().getRows();

    var row = allRows.get(0);

    var gridEntity = row.getData();

    var entity = gridEntity.getEntity();

    var entityReference = entity.getEntityReference();

    var primeAttributeValue = entity.getPrimaryAttributeValue();

    }

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the August Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
11manish Profile Picture

11manish 61 Super User 2026 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 38 Most Valuable Professional

#3
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 22 Super User 2026 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans