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

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How can I patch a lookup field with the API?

(0) ShareShare
ReportReport
Posted on by 225

I've worked thru a form for all kinds of input and right now I'm struggling a bit to set (or rather PATCH) the value holding the selected value of the option set. Now, for all other types the following code works great (for its field):

                var json = 
                {
                    "tuv_optionsetalternative": selectedValue
                };


                PatchEntity(id, "tuv_servicetasks", json);

Here I'm making json where i set the tuv_optionsetalternative to the selected value with the function PatchEntity. The selectedValue is the GUID of the selected lookup value. Its looks like this:

        function PatchEntity(id, entityName, json) {

            var req = new XMLHttpRequest();
            req.open("PATCH", encodeURI(Xrm.Page.context.getClientUrl() + "/api/data/v8.1/" + entityName + "("+ id +")"), true);
            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) 
                {        
                    req.onreadystatechange = null;
                
                    if (this.status == 204) {
                        Debug("Successfully updated entity.");
                        Debug(json);
                    } else {
                        Debug("Could not patch entity.")
                        Debug(json);
                    }
                }
            } 

            req.send(JSON.stringify(json));
        }
This does not work. I get "Could not patch entity." for patching this lookup only, all the others are fine.

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at
    RE: How can I patch a lookup field with the API?

    Hi Lars,

    As also suggested by other, the best option for you is to use the CRM Rest builder tool. This tool will take care of the schema name and will provide you the exact script to be used. You can also use this tool to execute the request on demand. It is very simple to use and reliable.

    https://github.com/jlattimer/CRMRESTBuilder

    rest2.png

    rest2.png

    Hope this helps

  • Suggested answer
    Andreas Cieslik Profile Picture
    9,267 on at
    RE: How can I patch a lookup field with the API?

    or try your original statement like this:

    JSONOBJECT["tuv_optionset@odata.bind"] = "/tuv_optionset(DFE54660-37CD-E511-80DE-6C3BE5A831DC"

  • Suggested answer
    Andreas Cieslik Profile Picture
    9,267 on at
    RE: How can I patch a lookup field with the API?

    I guess you need to do it as explained here:

    msdn.microsoft.com/.../mt607875.aspx

    PATCH [Organization URI]/api/data/v8.2/opportunities(00000000-0000-0000-0000-000000000001) HTTP/1.1

    Content-Type: application/json

    Accept: application/json

    OData-MaxVersion: 4.0

    OData-Version: 4.0

    {

    "customerid_account@odata.bind":"[Organization URI]/api/data/v8.2/accounts(00000000-0000-0000-0000-000000000002)"

    }

  • Suggested answer
    Andreas Cieslik Profile Picture
    9,267 on at
    RE: How can I patch a lookup field with the API?

    Hi Lars,

    getting data is different than setting data.

    Also please read on this article:

    msdn.microsoft.com/.../gg334767.aspx

    if you use this header statement:

    Prefer: odata.include-annotations="*"

    you will get _tuv_optionset _value and _tuv_optionsetalternative_value

  • _Lars_ Profile Picture
    225 on at
    RE: How can I patch a lookup field with the API?

    I'm looking at the "Fields" part of the entity in the solution. We have two fields:

    - tuv_optionset (lookup)

    - tuv_optionsetalternative (lookup)

    These along with other fields like tuv_name (single line), tuv_value_decimal (decimal number) and such.

    So by using javascript and GETting the entity and just output it something strange happens; the "normal" types like single line, decimal and other are outputted as expexted: tuv_name, tuv_value_decimal. But not the lookups. They are excluded, instead I get this other variables holding the expected values; _tuv_optionset _value and _tuv_optionsetalternative_value. While these are readable in the API I am not able to set them.

    I think this is the root of the problem why I'm having some problems setting the values with the error messages (I't cant find tuv_optionsetalternative). tuv_optionsetalternative is the selected value of the tuv_optionset list. tuv_optionset is a list of alternatives where tuv_optionsetalternative is the user selected list element.

    Does anyone get what the problem is. Why isn't the lookup exposed the same way as the other variables? Is this a configuration error?

  • Andreas Cieslik Profile Picture
    9,267 on at
    RE: How can I patch a lookup field with the API?

    I just downloaded odata metadata from developer resources link. Pawel is correct, it should be all lower case.

  • Suggested answer
    tw0sh3ds Profile Picture
    5,600 on at
    RE: How can I patch a lookup field with the API?

    You said that your field is called "tuv_optionsetalternative", so why are you using "tuv_optionsetalternativeid"? If it's true that the name of the field is "tuv_optionsetalternative", then proper JSON should be

    var json = 
          {
               "tuv_optionsetalternative@odata.bind": "/tuv_optionsetalternatives("+ selectedValue +")"
          };

    And I'm assuming that selectedValue is GUID without any braces etc.

    Also, if you have problems with building WebApi requests, use CRMRest builder from Jason Lattimer

    It's not true what Andreas is suggesting that you have to use SchemaNames - that's outdated answer, you can use logical names (all lowercase)

  • Suggested answer
    Andreas Cieslik Profile Picture
    9,267 on at
    RE: How can I patch a lookup field with the API?

    When you open Settings -> Customizations -> Customize the System

    open entities and your custom entity and click on the fields Navigation entry.

    now you should have an overview of your field similar to this Picture:

    fln.PNG

    As you can see there is a column Schema Name.

    You need to use this name for you JavaScript JSON object.

  • _Lars_ Profile Picture
    225 on at
    RE: How can I patch a lookup field with the API?

    I'm pretty new to Javascript and Dynamics. What part is the schema name? The one with the ending "id" og "s"? Its a bit confusing, I dont quite understand the syntax of the json string with its parts.

    And again, while using API to GET all the fields, there is no tuv_optionsetalternative (or with id), only _tuv_optionsetalternative_value which is read only it seams.

  • Suggested answer
    Andreas Cieslik Profile Picture
    9,267 on at
    RE: How can I patch a lookup field with the API?

    Can you make sure that you have provided the right schemaname of the lookup field?

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#2
ED-30091530-0 Profile Picture

ED-30091530-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans