0

I have Global action in CRM. It have one input string parameter. This is call for sending request

obj.CallActivity("this is parameter string","MyActionName")

and my method CallActivity looks like this

public async Task<bool> CallActivity(string record, string Activity)
        {
           try
            {

                HttpRequestMessage requestMessage = null;
                requestMessage = new HttpRequestMessage(HttpMethod.Post, App.ResourceUri + "/api/data/v8.2/" + Activity); //uri to activity
                requestMessage.Content =new StringContent(record); 
                requestMessage.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
                //Send the HttpRequest
                Task<HttpResponseMessage> response = httpClient.SendAsync(requestMessage);
                  response.Wait();
                //If the response is Successfully executed then it will return the value true
                if (response.Result.IsSuccessStatusCode)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception error)
            {

                return false;
            }
        }

When the executing the request, I get the message:

StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:

Where I am making mistakes?

Filburt
  • 17,626
  • 12
  • 64
  • 115
user238271
  • 643
  • 3
  • 11
  • 27
  • Could you please add the actual parameter values? The error is very likely in the way the string parameter `Activity` is built. – Filburt Oct 13 '17 at 11:32
  • obj.CallActivity("{"id_contact":"452e368a-1783-e711-8102-70106faa95f1","id_car":"45436d5b-d19d-e711-8101-70106faa5221"}","MyActionName"). Input string parameter need to be like JSON string – user238271 Oct 13 '17 at 11:59
  • I assume your "MyActionName" consists of a publisher prefix and your Action schema name, so this shouldn't be the issue. However you are not setting the O-Data version headers. I've seen the Web API throw a wobbly over this so you may want to try adding `"OData-MaxVersion", "4.0"` and `"OData-Version", "4.0"` headers. – Filburt Oct 13 '17 at 12:17
  • On initialization class that have method CallActivity I set httpClient.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0"); httpClient.DefaultRequestHeaders.Add("OData-Version", "4.0"); httpClient.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json")); – user238271 Oct 13 '17 at 12:31
  • You need to use SDK. Possible duplicate of [How to call Action with parameter(s) using ExecuteWorkflowRequest in Dynamics CRM 2016?](https://stackoverflow.com/questions/37965325/how-to-call-action-with-parameters-using-executeworkflowrequest-in-dynamics-cr) – Alex Oct 16 '17 at 08:09
  • Is this being called from the CRM server or from an external application? – Reuben Swartz Oct 19 '17 at 12:44
  • Yes, I call it from external application – user238271 Oct 20 '17 at 13:31

1 Answers1

0

Below is the code which I use to call my Action from Plugin.

Sender and regardingcontact are the two parameters for my Action and this works fine for me.

OrganizationRequest req = new OrganizationRequest("crmp_emailbenachrichtigunganeventowner468fcc7975b9e71180e6005056936953");

                        req["Sender"] = new EntityReference(temp.GetAttributeValue<EntityReference>("ownerid").LogicalName, temp.GetAttributeValue<EntityReference>("ownerid").Id);

                        req["regardingcontact"] = new EntityReference(context.PrimaryEntityName, context.PrimaryEntityId);
                        OrganizationResponse response = service.Execute(req);
AnkUser
  • 5,421
  • 2
  • 9
  • 25