Advertisements

Archive

Archive for February 4, 2012

Assign record using JScript in CRM 2011

February 4, 2012 7 comments

Hi,

Below is the Jscript to assign a record to particular User using SOAP.

function AssignRecord(Assignee, Target, entityName) {

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\””;

request += ” xmlns:i=\”http://www.w3.org/2001/XMLSchema-instance\”>”;

request += “<request i:type=\”b:AssignRequest\””;

request += ” xmlns:a=\”http://schemas.microsoft.com/xrm/2011/Contracts\””;

request += ” 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>Target</c:key>”;

request += “<c:value i:type=\”a:EntityReference\”>”;

request += “<a:Id>” + Target + “</a:Id>”;

request += “<a:LogicalName>” + entityName + “</a:LogicalName>”;

request += “<a:Name i:nil=\”true\” />”;

request += “</c:value>”;

request += “</a:KeyValuePairOfstringanyType>”;

request += “<a:KeyValuePairOfstringanyType>”;

request += “<c:key>Assignee</c:key>”;

request += “<c:value i:type=\”a:EntityReference\”>”;

request += “<a:Id>” + Assignee + “</a:Id>”;

request += “<a:LogicalName>systemuser</a:LogicalName>”;

request += “<a:Name i:nil=\”true\” />”;

request += “</c:value>”;

request += “</a:KeyValuePairOfstringanyType>”;

request += “</a:Parameters>”;

request += “<a:RequestId i:nil=\”true\” />”;

request += “<a:RequestName>Assign</a:RequestName>”;

request += “</request>”;

request += “</Execute>”;

request += “</s:Body>”;

request += “</s:Envelope>”;

// Get server URL

var serverUrl = Xrm.Page.context.getServerUrl();

var req = new XMLHttpRequest();

req.open(“POST”, serverUrl, true)

// Responses will return XML. It isn’t possible to return JSON.

req.setRequestHeader(“Accept”, “application/xml, text/xml, */*”);

req.setRequestHeader(“Content-Type”, “text/xml; charset=utf-8”);

req.setRequestHeader(“SOAPAction”, “http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute&#8221;);

req.onreadystatechange = function () { assignResponse(req); };

req.send(request);

}

// This function gets called after the execution

function assignResponse(req) {

if (req.readyState == 4) {

if (req.status == 200) {

alert(“Success !!!”);

}

else {

alert(“Error – ” + req.responseXML);

}

}

}

How Do I Call this function :-

  • Pass User Id, Record Id and Entity name to the function

var userId = “”      // GUID of user whome to assign the record

var recordId = “”      // GUID of record you want to assign

var entityName = “”  // Entity name the record

AssignRecord(userId,recordId,entityName);

Hope it helps 🙂

Advertisements

Closing Aspx page on PostBack by suppressing close confirmation

February 4, 2012 Leave a comment

Hi,

Below is the code to close the current .aspx page in the Postback (i.e., On Button click) without “Close” confirmation dialog

// Close the window with no close confirmation

Response.Write(“<script language=’javascript’> { window.open(”, ‘_self’, ”); window.close(); }</script>”);

Hope it helps 🙂