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)

WEBAPI Syntax Confirmation Needed for EntityReference

(0) ShareShare
ReportReport
Posted on by 959

Hi Everyone

I've started experimenting with WEBAPI to update CRM entities. So far I've been able to create new records without issue.

But I've now hit a snag when it comes to changing an entity lookup field from one value to another. We are using the Newtonsoft.Json class to build our object. Our instruction set looks like this.

_crmEntityJSON = new JObject();
_crmEntityJSON.Add("jp_contactmanagerid@odata.bind", "/contacts(7E6B14FC-A08B-E611-80C2-00155D342408)");

I've tried many differing variations on this, without any success. I suspect I'm building this instruction incorrectly, but haven't found an example to compare with. Does anybody have a simple update entity reference method to illustrate the correct syntax?

Steve

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    ansrikanth Profile Picture
    3,115 on at

    Hi

    Not sure about NewtonsoftJson, but I have done almost the same thing many times in my JSON object and it worked perfectly well. This is how I used ...

    MyEntity["new_myfield@odata.bind"] = "/contacts(" + contactid + ")"; 

    I don't think, newtonsoftJson have some thing special to be added to work for Webapi. Can you tell what is the error you are getting? May be that helps in debugging the issue.

    -Srikanth

  • Steve Le Monnier Profile Picture
    959 on at

    Hi Srikanth

    Thanks for the info. It must be the 'Newtonsoft' I'm obviously not using it correctly, but good clear examples are hard to come by.

    When I output the JObject it doesn't look that much different to your hand crafted version:

    {

     "new_contactmanagerid@odata.bind": "/contacts(7E6B14FC-A08B-E611-80C2-00155D342408)"

    }

       base: {

     "new_contactmanagerid@odata.bind": "/contacts(7E6B14FC-A08B-E611-80C2-00155D342408)"

    }

       Type: Object

    The error I get back is a bad request, with this in the exception body:

    "An undeclared property 'new_contactmanagerid' 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."

    Its a bit of a mouthful, but seems to hint that something wasn't included?

  • Verified answer
    Steve Le Monnier Profile Picture
    959 on at

    Well. I finally got this to work, but the solution left me frustrated at the amount of time it took given the simplicity of what I was trying to do.

    Basically it came down to the case of the field names. For traditional fields they used the name (lowercase), but lookups need to be schema (proper-case). An inconsistent standard that shouldn't be.

    The second problem I had having got passed the naming issues was to update an entity and remove its lookup value. This is normally done by setting the field to NULL. However, with the Web API we couldn't get this to work in the update operation.

    Not wanting to make multiple calls to the platform I eventually decided to set the lookup field to GUID.EMPTY and then used my existing plugin validation event to spot it and set it to NULL. A bit of a fudge but at least its a working solution.

    Thanks for your time, and for newbies to web api "assume nothing... test everything".

  • Suggested answer
    ansrikanth Profile Picture
    3,115 on at

    Glad that you able to figure it out.. Totally agree being fields used some times as camelcase and some times lowercase.. they have to sort it out..

    Coming to clearing a particular field value using Webapi, you have to use DELETE Request.  Check this link..

    https://msdn.microsoft.com/en-in/library/mt607664.aspx?f=255&MSPPError=-2147217396#bkmk_deleteSingleProperty

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