Home > CRM 2011, JScript > Create record synchronously using OData and JScript in CRM 2011

Create record synchronously using OData and JScript in CRM 2011


Below is the Jscript function to create record synchronously using OData and Jscript

function createRecordSync(entityObject, odataSetName) {

var jsonEntity = window.JSON.stringify(entityObject);

// Get Server URL

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

//The OData end-point

var ODATA_ENDPOINT = “/XRMServices/2011/OrganizationData.svc”;

var createRecordReq = new XMLHttpRequest();

var ODataPath = serverUrl + ODATA_ENDPOINT;

createRecordReq.open(‘POST’, ODataPath + “/” + odataSetName, false);

createRecordReq.setRequestHeader(“Accept”, “application/json”);

createRecordReq.setRequestHeader(“Content-Type”, “application/json; charset=utf-8”);


var newRecord = JSON.parse(createRecordReq.responseText).d;

return newRecord;


How do I call this method :-

  • Create a new .jscript file (i.e., “account.js”)
  • Copy & Paste above code
  • Add “account.js” as a webresource in CRM
  • Add “Json2.js” & “jquery1.4.1.min.js” helper script files as webresources
  • Get the helper files from CRM 2011 SDk under path “\sdk\samplecode\js\restendpoint\jqueryrestdataoperations\jqueryrestdataoperations\scripts”
  • To create an “Account”
  • Pass “Account” object, OData Set name (i.e.,AccountSet)

var account = new Object();

account.Name = “Rajeev Pentyala”;

var createdAccount = createRecordSync(account, “AccountSet”);

if (createdAccount) {

alert(“New Account Created !!!; Id – “+createdAccount.AccountId);



  1. ezra
    May 30, 2013 at 11:57 AM

    when i try this code i get newRecord=undifind
    which meen that it didn’t create the record (it didn’t create the record),
    please help?

  2. ezra
    May 30, 2013 at 12:20 PM

    i found my problem.

    • Nawaf Khalife
      January 2, 2014 at 5:52 AM

      How did you resolve it please? I am getting the same error.

  3. January 29, 2018 at 11:40 PM

    at createRecordReq.send(jsonEntity);.. I am getting error.. Failed to load resource: the server responded with a status of 500 (Internal Server Error)? could you please help?

  1. No trackbacks yet.

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 )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: