Skip to main content

Notifications

Announcements

No record found.

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

Javascript runs early using formContext

(0) ShareShare
ReportReport
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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,445 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans