Someone posted a comment on my blog asking about Javascript and subgrids. Although I don’t yet need to do anything with Javascript and subgrids I thought I would. It certainly shows the benefit of Microsoft having forums for CRM problems.
Firstly the question is how to get the values from a subgrid, I found this good forum post and the Javascript is below
You can inspect the subgrid values on save by doing the following:
var gridControl = document.getElementById('subgrid_id').control;var ids = gridControl.get_allRecordIds();for(i = 0; i < ids.length; i++){var cellValue = gridControl.getCellValue('column_name', ids[i]);// logic}
Doing this on load is a bit more challenging since subgrids are loaded asynchronously and aren’t likely to be done loading when the form onload event fires. You can check the grid periodically though to see when it’s done loading by calling a function like the following in your form onload:
function subGridOnload(){var grid = document.getElementById('grid_identifications');if (grid.readyState!="complete"){// delay one second and try again.setTimeout(subGridOnload, 1000);return;}// logic}
I then also found a forum post on attaching events to a subgrid, which you can read here
For CRM 2011, your code should look like this:
if (Xrm.Page.ui.getFormType() != 1) {
Xrm.Page.ui.controls.get("IFRAME_OpportunityLines").setSrc(GetFrameSource("fj_opportunity_new_opportunitymanagementmodu"));;
Xrm.Page.ui.controls.get("IFRAME_OpportunityLines").onreadystatechange = function oppLineTotals() {
if (Xrm.Page.ui.controls.get("IFRAME_OpportunityLines").readyState == 'complete') {
var iFrame = frames[window.event.srcElement.id];
iFrame.document.all.crmGrid.attachEvent("onrefresh", GridRefresh);
}
}
}
Funny thing, the sdk recomended function did not work for me (i.e. Xrm.Page.ui.controls.get(“GridName”) ), and yet the following works (tested!)
var grid = document.getElementById("GridName");
grid.attachEvent("onrefresh", EventHandlerFunction);
because the grid.htc (Html Component) actually includes a public event “onrefresh”… And this event can be handled with any function. We dont need to dive into the eventManager and scriptEvents.



Dan Rigby
May 26, 2011
“Funny thing, the sdk recomended function did not work for me (i.e. Xrm.Page.ui.controls.get(“GridName”) ), and yet the following works (tested!)”
From what I’ve read, this was a bug introduced in Rollup 1 of CRM 2011.
Dan Rigby
May 26, 2011
Also, in my testing, the subgrid onrefresh event doesn’t get fired when the grid first loads it’s data, only on subsequent clicks of the refresh button.
Bhanu Bharadwaj
June 1, 2011
HI Every one,
I am creating a simple form in crm2011 for calculating salary. I have added the field on the form which are following
Basic
H.R.A. = 10 percent of basic;
T.A. = 20 percent of basic;
Gross = Basic + H.R.A.+T.A.;
The requirement is this , When I will enter the value in basic it should auto calculate the value of other field.
Kindly tell me how can i do this and where i write the code.
Bhanu Bharadwaj
June 2, 2011
Hi,
How Can register crm2011 Plug-in.
jz
June 8, 2011
hi,
i need to access the total record count dispalyed at bottom of the grid inside an iframe. please reply back if you have answer
Houseedition
June 22, 2011
Hi jz, I dont understand what total record is that you need, but I found this two methods.
- gridControl.get_selectedRecordCount()
- gridControl.get_totalRecordCount()
I have a question, How can I set value for some cell, I need add a link
var gridControl = document.getElementById(‘subgrid_id’).control;
var ids = gridControl.get_allRecordIds();
for(i = 0; i < ids.length; i++)
{
var cellValue = gridControl.getCellValue('column_name', ids[i]);
if (cellValue=="")
{
setValue("“);
}
I hope that can help me, Thanks!
Ian
August 16, 2011
is it possible to change the ‘view’ that the subgrid uses via javascript.
i’ve used the ‘addCustomView’ for a lookup control and would like the same concept for the subgrid
Gareth Tucker
August 22, 2011
Hi, any idea whether you can change the query behind a sub-grid via java script – i.e. modifiy the fetch xml to filter the results returned.
Hosk
August 22, 2011
I’m not sure but if it can be done then I would search Jamie Mileys blog – http://mileyja.blogspot.com/
I also found this blog post
http://javascripterrors.blogspot.com/2011/08/crm-2011-change-subgrid-view-java.html
pt72
August 31, 2011
I believe this will only get you records in the current page of the subgrid view. If that is correct, any idea how to retrieve all rows from all pages? My only thought is to use an oData call. Thanks.
Jamie Miley (@mileyja)
September 14, 2011
I haven’t covered that yet unfortunately.
But thanks for the thought Ben.
Ciaran
November 8, 2011
A problem has arisen recently with our CRM online where var gridControl = document.getElementById(‘subgrid_id’) is now returning null. I’ve looked at the HTML of the from in the browser and my grid “name” is there as an id. Anyone have any clue as to why the call to get the subgrid element fails and if there’s any workaround? The crm 2011 client side sdk says calls to document.getElementById(…) are not supported. I assume the reason is that MS don’t want to guarantee the HTML they output won’t change but if my subgrid is there an an element with an id = “my subgrid name”, how come the JS call returns null? Perplexed…
Jonathan
November 24, 2011
“I believe this will only get you records in the current page of the subgrid view. If that is correct, any idea how to retrieve all rows from all pages? My only thought is to use an oData call. Thanks.”
var ids = gridControl.get_allRecordIds();
I have noticed this too, does anyone know how to retrieve the IDs for all rows from all pages?
damien
November 29, 2011
for me it said that “get_allRecordIds” don’t exist on the control.
Lucas
November 29, 2011
The following will return the number of records found in the sub grid for line items on the opportunity form.
function TimeOutGrid() {
setTimeout(WriteInCount, 10000);
}
function WriteInCount() {
var spanList = document.getElementById(‘opportunityproductsGrid_d’).getElementsByTagName(“span”);
for (var i = 0; i < spanList.length; i++) {
if (spanList[i].id != null && spanList[i].id == "opportunityproductsGrid_ItemsTotal")
alert(spanList[i].innerText);
}
}
damien
November 30, 2011
thanks it works now. how can i retrieve de GUID of the row because i can’t display it the column in the sub grid then: how can i retrive it ? thanks
damien
December 2, 2011
hi
im looking a way to retrieve only selected record/ row. for the moment it retrieve all row/record.
Lucas
December 5, 2011
You can use JSCRIPT to get your id.
getSelectedRows : function(id){
var rows = [];
//gridBodyTable
var gridBodyTable = $(‘#’+id).contents().find(‘#gridBodyTable’);
gridBodyTable.children().eq(1).children().each(function(i,o){
var oid = $(o).attr(‘oid’);
var selected = $(o).attr(‘selected’);
//console.log(‘oid:’,oid,’ — ‘,’selected:’,selected)
//console.log(typeof (selected));
if(selected == true){
rows[rows.length] = oid;
}
});
//console.log(rows);
return rows;
},
Vineet
December 26, 2011
Hi,
I am setting up the url of iframe in Contract entity using JS. My iframe url points to Associated View url. Now, I want to get the id of a record which has been selected in an iframe using JS.
Fyi, I have tried the below code.
var gridControl = document.getElementById(“yourSubGridName”).control;
var ids = gridControl.get_selectedIds();
I cannot see the method name as get_selectedIds() which will give you the id of a selected row. Please suggest..
Thanks,
-Vineet
NewPath Technologies (@newpathtech)
February 14, 2012
I am not sure the recursive call to keep waiting for the grid container will work. I tried this technique and got the code to keep calling itself. Here’s the approach I tried which is similar to your approach but rather uses setTimeout to call another function that reads the data from the grid after a predetermined amount of time. I found 1000-2000 ms seems to work well. But I realize it is not ideal.
http://blog.newpathnetwork.org/2012/02/accessing-subgrids-aka-grids-on-crm2011-on-onload-and-onsave-events.html
Ryan Corrigal
March 13, 2012
If you are trying to focus a SubGrid with JavaScript, please see the article I wrote: http://prodynamicscrm.com/articles/using-javascript-set-focus-subgrid-control
Subhagya
April 23, 2012
Hello Everyone……
I want to divide one field on the CRM form by another field and get the result on the third field.
Kindly suggest
Subhagya
April 23, 2012
like…… if I have got:
1) Projects Opened
2) Projects Awarded
3) Average of Awardation => It should automatically calculate => Projects Awarded/Project Opened*100
Kindly suggest.
Hosk
April 23, 2012
have a plugin which runs on the update event and then do the calculation
himanshu
May 26, 2012
How can I use this when form type is 1???
Darren
June 19, 2012
Hello, I have used your code to attach an event to the the subgrid on refresh and it worked fine. Now I upgraded to UR8 and it stopped working, Any idea why and how i can fix it please?