Update sub grids record in MS CRM

Did you get a requirement to update child entity records available in sub grid in parent entity, so for example if you are working opportunity and you need to update all the related quotes records which is available in quote sub grids under opportunity screen. You can use below code and can modify based on your requirement

function GetAllQuotesIds() {
var gridControl = document.getElementById(“opportunityQuotesGrid”).control; //Change grid name here
var subGrid = gridControl.get_allRecordIds();
for (i = 0; i < subGrid.length; i++) {
var Quote = new Object();
Quote.Name = “Demo”;    // change to update your field
var Id = subGrid[i].replace(“{“, “”).replace(“}”, “”);
UpdateQuotes(Id,Quote); } }

function UpdateQuotes(Id,Quote)
var jsonEntity = JSON.stringify(Quote);
var ODataPath = Xrm.Page.context.getServerUrl() + “/XrmServices/2011/OrganizationData.svc/QuoteSet”; //Change entity name here
$.ajax({ type: “POST”,
contentType: “application/json; charset=utf-8”,
datatype: “json”,
data: jsonEntity,
url: ODataPath + “(guid'” + Id + “‘)”,
beforeSend: function (XMLHttpRequest)
{XMLHttpRequest.setRequestHeader(“Accept”, “application/json”);
XMLHttpRequest.setRequestHeader(“X-HTTP-Method”, “MERGE”); },
success: function (data, textStatus, XmlHttpRequest) { alert(“Success”); },
error: function (XMLHttpRequest, textStatus, errorThrown) { alert(“Error while updating record” } }); }

Make sure you have REST and Jquery libraries are added to your form.

Hope it will help someone.

Enjoy !!!

Leave a comment

Filed under Ajax, MS CRM 2011, MS CRM 2013

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s