Example of WebAPI Upsert function with Alternate Key (Patch Request)
Example of Upsert with Alternate Key
Just thought of sharing one of my learnings when I was working with WebAPI integration work in which we were trying to sync the leads to MS Dynamics CRM from another source. The challenge was that we wanted to use a composite Alternate key which has multiple fields (string and option-set) dataset values.
Note: Xrm.WebAPI. methods don't support patch requests for alternate keys.
Below is the example in which I have passed an alternate composite key in the Patch request and the values we are trying to update in the lead entity object.
To Create an alternate key in Dynamics CRM you can follow the below steps:
- Go to https://make.powerapps.com > Envrionment> Your Solution >Entity > Keys
- Select New key
- On the form, fill in the required fields (Display Name and Name) and then select and add the fields to the key.
- Save the key.
I'll not go in deep regarding alternate key concepts. In our case alternate Composite Key is: new_producttype+emailaddress1+firstname+lastname+mobilephone
Note: Just make sure while patch request alternate key values should match with entity object field values. It should be same otherwise it will not work
Please leave your valuable feedback!!
Comments
-
Example of WebAPI Upsert function with Alternate Key (Patch Request)For more information how to use alternate key with patch request kindly refer https://learn.microsoft.com/en-us/power-apps/developer/data-platform/use-alternate-key-reference-record?tabs=webapi
-
Hi @Varun I have an example where I also need to use a composite alternate key but in a slight different scenario: I have two custom entities : country (with code and description fields) /* with alternate key for the name that corresponds in fact to the country code */ countryState (with code and description fields and + countrycode) - because country state codes might repeat for different countries /* with alternate key for the name that corresponds in fact to the country code */ in the account entity I have lookups for these two fileds and the country I can pass it by "alssc_CountryAcme@odata.bind": "/alssc_countryacmes(alssc_name='US')" - and this works fine. for country state however I have to pass both parameters "alssc_ProvinceAcme@odata.bind": "/alssc_provinceacmes(alssc_name='AK', alssc_country='234')" This responds with error: "message": "The number of keys specified in the URI does not match number of key properties for the resource 'Microsoft.Dynamics.CRM.alssc_ProvinceAcme'.", Can you suggest how it should be done in this case ? Thanks in advance, Manuel
*This post is locked for comments