CrmService.Create Method Using JScript

Posted: September 2, 2010 in JScript, Microsoft Dynamics CRM
Tags: , ,

Create record using JScript

This sample shows how to use the CrmService.Create method using the same example provided in the Server Programming Guide: CrmService.Create method.

To test this sample:

  1. Paste the following code into any Event Detail Properties dialog box.
  2. Save the form and then click Create Form on the Preview menu.

When the event occurs, the code will run. This code creates and opens a new contact record.

// Prepare values for the new contact.

var firstname = “Jesper”;

var lastname = “Aaberg”;

var donotbulkemail = “true”;

var address1_stateorprovince = “MT”;

var address1_postalcode = “99999”;

var address1_line1 = “23 Market St.”;

var address1_city = “Sammamish”;

var authenticationHeader = GenerateAuthenticationHeader();

// Prepare the SOAP message.

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‘>”+

authenticationHeader+

“<soap:Body>”+

“<Create xmlns=’http://schemas.microsoft.com/crm/2007/WebServices‘>”+

“<entity xsi:type=’contact’>”+

“<address1_city>”+address1_city+”</address1_city>”+

“<address1_line1>”+address1_line1+”</address1_line1>”+

“<address1_postalcode>”+address1_postalcode+”</address1_postalcode>”+

“<address1_stateorprovince>”+address1_stateorprovince+”</address1_stateorprovince>”+

“<donotbulkemail>”+donotbulkemail+”</donotbulkemail>”+

“<firstname>”+firstname+”</firstname>”+

“<lastname>”+lastname+”</lastname>”+

“</entity>”+

“</Create>”+

“</soap:Body>”+

“</soap:Envelope>”;

// Prepare the xmlHttpObject and send the request.

var xHReq = new ActiveXObject(“Msxml2.XMLHTTP”);

xHReq.Open(“POST”, “/mscrmservices/2007/CrmService.asmx”, false);

xHReq.setRequestHeader(“SOAPAction”,”http://schemas.microsoft.com/crm/2007/WebServices/Create”);

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

xHReq.setRequestHeader(“Content-Length”, xml.length);

xHReq.send(xml);

// Capture the result

var resultXml = xHReq.responseXML;

// Check for errors.

var errorCount = resultXml.selectNodes(‘//error’).length;

if (errorCount != 0)

{

var msg = resultXml.selectSingleNode(‘//description’).nodeTypedValue;

alert(msg);

}

// Open new contact record if no errors.

else

{

var contactid = resultXml.selectSingleNode(“//CreateResult”);

window.open(“/sfa/conts/edit.aspx?id={“+contactid.nodeTypedValue+”}”);

}

A successful response includes XML with a CreateResponse element that returns the ID for the record created. The following is an example of a successful response:

<?xml version=”1.0″ encoding=”utf-8″?>
<soap:Envelope xmlns:soap=”http://schemas.xmlsoap.org/soap/envelope/&#8221; xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221; xmlns:xsd=”http://www.w3.org/2001/XMLSchema”&gt;
<soap:Body>
<CreateResponse xmlns=”http://schemas.microsoft.com/crm/2007/WebServices”&gt;
<CreateResult>368c8b1b-851c-dd11-ad3a-0003ff9ee217</CreateResult>
</CreateResponse>
</soap:Body>
</soap:Envelope>

Advertisements

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