Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CRM16 - Trigger custom action from WebApi

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 ?

like image 290
Daniel Jørgensen Avatar asked Jan 24 '17 12:01

Daniel Jørgensen


1 Answers

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.

like image 182
Nkosi Avatar answered Nov 06 '22 01:11

Nkosi