Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

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

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,085 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

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,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans