Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

newEntityId (Id from new record created) returning null upon new record creation

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

Hi everyone, 

I am new to dynamics 365 and CRM in general and I have been trying to create a new record, using api request, which id I need to use as an input to another function. The problem is that my newEntityId returns null when creating my new record, despite being able to effectively create the new record (I've created the function through Rest Builder). Does anyone have a solution for this? It is problem easy to solve but I am also new to Javascript and I've been searching the internet bu till now without any luck

*This post is locked for comments

  • Suggested answer
    gdas Profile Picture
    gdas 50,089 on at
    RE: newEntityId (Id from new record created) returning null upon new record creation

    Hello,

    I a not sure which version of rest builder are you using  I would recommend download the rest builder from below -

    https://github.com/jlattimer/CRMRESTBuilder/releases

    and import the managed solution in your CRM instance.

    Here is the step I have followed. 

    Step1 : Select the attributes to pass while creating record.

    4405.webapi1.png

    Step 2 : Click on Create Request .

    4405.webapi1.png

    Step 3 Here is the id I am getting 

    webapi.png

    Now here is my Code , where I have declare a variable in the first line for entity id and passing this to outside and inside of the method.

    function ClickMe() {
    
        //local variable for newEntityId
        var CreatedEntityID = null;
    
        //Created from rest builder 
        var entity = {};
        entity.firstname = "Goutam";
        entity.lastname = "Das";
        entity.telephone1 = "8598989878";
    
    
        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];
                    CreatedEntityID = newEntityId;//Assign the value to the local variable
                    // Pass to other method 
                    CreateAnotherRecord(CreatedEntityID);
                } else {
                    Xrm.Utility.alertDialog(this.statusText);
                }
            }
        };
        req.send(JSON.stringify(entity));
    
        // Pass to other method 
        CreateAnotherRecord(CreatedEntityID);
    }


    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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,468 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,289 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans