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)

CRM 2016 Web API create contacts receiving 500 error

(0) ShareShare
ReportReport
Posted on by

I am new to CRM 2016 Web API.

I am trying to create a new contact record, but got 

“The remote server returned an error: (500) Internal Server Error.”

Hard to debug without detailed error.

Following is the Json I am sending out:

{
"contactid":"2ce32e4d-29a0-424a-96c6-3a0359276612",
"parentcustomerid_account@odata.bind":"\/accounts(78D3E1AD-CF14-48D3-8E8D-1178C52B510C)",
"firstname":"TestFirstName",
"lastname":"TestLastName",
"jobtitle":"Software Design Engineer",
"address1_city":"Redmond",
"address1_country":"USA",
"address1_postalcode":"98052",
"address1_stateorprovince":"WA",
"telephone1":"425-123-4567",
"emailaddress1":"test@APITest.com",
"statecode":0
}

Anyone knows what is wrong?

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    Since you are creating a new contact, why do you have the contact id in the Json? Do you need it?

    Take a look at the code below to create new contact using Web Api:

    var entity = {};

    entity["parentcustomerid_account@odata.bind"] = "/accounts(12345678-1234-1234-1234-123456780124)";

    entity.lastname = "TestLast";

    entity.firstname = "TestFirst";

    entity.emailaddress1 = "johndoe@testmail.com";

    var req = new XMLHttpRequest();

    req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/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 {

               Xrm.Utility.alertDialog(this.statusText);

           }

       }

    };

    req.send(JSON.stringify(entity));

    You can use CRM Rest Builder to generate code as above:

    github.com/.../CRMRESTBuilder

    Hope this helps.

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi ,

    As Aric mentioned at the time of contact creation, you don't need to provide contacted and status as those  will create automatically.

    Secondly you are passing some look up value like city, country, parent customer id, you need to pass those using odata.bind.

    I would suggest remove extra attributes contacted,statuscode,parent customer id ,city and country and pass only required field and check.Once it's success then you can easily get which attribute is having problem.

    Hope this helps.

  • Community Member Profile Picture
    on at

    Even using your code get me 500 error. There must be something wrong with this CRM Server.

  • Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    Try downloading CRM Rest Builder (link above) and see if you can run any webapi statement against your CRM Server.

    This will help you know if there are actually issues with the CRM Server.

  • Community Member Profile Picture
    on at

    Tried to just set firstName lastName, still got the 500 error. Is there a way to get the detailed error? 500 is too generic to debug.

  • Community Member Profile Picture
    on at

    I can create other entities like Account, Task with no issue.

    But some other entities like Contacts, I am receiving 500 error, which is too generic to debug.

    Got stuck for 2 days...

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    I am not sure what is the issue just assuming could please download the data import template, you will get the minimum required field from there and try once .

  • Community Member Profile Picture
    on at

    Some suggestions:


    1:
    ________
    Make sure you remove the CURLY BRACKETS on the ID.

    //Something like:
    var objectId = Xrm.Page.data.entity.getId().replace('{', '').replace('}', '');

    2:
    ________

    I'm not sure about this one but maybe use:

    THIS:

    var note = Object();
    note["notetext"] = "Test text"
    note["subject"] = "Test subject";
    note["filename"] = "filename.txt";
    
    


    INSTEAD OF:

    var note = {};
    note.notetext = "Test text"
    note.subject = "Test subject";
    note.filename = "filename.txt";
    
    


    3:

    Check for errors in the developer tools console.

    4:

    You might have a try catch handler or error callback handler that could give you more information.

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