CRM16 - Trigger custom action from WebApi
Asked Answered
P

3

10

I've built a custom action in CRM that I need to fire through its WebAPI. The custom action is activated and I got no errors in CRM while creating it.

enter image description here

I try to call this action from a VB.NET application like:

Dim httpch As New HttpClientHandler
Dim requestUri As String = "contacts(1fcfd54a-15d3-e611-80dc-0050569ea396)/Microsoft.Dynamics.CRM.new_addnotetocontact"
httpch.Credentials = New NetworkCredential("username", "password", "domain")
Dim httpClient As New HttpClient(httpch)
httpClient.BaseAddress = New Uri(CRMWebApiUri)
httpClient.Timeout = New TimeSpan(0, 2, 0)
httpClient.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0")
httpClient.DefaultRequestHeaders.Add("OData-Version", "4.0")
httpClient.DefaultRequestHeaders.Add("Prefer", "odata.include-annotations='OData.Community.Display.V1.FormattedValue'")
httpClient.DefaultRequestHeaders.Accept.Add(New MediaTypeWithQualityHeaderValue("application/json"))
Dim jsonNote As JObject = New JObject(New JProperty("NoteTitle", "'Mails have been deleted'"), New JProperty("NoteText", "This contacts SmarterMail data has been deleted due to inactivity"))
Dim postData = New StringContent(jsonNote.ToString(), Encoding.UTF8, "application/json")

Dim retrieveContactResponse As HttpResponseMessage = httpClient.PostAsync(requestUri, postData).Result

What i get back is a status 400 with a message:

Request message has unresolved parameters.

I can make other calls to the same site and get all contacts as an example

What does this mean and how do I fix it ?

Protractile answered 24/1, 2017 at 12:28 Comment(3)
would like a C# example, showing how to pass information into an action within the body and how to create that action in CRM 2016, as well as process the parametersFrendel
@l--''''''---------'''''''''''' Creating your own actions in Dynamics CRM 2016Nomadic
@l--''''''---------'''''''''''' Web API Functions and Actions Sample (C#) - Dynamics CRM 2016Nomadic
P
0

I fixed this some time ago but haven't got the time to get back to answer it. In my case what was the issue was the way i made the request it self

Instead of the following way, which is stated in the question:

Dim postData = New StringContent(jsonNote.ToString(), Encoding.UTF8, "application/json")
Dim retrieveContactResponse As HttpResponseMessage = httpClient.PostAsync(requestUri, postData).Result

Instead of using the httpClient.PostAsync method and providing the StringContent object directly, I used the HttpRequestMessage object and giving that the StringContent object, and then provide the HttpRequestMessage object to the SendAsync method of the httpClient, and that seems to have solved my issue, as it is working now. Also notice that in the original question i had quotation marks in the value of the first JProperty in the JObject to be posted, I'm not sure this has anything to do with it though, but just posting it here as it is different from original code:

Dim jsonNote As JObject = New JObject(New JProperty("NoteTitle", "Mails have been deleted"), New JProperty("NoteText", "This contact's SmarterMail data has been deleted automatically due to inactivity on their CRM account"))
...
Dim reqMsg As New HttpRequestMessage(HttpMethod.Post, CRMWebApiUri + requestUri)
reqMsg.Content = New StringContent(jsonNote.ToString(), Encoding.UTF8, "application/json")
Dim retrieveContactResponse As HttpResponseMessage = httpClient.SendAsync(reqMsg).Result
Protractile answered 26/7, 2017 at 12:50 Comment(0)
N
6

What does this mean and how do i fix it ?

Referencing Request message has unresolved parameters.

In CRM when you get this error while calling action. then there may be three reasons behind that

  1. some parameters you are passing wrong. (make sure action name is correctly pass)
  2. your action is not activated
  3. your action name is duplicate and one action is in active mode and other is in draft.(as this is done from CRM side that one has to be in draft only two same name action wont be active at same time.)

No. 2 is already taken care of as it was already state that the custom action is activated.

No. 3 is addressed in the linked article and is plausible if you may have imported the actions twice in CRM or inadvertently created two actions with the same name.

To address no.1, I would suggest creating an object model to hold the data to be sent

Public Class Note 
    Public Property NoteTitle As String
    Public Property NoteText As String
End Class

CRM is very finicky about proper parameter formatting. Parameter names are also case sensitive. The '' in the NoteTitle will cause issues when serializing. Also, if possible use NewtonSoft.Json to craft the JSON payload instead of trying to build it on your own.

'Handler with credentials
Dim httpClientHandler As New HttpClientHandler With {
    .Credentials = New NetworkCredential("username", "password", "domain")}
'Create and configure HTTP Client 
Dim httpClient As New HttpClient(httpClientHandler) With {
    .BaseAddress = New Uri(CRMWebApiUri),
    .Timeout = New TimeSpan(0, 2, 0)}
httpClient.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0")
httpClient.DefaultRequestHeaders.Add("OData-Version", "4.0")
httpClient.DefaultRequestHeaders.Add("Prefer", "odata.include-annotations='OData.Community.Display.V1.FormattedValue'")
httpClient.DefaultRequestHeaders.Accept.Add(New MediaTypeWithQualityHeaderValue("application/json"))

'Create and populate data to be sent
Dim model As New Note With { 
    .NoteTitle = "Mails have been deleted", 
    .NoteText = "This contacts SmarterMail data has been deleted due to inactivity"}
'Serialize mode to well formed JSON
Dim json As String = JsonConvert.SerializeObject(model)
Dim postData = New StringContent(json, Encoding.UTF8, "application/json")
'invoking action using the fully qualified namespace of action message
Dim requestUri As String = "contacts(1fcfd54a-15d3-e611-80dc-0050569ea396)/Microsoft.Dynamics.CRM.new_addnotetocontact"
'POST the data
Dim retrieveContactResponse As HttpResponseMessage = Await httpClient.PostAsync(requestUri, postData)

Additional reference Dynamics CRM 2016: Use Web API actions

When invoking a bound function, you must include the full name of the function including the Microsoft.Dynamics.CRM namespace. If you do not include the full name, you will get the following error: Status Code:400 Request message has unresolved parameters.

Nomadic answered 18/7, 2017 at 22:56 Comment(0)
U
0

Change the name new_addnotetocontact into new_AddNoteToContact, it will work.

Dim requestUri As String = "contacts(1fcfd54a-15d3-e611-80dc-0050569ea396)/Microsoft.Dynamics.CRM.new_AddNoteToContact"

Process Schema is like below:

<Action Name="new_AddNoteToContact" IsBound="true">
  <Parameter Name="entity" Type="mscrm.contact" Nullable="false" />
  <Parameter Name="NoteTitle" Type="Edm.String" Nullable="false" Unicode="false" />
  <Parameter Name="NoteText" Type="Edm.String" Nullable="false" Unicode="false" />
  <ReturnType Type="mscrm.annotation" Nullable="false" />
</Action>

Unique Name: new_AddNoteToContact

Ref: https://msdn.microsoft.com/en-us/library/mt607600.aspx#Anchor_3

Update: If you would have edited the unique name after creating action, then duplicates will be created in Processes entity. Pls delete the dupes from Adv.Find

Unbending answered 25/7, 2017 at 18:40 Comment(0)
P
0

I fixed this some time ago but haven't got the time to get back to answer it. In my case what was the issue was the way i made the request it self

Instead of the following way, which is stated in the question:

Dim postData = New StringContent(jsonNote.ToString(), Encoding.UTF8, "application/json")
Dim retrieveContactResponse As HttpResponseMessage = httpClient.PostAsync(requestUri, postData).Result

Instead of using the httpClient.PostAsync method and providing the StringContent object directly, I used the HttpRequestMessage object and giving that the StringContent object, and then provide the HttpRequestMessage object to the SendAsync method of the httpClient, and that seems to have solved my issue, as it is working now. Also notice that in the original question i had quotation marks in the value of the first JProperty in the JObject to be posted, I'm not sure this has anything to do with it though, but just posting it here as it is different from original code:

Dim jsonNote As JObject = New JObject(New JProperty("NoteTitle", "Mails have been deleted"), New JProperty("NoteText", "This contact's SmarterMail data has been deleted automatically due to inactivity on their CRM account"))
...
Dim reqMsg As New HttpRequestMessage(HttpMethod.Post, CRMWebApiUri + requestUri)
reqMsg.Content = New StringContent(jsonNote.ToString(), Encoding.UTF8, "application/json")
Dim retrieveContactResponse As HttpResponseMessage = httpClient.SendAsync(reqMsg).Result
Protractile answered 26/7, 2017 at 12:50 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.