Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Set Lookup using Web API on record create

Posted on by 90

I have two entities: Exam Schedule and Exam. I am using Web API to create Exam record upon Exam Schedule field update. I am using following code:

var serverURL = Xrm.Page.context.getClientUrl();
var req = new XMLHttpRequest();
req.open("POST", encodeURI(serverURL + "/api/data/v8.0/new_examinations"), 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()
{
var accountUri = this.getResponseHeader("OData-EntityId");
};
var body=JSON.stringify({
"new_name": name,
});
req.send(body);

I have to set a lookup field on Exam. How can I set the lookup field?

*This post is locked for comments

  • Verified answer
    Nithya Gopinath Profile Picture
    Nithya Gopinath 17,074 on at
    RE: Set Lookup using Web API on record create

    Hi Chethan Lothari,

    Try the code below.

    var serverURL = Xrm.Page.context.getClientUrl(); 
    var req = new XMLHttpRequest(); 
    req.open("POST", encodeURI(serverURL + "/api/data/v8.0/new_examinations"), 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()
    { 
    var accountUri = this.getResponseHeader("OData-EntityId"); 
    };
    var body=JSON.stringify({
    "new_name": name,
    "primarycontactid@odata.bind":"/contacts(00000000-0000-0000-0000-000000000001)" //Setting Parent Contact lookup for example. Chnage your lookup id accordingly.
    });
    req.send(body);

    See: https://stackoverflow.com/questions/36700796/set-crm-lookup-values-with-webapi

    Hope this helps.

  • Suggested answer
    ram r Profile Picture
    ram r on at
    RE: Set Lookup using Web API on record create

    Example for setting an incident lookup is as follows

    "new_PrimaryIncident@odata.bind":"/incidents(10000000-0000-0000-0000-000000000000)"


    Refer: community.dynamics.com/.../know-how-set-a-lookup-value-on-create-update-of-a-record-using-mscrm-webapi-an-undeclared-property-error

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Set Lookup using Web API on record create

    Refer this:

    www.inogic.com/.../set-values-of-all-data-types-using-web-api-in-dynamics-crm-through-c

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,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans