web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Creating an contact record using the OData endpoint with JavaScript

(0) ShareShare
ReportReport
Posted on by 605

Hi all,

I am trying to create a contact record using the OData endpoint with JavaScript, here is my code: 

function CreateNewContact()
{
var contact= {};

contact.lastname= "Test JS Contact";

SDK.REST.createRecord(
contact,
"contact",
function (contact)
{
alert("Account Created with name" + " " + contact.lastname);
},
errorHandler
);

function errorHandler(error) {
writeMessage(error.message);
}
}

I guess this is not creating any contact record in Contact Entity. 

What might have gone wrong ?

Thanks.

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Yadnyesh Kuvalekar Profile Picture
    4,102 on at
    Hi Rahul,

    Try using below code:

    function CreateNewContact()
    {
     var contact= {};
    
    contact.LastName= "Test JS Contact";
    
    SDK.REST.createRecord(
     contact,
     "contact",
     function (contact) 
     {
     alert("Account Created with name" + " " + contact.lastname);
     },
     errorHandler
     );
    
    function errorHandler(error) {
     writeMessage(error.message);
     }
    }
    


    You should use Schema names while using SDK.REST. Please refer below sample code by MSDN.

    https://msdn.microsoft.com/en-us/library/gg334427(v=crm.7).aspx

  • Suggested answer
    Kumar Viju Profile Picture
    1,026 on at

    Hi Rahul,

    Since you have mentioned CRM 2016 , i would recommend start using Web API for creating ODATA query's.

    Microsoft is going to eventually depreciate 2011 endpoints.

    And the best tool which i use always for creating ODATA query is CRM Rest Builder. Amazing work by the team ( Jason Lattimer )

    https://crmrestbuilder.codeplex.com/ 

    It can construct your queries within seconds, and the possibility of error is negligible ( As we developer knows how frustrating it can be making silly mistake and finding the root cause for Javascript to fail). You can take control of making the query Sync or Async. 

    FYI. I don't think SDK.REST can make synchronous call.

    This is the sample Code I created in one go, using the tool.

    Learn More About Web API: https://msdn.microsoft.com/en-us/library/mt593051.aspx 

    ===

    WEB API

    var entity = {};
    entity.firstname = "Vijay";
    entity.lastname = "Kumar";

    var req = new XMLHttpRequest();
    req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v8.0/contacts", true);
    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.onreadystatechange = function () {
    if (this.readyState === 4) {
    req.onreadystatechange = null;
    if (this.status === 204) {
    var uri = this.getResponseHeader("OData-EntityId");
    var regExp = /\(([^)]+)\)/;
    var matches = regExp.exec(uri);
    var newEntityId = matches[1];
    }
    else {
    alert(this.statusText);
    }
    }
    };
    req.send(JSON.stringify(entity));

    =====

    If you still want use SDK Rest.

    var entity = {};
    entity.FirstName = "Vijay";
    entity.LastName = "Kumar";

    SDK.REST.createRecord(
    entity,
    "Contact",
    function (result) {
    var newEntityId = result.ContactId;
    },
    function (error) {
    alert(error.message);
    }
    );

     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans