Skip to main content

Notifications

Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

Javascript runs early using formContext

Posted on by 5

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?

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Javascript runs early using formContext

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

    }

  • daniellee Profile Picture
    daniellee 5 on at
    RE: Javascript runs early using formContext

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

  • RE: Javascript runs early using formContext

    Hi daniellee,

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

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

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Complete!

🔔 Be sure to subscribe to the new forums you are interested in to stay up to date! 🔔

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 229,928 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans