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)

Unable to Update Ownerid field of custom entity using odata Json call

(1) ShareShare
ReportReport
Posted on by

Hi,

I'm facing issue while updating ownerid field of an entity with team,using odata json call,

my code is as follows:

public async Task TestUpdateUsingJson()

{

var complaintcasedata = new new_complaintcase();

complaintcasedata = GetComplaintCaseStub();

var recordid = complaintcasedata.new_complaintcaseid;

JObject jsondata = new JObject();

jsondata["ownerid@odata.bind"] = "/teams(" + complaintcasedata.ownerid + ")";

var ComplaintCaseResult = await serviceRequestProcessor.Update(jsondata,recordid, "new_complaintcases");

}

in above code new_complaintcase is my custom entity,in which I'm trying to update ownerid with teams

GetComplaintCaseStub() method in above method has dummy data as follows:

private static new_complaintcase GetComplaintCaseStub()

{

var complaintCase = new new_complaintcase();

complaintCase.new_aduserguid = "8604b472-edff-48d2-b0e6-dc83f7e241a6";

complaintCase.new_businessareaid = "Customer Relations";

complaintCase.new_casestatus = 100000000;

complaintCase.new_complaintagainst = 100000000;

complaintCase.new_complaintcaseid = Guid.Parse("3E482AFB-2190-4D4D-8383-6E0D13697E3C");

complaintCase.new_complaintdate = DateTime.Now;

complaintCase.new_complaintraisedbytypes = "Customer";

complaintCase.new_customernames = "Mrs N Qiehsaw, Mr M Ogcfqyu";

complaintCase.new_departmentraisedby = "IT";

complaintCase.new_originalcomplaintdate = DateTime.Now;

complaintCase.new_outstandingtask = "";

complaintCase.new_primaryaccount = "10003225";

complaintCase.new_primaryelementwithsubcategoryandcategory = "Redemption, Fees incorrectly applied";

complaintCase.new_primaryloanaccount = new new_loan { new_loanid = Guid.Parse("96771835-9162-434B-B3D1-0B88B33DB379"), new_name = "10003225" };

complaintCase.new_requestmethod = 100000001;

complaintCase.new_rootcause = 99999999;

complaintCase.new_sendcorrespondence = true;

complaintCase.new_summary = "test";

complaintCase.new_teamownerreason = "test";

complaintCase.new_type = 100000000;

complaintCase.new_username = "ABC";

 

complaintCase.ownerid = new team { name = "Customer Relations", teamid =Guid.Parse("37f8b9a4-e7b9-e511-80fb-3863bb353f28"),businessunitid=Guid.Parse("c9635b80-e4b9-e511-80f9-3863bb34e990") };

 

return complaintCase;

}

and below is my odata update call

public async Task<string> Update(JObject entityTobeUpdated, Guid recordid, string clientURL)

{

 

var recordId = recordid.ToString();

try

{

//Create HttpClient object to send and receive Http Requests and Response

var httpClient = await _connectionManager.CreateHttpClient();

using (httpClient)

{

var jsonData = JsonConvert.SerializeObject(entityTobeUpdated);

HttpRequestMessage request = new HttpRequestMessage(new HttpMethod("PATCH"), new Uri(httpClient.BaseAddress + clientURL)) { Content = new StringContent(jsonData, System.Text.Encoding.UTF8, "application/json") };

 

var response = await httpClient.SendAsync(request);

//If the response is Successfully executed then it will return the value true

if (response.IsSuccessStatusCode)

{

var recordUrl = response.Headers.GetValues("OData-EntityId").FirstOrDefault();

var splitRetrievedData = recordUrl.Split('[', '(', ')', ']');

recordId = splitRetrievedData[1];

}

}

}

catch (Exception exception)

{

throw new CustomODataException(exception.Message, exception);

}

return recordId;

}

I'm getting error that "method is not allowed",so I'm unable to update my owner id with team...please help me where I'm going wrong!


*This post is locked for comments

I have the same question (0)
  • JohnAnonymous Profile Picture
    5,241 on at

    I'm missing the url you create to do the patch request. The error seems to point to a mismatch of the URL and the method (PATCH).

    Maybe try postman or advanced rest client to test your call. Makes testing the api much easier.

  • Community Member Profile Picture
    on at

    HI, thanks for reply, I changed my PATCH call,and tried to update other field with the PATCH call, and this field get updated properly, but I'm unable to update ownerid.

    My Patch call is as below, it works properly for other field but not for Ownerid:

    public async Task<string> Update(JObject entityTobeUpdated, Guid recordid, string clientURL)

           {

               var recordId = recordid.ToString();

               try

               {

                   //Create HttpClient object to send and receive Http Requests and Response

                   var httpClient = await _connectionManager.CreateHttpClient();

                   using (httpClient)

                   {

                       var jsonData = JsonConvert.SerializeObject(entityTobeUpdated);

                       HttpRequestMessage request = new HttpRequestMessage(new HttpMethod("PATCH"), new Uri(httpClient.BaseAddress + clientURL+"("+recordId+")")) { Content = new StringContent(jsonData, System.Text.Encoding.UTF8, "application/json") };

                       var response = await httpClient.SendAsync(request);

                       //If the response is Successfully executed then it will return the value true

                       if (response.IsSuccessStatusCode)

                       {

                           var recordUrl = response.Headers.GetValues("OData-EntityId").FirstOrDefault();

                           var splitRetrievedData = recordUrl.Split('[', '(', ')', ']');

                           recordId = splitRetrievedData[1];

                       }

                   }

               }

               catch (Exception exception)

               {

                   throw new CustomODataException(exception.Message, exception);

               }

               return recordId;

           }

  • Community Member Profile Picture
    on at

    while updating ownerid am getting "Bad request" error

  • Suggested answer
    Nithya Gopinath Profile Picture
    17,078 on at

    Hi Shweta,

    Solution for the error bad request is discussed in the following thread.

    community.dynamics.com/.../149096

    Also, please refer the code in the following blog to update the owner lookup field.

    guruprasadcrm.blogspot.in/.../update-optionset-lookup-data-using.html

  • Community Member Profile Picture
    on at

    Hi,I'm using below json call to update OwnerId field.where ownerId is of type Team

    complaintCase.ownerid = new team { name = "Customer Relations", teamid = Guid.Parse("9072B3AE-F0CB-E511-810D-3863BB347D90") };

    jsondata["_ownerid_value@odata.bind"] = "/teams(" + complaintcasedata.ownerid + ")";

  • Community Member Profile Picture
    on at

    but this line of code did not update ownerid,gives error bad Request, for other fields it works fine.

  • Community Member Profile Picture
    on at

    Was there ever a solution found for this problem?  I'm running into the same issue trying to update the Owner of a custom entity record.  

  • Suggested answer
    Community Member Profile Picture
    on at

    After further investigation and digging through the full $metdata information of the API, the PATCH binding code from javascript should look like this :

    entity = { "ownerid@odata.bind" : "/systemusers(" + userId + ")" }

    I was trying everything but within the $metadata information for my custom entity, look to the "Partner" property, that's the name you should be using as the field name to bind to.  

  • MohammadBB Profile Picture
    325 on at

    The same thing for team would be like this:

    entity["ownerid@odata.bind"] = "/teams(" + teamId + ")"

  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    Try to replace line

    jsondata["ownerid@odata.bind"] = "/teams(" + complaintcasedata.ownerid + ")";

    with line

    jsondata["ownerid@odata.bind"] = "/teams(" + ((team)complaintcasedata.ownerid).teamid + ")";

    ownerid property is not string - it's an object of team type.

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