Create record using Jscript in Microsoft Dynamics CRM 2011

Posted: October 11, 2012 in CRM 2011, Jscripts

Add Account record using Jscript

In my example i’m going to create account entity record in CRM.

To perform an ODATA call, we need to add jquery1.4.1.min and json2 files in your Form Properties. You can get those files from sdk.
path – \sdk\samplecode\js\restendpoint\jqueryrestdataoperations\jqueryrestdataoperations\scripts.

function CreateAccount() {
    var serverUrl = Xrm.Page.context.getServerUrl().toString();
    var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
    var ODATA_EntityCollection = "/AccountSet";

    var objAccount = new Object();
    // set the name of Account
    objAccount.Name = "Account created from jscript";

    // set the Primary Contact lookup field
    objAccount.PrimaryContactId = { Id: '{421E1EE5-500A-E211-B3FA-78E3B508F827}', LogicalName: "contact", Name: 'Adrian Dumitrascu (sample)' };

    // set the Address Type optionset field
    objAccount.Address1_AddressTypeCode = { Value: 2 };

    // set the Credit Hold boolean field
    objAccount.CreditOnHold = true;

    // Parse the entity object into JSON 
    var jsonEntity = window.JSON.stringify(objAccount);

        type: "POST",
        contentType: "application/json; charset=utf-8",
        datatype: "json",
        url: serverUrl + ODATA_ENDPOINT + ODATA_EntityCollection,
        data: jsonEntity,
        beforeSend: function (XMLHttpRequest) {
            XMLHttpRequest.setRequestHeader("Accept", "application/json");
        success: function (response) {
            if (response != null && response.d != null) {
        error: function (xmlHttpRequest, textStatus, errorThrown) {
            alert("Status: " + textStatus + "; ErrorThrown: " + errorThrown);

Leave a Reply

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

You are commenting using your 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