Create Record using Web API

Welcome to my old Blogspot blog! You can read this post on our new website: Read this post on our new website 

Following is detail sample code to create a record using Web API in Dynamics CRM.
/*
entityPlurarName: entityPlurarName is the plural entity logical name of entity e.g for account it is accounts. for opportunity it is opportunities
entityObject: entityObject is an object that of entity contians fields and values that needed to be create.
e.g
  var entityObject = {};
      entityObject["originatingleadid@odata.bind"] = "/leads(" + guid + ")";  // lookup   
      contact["firstname"] = string value;  // Single line of text 
      contact["po_preferredlanguage"] = string value; //Option set
      contact["donotemail"] = true/false //two Option  
*/
function createRecord(entityPlurarName, entityObject) {
    var id = null;
    var req = new XMLHttpRequest();
    req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/" + entityPlurarName, false);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.send(JSON.stringify(entityObject));
    if (req.readyState === 4) {
        if (req.status === 204) {
            var uri = req.getResponseHeader("OData-EntityId");
            var regExp = /\(([^)]+)\)/;
            var matches = regExp.exec(uri);
            id = matches[1];
        }
        else {
            var error = JSON.parse(req.response).error;
            Xrm.Utility.alertDialog(error.message);
        }
    }
    return id;
}
You can call the create record method as:
var account = {};
account["primarycontactid@odata.bind"] = "/contacts(" + removeCurlyBraces(Xrm.Page.getAttribute("primarycontactid").getValue()[0].id) + ")";  // lookup 
account["name"] = "Test Account name";  // Single line of text 
account["paymenttermscode"] = "1";//Text(Net 30) //Option set
account["creditonhold"] = true; //two Option
var accountid=createRecord("accounts", account);

3 comments:

  1. Hey Siddique! OData-EntityId header was just was I was looking for, thanks! :-)

    ReplyDelete
  2. Hi Siddique I'm using originatingleadid@odata.bind":"/leads(66a68c45-1afe-ec11-82e5-002248233f99)" - However I'm getting and ambiguous error:

    {
    "error": "Error calling endpoint \"Microsoft Dynamics 365 - Create Opportunity\"",
    "endpoint": {
    "name": "Create Opportunity",
    "datasource": "Microsoft Dynamics 365"
    },
    "request": {
    "url": "https://dowuat.crm.dynamics.com/api/data/v9.0/opportunities",
    "method": "POST"
    },
    "response": {
    "status": 400,
    "body": {
    "code": "0x80040224",
    "message": "Object reference not set to an instance of an object."
    }
    },
    "external error": true
    }

    Any thoughts?

    ReplyDelete
  3. Hi Siddique, I'm using originatingleadid@odata.bind":"/leads(66a68c45-1afe-ec11-82e5-002248233f99)"
    {
    "error": "Error calling endpoint \"Microsoft Dynamics 365 - Create Opportunity\"",
    "endpoint": {
    "name": "Create Opportunity",
    "datasource": "Microsoft Dynamics 365"
    },
    "request": {
    "url": "https://uat.crm.dynamics.com/api/data/v9.0/opportunities",
    "method": "POST"
    },
    "response": {
    "status": 400,
    "body": {
    "code": "0x80040224",
    "message": "Object reference not set to an instance of an object."
    }
    },
    "external error": true
    }

    ReplyDelete