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

Notifications

Announcements

No record found.

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)
  • David Jennaway Profile Picture
    14,065 on at

    Do you get a useful error ?

    And just to clarify:

    • Is this a lookup attribute, or an option set ?
    • Can you update other attributes of the same type (lookup or option set) with this code, or just attributes of different types
  • _Lars_ Profile Picture
    225 on at

    The error is: The property 'tuv_optionsetalternative' does not exist on type 'Microsoft.Dynamics.CRM.tuv_servicetask'

    So whats going on here, it cant find the field tuv_optionsetalternative in the entity? I'm looking at it, its there :-/

    The above code is able to update all fields other then Lookup. It Updates Option Set, whole number, single line of text. Just everything besides Lookup.

  • Suggested answer
    Andreas Cieslik Profile Picture
    9,267 on at

    Have you published the customization of your custom entity?

    Then I would check if you have a typo in your attribute's name. Secondly, please check if the casing of that attribute's internal name is correct.

  • _Lars_ Profile Picture
    225 on at

    Hi Andreas!

    I did a "Publish All Customizations" to be sure, but ALL the lookup fields are not listed when I list the entities fields. However, I got this list item: "_tuv_optionsetalternative_value" (all the lookups are with "_" at the start of the name) but trying to set this with the API results in an error stating I am not allowed to set this directly.

  • Suggested answer
    Andreas Cieslik Profile Picture
    9,267 on at

    This might help:

    parthimscrm.wordpress.com/.../retrieve-lookup-option-set-value-from-web-api-mscrm

    also I recommend to use CRM Rest Builder to generate Web API requests:

    github.com/.../CRMRESTBuilder

  • Suggested answer
    Community Member Profile Picture
    on at

    Hi,

    For lookup type attributes you need to give the guid of lookup record along with that related entity's schema name. So selectedValue should be object like this -

    var selectedValue=new Object();

     selectedValue.Id=guid;

     selectedValue.LogicalName="contact";

    Secondly for the attribute not found error, I think you need to give schema name of the attribute instead of name which could be 'tuv_OptionSetAlternative'. Mark the capitals.

    Hope this helps

  • _Lars_ Profile Picture
    225 on at

    Hello Sanchita, where goes the actual new value if id is the id of the lookup, the LogicalName is the name of the lookup (kinda redundant?)?

  • Suggested answer
    Andreas Cieslik Profile Picture
    9,267 on at
  • _Lars_ Profile Picture
    225 on at

    I tried this 

                    var json = 
                    {
                        "tuv_optionsetalternativeid@odata.bind": "/tuv_optionsetalternatives("+ selectedValue +")"
                    };
    
                    PatchEntity(id, "tuv_servicetasks", json);

    And got this error:

    An undeclared property 'tuv_optionsetalternativeid' which only has property annotations in the payload but no property value was found in the payload. In OData, only declared navigation properties and declared named streams can be represented as properties without values.

  • Suggested answer
    Andreas Cieslik Profile Picture
    9,267 on at

    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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans