Filtering across multiple Dynamics entities in Common Data Service - dynamics-crm

I'm trying to query our Dynamics 365 CRM through the Common Data Service / Microsoft Dataverse. I can manage this when I need to just filter on fields that exist within the entity type I'm searching. But I also need to filter on a different linked entity.
So far I can query on the contact entity just fine:
var contacts = svc.Get("contacts?" +
"$select=firstname,lastname,address1_name,address1_line1," +
"address1_postalcode,emailaddress1,statecode,_parentcustomerid_value&" +
$"$filter=contains(firstname,'{firstName}') and " +
$"contains(lastname, '{lastName}')"
, formattedValueHeaders);
And this returns a list of contacts which match the filter.
However, I also need to filter on the Account Name that the contact is linked to. If I had the AccountId value then I could do this by adding $"contains(_parentcustomerid_value, '{accountId}')" to the filter. But I can't use this as the input data I'm receiving to query on is the name of the account and not an Id value.
Any ideas on how to do this?
Currently, the only way I can see is to use my existing query, then to loop through each contact and check the returned account name in the response, and if this matches then in effect I've filtered it. But I would prefer to be able to have the whole filter all in a single query.

You can use this to filter based on lookup field name value.
https://devorg.crm.dynamics.com/api/data/v9.1/contacts?$filter=parentcustomerid_account/name eq 'test'

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

Fetch value of a field from one entity to second entity based on the lookup field selected in second entity using Fetch XML

I am new to CRM. I have a requirement where I have an "account" entity and a "contact" entity. In contact form I have a lookup field for searching account name which is stored in account entity. When I select an organization through lookup, the contact number of that organization should be fetched and saved in another field in contact form. I need to do it with fetch XML.
Option 1: You can do a no-code solution by mapping fields between account & contact.
Option 2: If you don't want a physical copy of field value in both entity, use Quick view form of Account lookup to just display the fields in contact without storing redundant data.
Option 3: If you still want to store the value onchange of account, retrieve it & set it in contact field. This step by step guide will help you to build fetchxml & use it in JS. Your question doesn't say if you are stuck in query or any error.

directory api - get users using orgName

I have four sub-organizations defined. (/AdminOrg, /subOrgA, /SubOrgB, /subOrgA/SubOrgAA)
my directory.orgunits.list query return below data. I store the return array in a variable called orgUnits[]:
http://pastebin.com/Kzud6SAq
I have 4 users in my organization. one in each sub-organization. the users.list return below data:
http://pastebin.com/6ttSgDSe
I am trying to get no. of users within an organization (without including sub organizations in them)
Option 1:
The query directory.users.list.query("orgUnitPath=/subOrgA") includes users from sub organization (/subOrgA/SubOrgAA) too and does not meet my needs.
Option 2:
I tried the query directory.users.list.query("orgName=orgUnits[]->name"). It queries for users[]->organizations[]->name. And this field is null by default. It does not get populated with orgUnits[]->name.
My questions are
How to populate users.organizations[].name?
Is there any way I can get users within an organization without including sub-organizations in them
Unfortunately it isn't possible to search for users within an orgUnit not including sub-orgs. The orginations field in the Users resource, as well as the orgName, orgTitle, orgDepartment, orgDescription,
orgCostCenter query parameters, refer to a completely separate set of data that appears to be used by the API only. Only the orgUnitPath field and query parameter operate on the organizations visible within the Google Apps Admin Console.

Resources