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.
“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.
LikeLike
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.
LikeLike
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.
LikeLike
Hi,
How Can register crm2011 Plug-in.
LikeLike
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
LikeLike
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!
LikeLike
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
LikeLike
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.
LikeLike
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
LikeLike
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.
LikeLike
I haven’t covered that yet unfortunately. 😦 But thanks for the thought Ben.
LikeLike
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…
LikeLike
“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?
LikeLike
for me it said that “get_allRecordIds” don’t exist on the control.
LikeLike
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);
}
}
LikeLike
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
LikeLike
hi
im looking a way to retrieve only selected record/ row. for the moment it retrieve all row/record.
LikeLike
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;
},
LikeLike
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
LikeLike
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
LikeLike
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
LikeLike
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
LikeLike
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.
LikeLike
have a plugin which runs on the update event and then do the calculation
LikeLike
How can I use this when form type is 1???
LikeLike
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?
LikeLike
Hi,
Here’s my situation:
I have an entity, Project Work. The form has the field “Time Allocated (hrs)”. I have Project Work as a subgrid in the Project form. The Project form has “Total Allocated (hrs)”. This is because one Project can have many “Project Work”. How can I show the sum of all the “Time Allocated (hrs)” from Project Work in the “Total Allocated (hrs)” on the Project form?
Hope this had made sense.
Thank you
LikeLike
I think you would probably need a plugin calculating the Total Allocated hours, this would probably be triggered by updating/saving of a project work entity.
then you just show that field on the form.
LikeLike
Hi, I wan’t to update parent record’s id via a button placed on sub grid. Can some body please help me in this.
LikeLike
I would raise this question on the CRM Forum, where someone will help you with your question
https://community.dynamics.com/crm/f/117.aspx?Sort=Active&pi12232=1
LikeLike
Hi, I used your 2013 code to try make it work for my grid, but I keep getting Null returned
I believe it might be to do with the ids as its getting the record id’s in one long string
Any idea how to resolve this problem?
Thanks
LikeLike
Hi All,
I am stuck at this lone of code which is not getting recognised. Can some one plz help!
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
DK
LikeLike
Are you using CRM 2011?
LikeLike
Hi hosk.
Thanks for reply.
No , I am using Dynamic CRM 2015 onpremise to launch modal window by capturing Quick view subgrid row on double click. Plz point me in right direction as i struggled much with this.
I have scenario for which i need solution as i referred many blog posts but didn’t help.
On phone call entity form i have placed/ inserted a subgrid of quickview for customEntity1. As i can see the records in subgrid and once i click on 1st column which is hyperlinked get’s open with all the pre populated values in entity form. but i don’t want in this way because i need to enter again phone detail as it’s already navigated and lost record data instead i want to open the subgrid row highlighted/ selected record in modal window so that i can come back on phone call form without losing data. I am using Dyn CRM 2015 onpremise.
Please help for this!
Thanks
DK
LikeLike
I would raise this question on the CRM forum where one or many people will help you with this problem
https://community.dynamics.com/crm/f/117
LikeLike
Hi Hosk,
Thanks for that but i have already raised there but with lesser proper help so came to your blog
https://community.dynamics.com/crm/f/117/t/183121
Thanks
DK
LikeLike