Retrieve all OptionSet values using OData in Dynamics CRM - dynamics-crm

I am quite new to Dynamics CRM. I am building an app which should update entity in Dynamics CRM. I can update simple types without any issues. Now the situation is, I have declared some custom Option Sets in Contact entity.
Is there any way to retrieve all the possible OptionSet values (text and value) so that my app can look for appropriate value and set it in the payload it is generating?
I can not find any endpoint in WebAPI as well as XRMServices/2011/OrganizationData.svc. Any help would be really awesome.

You can use either the Web API or Organisation Service to retrieve The metadata and data models in Microsoft Dynamics CRM. Check out the sub articles of that one for specific examples and details.
Web API example Querying EntityMetadata attributes.
The following query will return only the PicklistAttributeMetadata
attributes and will include the LogicalName as well as expanding the
OptionSet and GlobalOptionSet collection-valued navigation properties.
GET [Organization URI]/api/data/v8.1/EntityDefinitions(70816501-edb9-4740-a16c-6a5efbc05d84)/Attributes/Microsoft.Dynamics.CRM.PicklistAttributeMetadata?$select=LogicalName&$expand=OptionSet,GlobalOptionSet

Another option would be to get the data via the StringMap entity:
[Organization URI]/api/data/v9.1/stringmaps?fetchXml=<fetch><entity name='stringmap'><filter><condition attribute='objecttypecodename' operator='in'><value>account</value><value>opportunity</value></condition></filter></entity></fetch>
Will provide data that looks like this:
{
"#odata.etag": "W/\"406742363\"",
"value": "Open",
"attributename": "statecode",
"langid": 1033,
"objecttypecode": "opportunity",
"attributevalue": 0,
"stringmapid": "0fe09734-3914-e711-80ef-e0071b6a7121",
"organizationid": "f95718b2-5c63-46df-adc3-c3b546cf686a",
"displayorder": 1
},
{
"#odata.etag": "W/\"406742364\"",
"value": "Won",
"attributename": "statecode",
"langid": 1033,
"objecttypecode": "opportunity",
"attributevalue": 1,
"stringmapid": "10e09734-3914-e711-80ef-e0071b6a7121",
"organizationid": "f95718b2-5c63-46df-adc3-c3b546cf686a",
"displayorder": 2
},
Simpler query:
[Organization URI]/api/data/v9.1/stringmaps?$filter=objecttypecode eq 'account' or objecttypecode eq 'opportunity'

If your options are global, this is the easiest way to get all options:
/api/data/v9.1/GlobalOptionSetDefinitions(Name='new_category')

Use below code to get specific option set for specific entity:
(replace EntityLogicalName and AttributeLogicalName with your input params)
GET [Organization URI]/api/data/v9.1/EntityDefinitions(LogicalName='EntityLogicalName')/Attributes/Microsoft.Dynamics.CRM.PicklistAttributeMetadata?$select=LogicalName&$expand=OptionSet($select=Options),GlobalOptionSet($select=Options)&$filter=LogicalName eq 'AttributeLogicalName'

First, you need the attribute name and type, which you can find here:
/api/data/v9.1/EntityDefinitions(LogicalName='myEntity')?$select=LogicalName&$expand=Attributes($select=LogicalName)
Replace myEntity above with your entity name. The resulting page lists all the attributes that make up your entity. Locate the desired option set attribute and note its logical name and type.
Armed with that information, go here:
/api/data/v9.1/EntityDefinitions(LogicalName='myEntity')/Attributes(LogicalName='myAttribute')/Microsoft.Dynamics.CRM.MultiSelectPicklistAttributeMetadata?$select=LogicalName&$expand=OptionSet($select=Options),GlobalOptionSet($select=Options)
Replace myEntity and myAttribute with your entity name and attribute name, respectively. If your attribute type is not MultiSelectPicklistAttributeMetadata, replace that with the correct type that was found on the previous page. This returns a list of all the possible values for the option set (both text and numeric values).

Related

Post data to custom AL field in Business Central

I am trying to create an API call to add data into a custom field that was created on the Sales Order page using AL Extensions. The issue is when I try to do the api call through postman, I am getting "The Property "propertyName" does not exist on type 'Microsoft.NAV.salesOrder'". First of all, I don't even know if the API allows for this, so is it even possible? And secondly, if it is possible, is there a certain way to set up the API call or the Field through the AL Extension?
tableextension 50100 "AddProjectIdToSalesOrder" extends "Sales Header"
{
fields
{
field(50100; "CrmProjectId"; Guid)
{
Caption = 'Crm Project Id';
DataClassification = OrganizationIdentifiableInformation;
}
}
}
pageextension 50100 "AddProjectIdToSalesOrder" extends "Sales Order"
{
layout
{
addlast(General)
{
field("CRM Project Id"; Rec.CrmProjectId)
{
ApplicationArea = all;
ToolTip = 'The Guid of the related Project Record in the CRM environment';
}
}
}
}
This is how I am setting up the field with the AL extension, and for the post call, I am just creating a new Sales Order with a post and the body looks like:
{
"customerNumber" : "10000",
"CrmProjectId" : "random-guid"
}
And the error is "Bad Request": "The property 'CrmProjectId' does not exist on type 'Microsoft.NAV.salesOrder'. Make sure to only use property names that are defined by the type." Any help would be appreciated.
The Sales Order API is a separate page. It is not equivalent to the Sales Order page so you have to modify the API to accomplish what you want.
However the standard API's provided by Microsoft can't be extended.
You are left with two options:
Make a copy of the standard Sales Order API (this involves making a copy of all the linked APIs as well e.g. the Sales Line API).
Create a new API page with the single purpose of updating your new field. Then you would use the standard Sales Order API to create the Sales Order and then update CrmProjectId with a second call to your custom API page.

Issues with Quickbase API call

I am using JSON Quickbase API documentation below:
Quickbase API
I am trying to update records with Quickbase via recordId as per below, and it's working fine:
{
"to": "my-table-id-goes-here",
"data": [
{
"6": {
"value": "nancy more is the value to be updated"
},
"3": {
"value": "recordId_to_be_used_to_make_updates"
}
}
]
}
My issue: I want to update where email and userid is equal to certain value.
Eg. in normal SQL queries something like "update mytable_name set name ='nancy more' where email='nancy#gmail.com' and userid=70".
Is it possible with Quickbase? Is there a way to achieve that based on the code above, assuming email field is 7 and userid field is 8 or whatever?
The end result is possible but not through a single API call. The insert/update records API call for Quick Base only updates records when the key field is included in the record payload (the key field is the record ID by default but can be changed to another field in the table). If you don't already know the value of the key field, you'll need to query for the matching records first and then use the returned record ID/key field to perform that update.
For example, you could query for records where email is "nancy#gmail.com" and userid is 70:
POST https://api.quickbase.com/v1/records/query
QB-Realm-Hostname: host
Authorization: QB-USER-TOKEN userToken
Content-Type: application/json
{
"from": "tableId",
"where": "{7.EX.'nancy#gmail.com'}AND{8.EX.70}"
}
You can then use the id's of the returned set of records to perform your update. How you go about reading the response and making the upsert request will depend on the language you're using.

How are arguments added to GraphQL, do they need to be defined before?

Hi Everyone I am just trying to learn graphql as I am using Gatsby. I want to know does each field in graphql take an argument or does it need to be defined somehow before. So for example if you visit this link graphql search results
https://graphql.org/swapi-graphql?query=%7B%0A%09allPeople%20%7B%0A%09%20%20people%20%7B%0A%09%20%20%20%20id%0A%20%20%20%20%20%20name%0A%20%20%20%20%20%20birthYear%0A%20%20%20%20%20%20eyeColor%0A%09%20%20%7D%0A%09%7D%0A%7D%0A
If i wanted to limit people by eye color how would I do that. In the docs it seems easy as you would just do something like people(eyecolor: 'brown') but that doesn't seem possible. Am I missing something? I basically want to do a SQL style search for all people where eye color is brown.
Thanks.
Arguments need to be defined in the schema and implemented in the resolver. If you're consuming a 3rd party API (like the link you provided), you're limited to their schema. You can tell by looking at their schema (by clicking Docs on the right side of the page) which fields take arguments. For example, person takes id and personID arguments:
people doesn't take any arguments, as seen in the schema:
If you're building your own schema, you can add arguments to any field, and when you implement the resolver for that field you can use the arguments for logic in that resolver.
If you're working with a schema that you don't control, you'll have to add filtering on the frontend:
const {people} = data.allPeople;
const brownEyedPeople = people.filter(({eyeColor}) => eyeColor === 'brown');
When you start developing in Gatsby and actually pull your data into Gatsby, there will be a filter query option that automatically becomes available in the query arguments.
https://www.gatsbyjs.org/docs/graphql-reference/#filter
You can expect to be able to filter your people by eyeColor by using the below query:
{
allPeople(filter: { eyeColor: { eq: "brown" } }) {
edges {
node {
id
name
birthYear
eyeColor
}
}
}
}

Microsoft 365 API : Issue at attaching Contact to Campaign Response

I am trying to attach a contact to campaign response.
I am using rest API for that.
https://learn.microsoft.com/en-us/dynamics365/customer-engagement/web-api/campaignresponse?view=dynamics-ce-odata-9
Post Data :
{
"firstname": "TestFirst",
"lastname": "TestLast",
"emailaddress": "test#test.com",
"telephone": "1234567890",
"prioritycode": 0,
"responsecode": 1,
"subject": "Test Subject",
"statuscode": 1,
"regardingobjectid_campaign#odata.bind": "/campaigns(xxxx90c-11ef-e811-a966-000d3ad24a0d)",
"regardingobjectid_contact#odata.bind": "/contacts(xxxxfa2e-c3b5-e811-a962-000d3ad24a0d)"
}
Here is my JSON.
I am getting Error : “Campaign as RegardingObject must be supplied”. Without contact, it works fine.
I had the same problem and the documentation is not very clear about it, I had to check all the relationships of the CampaignResponse in order to understand how to solve this.
If you want to create a CampaignResponse linked to both a Campaign and a Contact you need to do the following:
Create a CampaignResponse with the "regardingobjectid_campaign#odata.bind" in the params sent.
POST "https://some_subdomain.crm6.dynamics.com/api/data/v9.0/campaignresponses"
{
"regardingobjectid_campaign#odata.bind": "/campaigns(CAMPAIGN_ID_HERE)",
"description": "some desc",
"subject": "some subject "
}
Then find the CampaignResponse you just created to get its activityid (every CampaignResponse is an Activity)
Finally, you need to create a new ActivityParty, that will link the Contact to the CampaignResponse.
POST "https://some_subdomain.crm6.dynamics.com/api/data/v9.0/campaignresponses(CAMPAIGN_ID_HERE)/activitypointer_activity_parties"
{
"partyid_contact#odata.bind": "/contacts(CONTACT_ID_HERE)",
"participationtypemask": 11 //this is the code for customers
}
The "Regarding" lookup field can only be set to a single "regarding" record. Even though it appears that there are different Regarding fields, one for each entity type, those are "helper" fields that let you easily set the main Regarding field by setting one of those regardingobjectid_xxx fields.
You must choose to use either a campaign or a contact as your Regarding field. You can of course create other lookups, so you could use the Regarding field for your campaign and then add an additional Contact lookup field, for example.

GraphQL how to make relational queries?

I am using GraphQL Express:
I am trying to run this query on relational data by property of related node:
{
Employee {
name
Item(name: "Laptop") {
name
}
}
}
But it is giving an error:
{
"errors": [
{
"message": "Unknown argument \"name\" on field \"Item\" of type \"Employee\".",
"locations": [
{
"line": 4,
"column": 10
}
]
}
]
}
Check Generated Schema Here
You can try it out here
What am I missing? Are they not supporting this kind of functionality yet?
Yes, as the error indicates there is no argument called name on the field Item.
Looks like they've got some invalid characters in their schema at the moment, which is causing issues with the GraphiQL interface loading correctly, but normally you could click on any field on the left hand side to open up the Docs panel and get additional details, including what arguments, if any, are available for it. Alternatively, you can press CTRL+SPACE or ALT+SPACE to activate the auto-complete (or just start typing).
The bigger problem is that you are using the name casing for queries as you are typesj.
You think you are trying to query all Employee types, but in fact it's hitting the Employee query which only has a name field, so Item (which should be item: Item) does not exist.
TL;DR, use capitalized strings for types, like Employee and non-capitalized names like employees to indicate a query that returns a number of Employee type instances.

Resources