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

Announcements

No record found.

News and Announcements icon
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

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

I have the same question (0)
  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans