Facing error when I make a POST request to create entity in Dynamics CRM from Postman? - dynamics-crm

I am having trouble making a successful HTTPS Post Request to Dynamics 365 CRM customer service.
I am able to successfully place GET requests but facing issue while creating new account/contact/incident.
Request body:
{
"name": "MS account test",
"customerid_contact#odata.bind": "/contacts(904d1761-b522-ed11-9db2-000d3af06307)"
}
Please help!!
I get the following error: "An undeclared property 'customerid' 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."

You are trying to bind contact Record.
For Account Entity, when you are trying to add contact as lookup, field name should be primarycontactid.
For incident entity it will be below 3 field
primarycontactid#odata // Lookup
msa_partnercontactid // Lookup
responsiblecontactid // Lookup
for Contact it will be
parent_contactid

for relationships you need to use the navigation property instead of the lookup name (they can be equal but is not always the case).
as you mentioned 3 different tables (account, contact, incident) you can create the request using my tool Dataverse REST Builder, inside the generated code you can see the name or you can export the collection to Postman to see the raw request.

Related

Microsoft Dynamics API - Getting Fields from Entity with Navigation

I am trying to get the field list from an entity, in example contact, I have successfully done it doing a Request to:
EntityDefinitions(LogicalName='contact')/Attributes/Microsoft.Dynamics.CRM.AttributeMetadata
But now I need to get the Navigation Properties defined on this entity, I have found information on how to get Navigation Properties when querying a specific record by id, but in this case I need it when getting the field list.
Thanks in advance for your help
Referring the docs: https://learn.microsoft.com/en-us/powerapps/developer/data-platform/webapi/query-metadata-web-api
You already have the Lookup attributes, AttributeMetadata has everything.
If you want to only read the metadata of lookups, change the type to LookupAttributeMetadata.
EntityDefinitions(LogicalName='contact')/Attributes/Microsoft.Dynamics.CRM.LookupAttributeMetadata
To obtain relationships info alongside the columns list then an additional query is required, RelationshipDefinitions:
/RelationshipDefinitions/Microsoft.Dynamics.CRM.OneToManyRelationshipMetadata
/RelationshipDefinitions/Microsoft.Dynamics.CRM.ManyToManyRelationshipMetadata
filter on ReferencedEntity / ReferencingEntity to limit the results
/RelationshipDefinitions/Microsoft.Dynamics.CRM.ManyToManyRelationshipMetadata
filter on Entity1LogicalName / Entity2LogicalName / IntersectEntityName

How to retrieve field values and its translations from Microsoft dynamics CRM using REST API

I need to retrieve the entities from my CRM site and all the fields associated with that entity. Need to get the translated values as well.
Please provide some queries that will be helpful in this case
I tried with below queries, but this could not fetch all the values.
..../api/data/v9.1/EntityDefinitions(LogicalName='account')/Attributes/Microsoft.Dynamics.CRM.PicklistAttributeMetadata?$select=LogicalName&$expand=OptionSet,GlobalOptionSet
..../api/data/v9.1/EntityDefinitions(LogicalName='account')/Attributes
..../api/data/v9.1/GlobalOptionSetDefinitions
You can retrieve CRM entities by calling:
[Organization URI]/api/data/v9.1/
In order to get an entity's attributes you can call:
[Organization URI]/api/data/v9.1/EntityDefinitions(LogicalName='[Entity name (ex: account)]')/Attributes
For attribute translation, navigate to 'DisplayName' you will find 'LocalizedLabels'.
For more information you can check this url:
https://learn.microsoft.com/en-us/powerapps/developer/common-data-service/webapi/query-metadata-web-api

How to populate look up field based on text field - Dynamics 365

I am trying to populate contact field (Potential Customer) based on text field (Email Address) set. To bear in mind, it should be unique based on the customer record.
Condition is if Email address field is set, look up field (Potential Customer) value should be fetched based on the Email address field.
Wherever you are trying to fill that contact lookup from (plugin, form script, console job, web app, ETL, MS Flow, etc), you have to fetch the contact by filtering the email value and set it in lookup entity reference.
You can use web api or fetchxml to achieve that. Here is how it should look like.
https://crmdev.crm.dynamics.com/api/data/v9.0/contacts?$select=fullname&$filter=emailaddress1 eq 'test#gmail.com'
C# example
Javascript example
Once you fetch the lookup field based on email address using web api which is suggested by Arun. To set lookup field on form using javascript see below article.
https://xrmdynamicscrm.wordpress.com/2020/06/17/dynamics-crm-365-set-lookup-regarding-field-while-using-xrm-navigation-openform-formparameters/
Please mark my answer verified if i were helpful

How to create a contact record with customer field set as contact or account

I am trying to create a contact record with its customer set to Account/Contact where customer records are pre-existing.
Here is the request that I am trying.
POST <service>/api/data/v8.0/contacts
{
'lastname':’contact1’,
'parentcustomerid_contact': '/contacts(f797401d-64c9-e711-8125-000d3a22f66f)'
}
I also tried providing full url of the contact record, but that is also not working. Appreciate any help with correct query or any document that I shall go through to get this scenario working.
You have to use like this parentcustomerid_contact#odata.bind to bind single valued navigation properties.
you must use the #odata.bind annotation to set the value of a single-valued navigation property.
Read more

WebAPI - odata service adding ForeignKey

i am building my the model using ODataModelBuilder, i am trying to create navigation property however in the metadata i dont see any foreginkey indication, in my solution i am not using EF, so there is no foreignKey attribute, is it possible to add it by code?
As you clarified in your comment, the reason you want to add foreign key information is because your client application is not including related entities when you query the main entity. I don't think foreign keys are the problem here.
As an example, I'll use two entity types: Customer and Order. Every Customer has some number of associated Orders, so I have a navigation property on Customer called Orders that points to a collection of Orders. If I issue a GET request to /MyService.svc/Customers(1), the server will respond with all of the Customer's information as well as URLs that point to the related Order entities*. I won't, by default, get the data of each related Order within the same payload.
If you want a request to Customers(1) to include all of the data of its associated Orders, you would add the $expand query option to the request URI: /MyService.svc/Customers(1)?$expand=Orders. Using the WCF Data Services client (DataServiceContext), you can do this with .Expand():
DataServiceQuery<Customer> query = context.Customers.Expand("Orders");
However, WebAPI OData doesn't currently support $expand (the latest nightly builds do though, so this will change soon).
The other approach would be to make a separate request to fill in the missing Order data. You can use the LoadProperty() method to do this:
context.LoadProperty(customer, "Orders");
The LoadProperty approach should work with WebAPI as it stands today.
I know this doesn't answer your original question, but I hope addresses your intent.
*In JSON, which is the default format for WebAPI OData services, no links will show up on the wire, but they are still there "in spirit". The client is expected to be able to compute them on its own, which the WCF Data Services Client does.

Resources