0
votes

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?

1
Could you please add the actual parameter values? The error is very likely in the way the string parameter Activity is built. - Filburt
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
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
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

1 Answers

0
votes

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);