, , ,

Hello Everyone,

In my Previous post I created a lead from from a custom Entity. Now my next step was to change the Status of that Custom Entity as Inactive, close the Entity and Refresh the Parent Grid View.

To Deactivate an Entity through Javascript, I found a very nice blog from Guru Prasad which I would like to share here, so that I can consult this every-time I use this functionality again.

After this my next step was to Refresh the Parent Grid View after record Deactivation.

1. Refresh the Grid from Parent Homepage:


You can view this function being called at OnClick event, from the Developers Tool by pressing F12 and selecting the Green Refresh Button

2. Refresh the Grid from inside the Form

//This refreshes the entire element in parent window that contains the view

//This refreshes just the grid control that contains the view

So I used the Second option in my Jscript-code which I’m calling within the Form. I called the Refresh just after Deactivating my record and before Closing the form(as since M using windows.parent.opner).

Finally my complete code would be:

function deactivate() {
function changeRecordStatus(RECORD_ID,stateCode,statusCode) {
// create the SetState request
 var request = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">";
 request += "<s:Body>";
 request += "<Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">";
 request += "<request i:type=\"b:SetStateRequest\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\" xmlns:b=\"http://schemas.microsoft.com/crm/2011/Contracts\">";
 request += "<a:Parameters xmlns:c=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">";
 request += "<a:KeyValuePairOfstringanyType>";
 request += "<c:key>EntityMoniker</c:key>";
 request += "<c:value i:type=\"a:EntityReference\">";
 request += "<a:Id>" + RECORD_ID + "</a:Id>";
 request += "<a:LogicalName>new_EntityName</a:LogicalName>";
 request += "<a:Name i:nil=\"true\" />";
 request += "</c:value>";
 request += "</a:KeyValuePairOfstringanyType>";
 request += "<a:KeyValuePairOfstringanyType>";
 request += "<c:key>State</c:key>";
 request += "<c:value i:type=\"a:OptionSetValue\">";
 request += "<a:Value>"+stateCode+"</a:Value>";
 request += "</c:value>";
 request += "</a:KeyValuePairOfstringanyType>";
 request += "<a:KeyValuePairOfstringanyType>";
 request += "<c:key>Status</c:key>";
 request += "<c:value i:type=\"a:OptionSetValue\">";
 request += "<a:Value>"+statusCode+"</a:Value>";
 request += "</c:value>";
 request += "</a:KeyValuePairOfstringanyType>";
 request += "</a:Parameters>";
 request += "<a:RequestId i:nil=\"true\" />";
 request += "<a:RequestName>SetState</a:RequestName>";
 request += "</request>";
 request += "</Execute>";
 request += "</s:Body>";
 request += "</s:Envelope>";
//send set state request
 type: "POST",
 contentType: "text/xml; charset=utf-8",
 datatype: "xml",
 url: Xrm.Page.context.getServerUrl() + "/XRMServices/2011/Organization.svc/web",
 data: request,
 beforeSend: function (XMLHttpRequest) {
 XMLHttpRequest.setRequestHeader("Accept", "application/xml, text/xml, */*");
 XMLHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
 success: function (data, textStatus, XmlHttpRequest) {
 error: function (XMLHttpRequest, textStatus, errorThrown) {

After Publishing the WebResource, In case you get an error “$ is Undefined”. Kindly Visit my Next blog for details.

Hope this was Helpful.

Thanks !!!