locked
Create Account Record with Lookup Field Value using Web API RRS feed

  • Question

  • I am creating an Opportunity using the new Web API REST endpoint and I have the following code to do so

    var entity = {};
    entity.name = "ABCD";
    entity.customerid = {
                Id: "CC7CC89D-3A4D-4BA1-9255-5DC2A92856DB",
                LogicalName: "account"
            };

    var req = new XMLHttpRequest();
    req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v8.0/opportunities", 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 {
                alert(this.statusText);
            }
        }
    };
    req.send(JSON.stringify(entity));

    My problem is I can't include a lookup field in the create, every time I do so, I am getting an error.

    What is the correct format to include a lookup field in create?

    Thanks a million!!

    Monday, June 20, 2016 2:57 PM

Answers

  • Looks like you'll need to do something like this: 

    var entity = {};
    entity.name = "Test32432";
    entity["customerid_account@odata.bind"] = "/accounts(AAA19CDD-88DF-E311-B8E5-6C3BE5A8B200)"

    Looks like your using my CRM REST Builder tool - I'll need to update accordingly :)


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Monday, June 20, 2016 4:39 PM
    Moderator