Using JavaScript to assign a record

A while back, a user on the Microsoft Dynamics forums asked how to assign a record using JavaScript. Here’s the code, remember to update the userid and recordid variables.

var header = GenerateAuthenticationHeader();

var userid = "838975f4-029c-de11-9693-0003ffc4c746";
var recordid = "9721c859-d185-dc11-ac34-000c291d8da3";
var target = "TargetOwnedAccount";

var xml = "" +
"<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
header +
" <soap:Body>" +
" <Execute xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" +
" <Request xsi:type=\"AssignRequest\">" +
" <Target xsi:type=\""+ target +"\">" +
" <EntityId>"+ recordid +"</EntityId>" +
" </Target>" +
" <Assignee>" +
" <PrincipalId xmlns=\"http://schemas.microsoft.com/crm/2006/CoreTypes\">"+ userid +"</PrincipalId>" +
" <Type xmlns=\"http://schemas.microsoft.com/crm/2006/CoreTypes\">User</Type>" +
" </Assignee>" +
" </Request>" +
" </Execute>" +
" </soap:Body>" +
"</soap:Envelope>" +
"";

var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");

xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xmlHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Execute");
xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
xmlHttpRequest.send(xml);

var resultXml = xmlHttpRequest.responseXML;

(I pasted it here so that I can find it easily in future).

Consuming the MS CRM Web Service for a Secondary Organisation

If you’ve got multiple CRM organisations in a MS CRM instance, you would have noticed that connecting to the default Web Service gives you only the customisations of the default organisation. In the past, I’ve been downloading the WSDL files in the Customisation section for each of my secondary organisations and then creating a Web Service reference to the downloaded file- especially annoying when you make changes to your customisation and have to keep the file updated!

Well, I was about to do the same this morning.. until I discovered realised that I could simply consume the download URL of the WSDL file itself:

http://SERVERNAME/MSCrmServices/2007/CrmServiceWsdl.aspx?uniquename=ORGANISATION

Why didn’t I discover this before?

Performing a Javascript Web Service call to the Metadata Service

About a week ago a colleague of mine told me that he’d been able to use my JS script to retrieve fields from a related record upon the change of a lookup value- which worked exactly as he wanted. However, one of the fields he retrieved was a Picklist value- which was obviously a numeric value and pretty pointless since the field he was displaying the value in was nvarchar.

He had three options;

1) Hard code the text values for each option value in his code (yeah, right!),

2) Create a picklist with the same values (text and numeric) as the source picklist and ensure that his documentation clearly outlines that the two picklists will forever have to kept in sync (umm, no thanks),

3) Perform a web service call to the Metadata service and retrieve the options for the source picklist (!).

So I decided to put together the JavaScript code to achieve (3). In the example below, I’m retrieving the accountcategorycode field from the Account entity.

var request = "" +
"<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
GenerateAuthenticationHeader() +
" <soap:Body>" +
" <Execute xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" +
" <Request xsi:type=\"RetrieveAttributeRequest\">" +
" <MetadataId>00000000-0000-0000-0000-000000000000</MetadataId>" +
" <EntityLogicalName>account</EntityLogicalName>" +
" <LogicalName>accountcategorycode</LogicalName>" +
" <RetrieveAsIfPublished>false</RetrieveAsIfPublished>" +
" </Request>" +
" </Execute>" +
" </soap:Body>" +
"</soap:Envelope>" +
"";
var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
xmlHttpRequest.Open("POST", "/mscrmservices/2007/MetadataService.asmx", false);
xmlHttpRequest.setRequestHeader("SOAPAction",
"http://schemas.microsoft.com/crm/2007/WebServices/Execute");
xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlHttpRequest.setRequestHeader("Content-Length", request.length);
xmlHttpRequest.send(request);
var result = xmlHttpRequest.responseXML;
alert(result.xml);

Hope that helps somebody out there!