Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Service | Customer Service, Contact Center, Fie...
Unanswered

Unable to create record having lookup field using Web API

(0) ShareShare
ReportReport
Posted on by 90

I have an entity "Customer Asset" which has relation with another entity "External Project". The field of the external project is Lookup in the customer asset entity. When trying to create a new record in customer asset entity using CRM REST Builder. I got no response and the record is not created. But when i remove the external project from the added attributes the record is created successfully. i don't know where is the problem!

The API:

var entity = {};

entity.msdyn_name = "Asset test";
entity.new_assetcode = " 56587";
entity["new_externalproject@odata.bind"] = "/msdynce_externalprojects(xxx)";

var req = new XMLHttpRequest();
req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/msdyn_customerassets", 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));

  • Abbas Khan Profile Picture
    Abbas Khan 255 on at
    RE: Unable to create record having lookup field using Web API

    I would suggest running Synchronous and debug it and then you should be able dig into it.

  • CSafwat Profile Picture
    CSafwat 90 on at
    RE: Unable to create record having lookup field using Web API

    Yes, the GUID is correct.

  • ba365guy Profile Picture
    ba365guy 2,948 on at
    RE: Unable to create record having lookup field using Web API

    your Syntax seems ok. Is the GUID passed correct?

  • CSafwat Profile Picture
    CSafwat 90 on at
    RE: Unable to create record having lookup field using Web API

    Schema name of external project entity: "msdynce_externalproject"

    Schema name of look up field in asset entity: "new_externalproject"

  • ba365guy Profile Picture
    ba365guy 2,948 on at
    RE: Unable to create record having lookup field using Web API

    Can you mention the schema name of external project entity and schema name of look up field in asset entity here. 

    So that I can check and suggest something.

  • CSafwat Profile Picture
    CSafwat 90 on at
    RE: Unable to create record having lookup field using Web API

    I have tried the same scenario with another lookup field and it is working fine but i there is a problem with that field.

  • Abbas Khan Profile Picture
    Abbas Khan 255 on at
    RE: Unable to create record having lookup field using Web API

    Have you tried debugging it and see what data is being passed?

    I have tested similar scenario and created a task on contact entity which seems to work fine. Make sure you are removing brackets at the start and end of the guid

    Sample is below

          var contId = myContactGuid.replace("{", "");

                   contId = contId.replace("}", "");

                   var newTask = {};

                   newTask["subject"] = document.getElementById("subject").value;

                   newTask["scheduledend"] = document.getElementById("followup").value;

                   newTask["regardingobjectid_contact@odata.bind"] = "/contacts(" + contId + ")";  //set look up ID here

                   var req = new XMLHttpRequest();

                   req.open("POST", serverURL + "/api/data/v8.0/newTasks", true);

                   req.setRequestHeader("Accept", "application/json");

                   req.setRequestHeader("Content-Type", "application/json; charset=utf-8");

                   req.setRequestHeader("OData-MaxVersion", "4.0");

                   req.setRequestHeader("OData-Version", "4.0");

                   req.onreadystatechange = function () {

                       if (this.readyState == 4 /* complete */) {

                           req.onreadystatechange = null;

                           if (this.status == 204) {

                               //something here if needed

                           } else {

                               //something here if needed

                           }

                       }

                   };

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

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

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

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 231,436 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans