Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

CRM 2016 Web API create contacts receiving 500 error

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

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

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: CRM 2016 Web API create contacts receiving 500 error

    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.

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: CRM 2016 Web API create contacts receiving 500 error

    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
    Community Member Microsoft Employee on at
    RE: CRM 2016 Web API create contacts receiving 500 error

    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...

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: CRM 2016 Web API create contacts receiving 500 error

    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.

  • Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: CRM 2016 Web API create contacts receiving 500 error

    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
    Community Member Microsoft Employee on at
    RE: CRM 2016 Web API create contacts receiving 500 error

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

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: CRM 2016 Web API create contacts receiving 500 error

    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.

  • Suggested answer
    Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: CRM 2016 Web API create contacts receiving 500 error

    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.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans