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?
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();
}
I believe I am using the addOnLoad event, however, if I use formContext the onload event launches immediately.
Hi daniellee,
Have you tried to move the code to the subgrid onload event ?
Reference : docs.microsoft.com/.../addonload
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,134 Super User 2024 Season 2
Martin Dráb 229,928 Most Valuable Professional
nmaenpaa 101,156