Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Unanswered

Error stringifying an object when calling Custom Action in OData

(0) ShareShare
ReportReport
Posted on by 8

I have created a custom action that takes in an input parameter called paramsInput (string).

I want to send to said action a stringified JSON object so that it may be deserialized inside the action.

public void Execute(IServiceProvider serviceProvider)
{
    try
    {
        _context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
        IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
        _crmService = factory.CreateOrganizationService(_context.UserId);

        string paramsInput = _context.InputParameters.Contains("paramsInput") ? _context.InputParameters["paramsInput"].ToString() : string.Empty;

        if (paramsInput != string.Empty)
        {
            InputParams inputParameters = JsonConvert.DeserializeObject(paramsInput);

            // logic here...       
        }
    }
    catch (Exception ex)
    {
        // handle exception...
    }
}

I have also created a generic function that recieves the action's name and the parameters to send and calls the action via OData:

public async Task CallMessage(string action, JObject parameters)
{
    RegenerateAccess();  // Microsoft Online

    bool succeeded = false;
    string urlAPI = "/api/data/v9.1/"   action;
    using (HttpClient client = new HttpClient())
    {
        // Setup client configuration here...
        
        HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, urlAPI);
        string jsonObj = JsonConvert.SerializeObject(parameters);
        request.Content = new StringContent(jsonObj, Encoding.UTF8, "application/json");
        request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");

        //Set the access token
        request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _authResult.AccessToken);
                
        HttpResponseMessage response = await client.SendAsync(request);
        response.EnsureSuccessStatusCode();
        succeeded = true;
        return succeeded;
    }
}

My issue is that I'm getting a Bad Request 400 back, and the response reads - "Microsoft.OData.ODataException: An unexpected 'StartObject' node was found for property named 'paramsInput' when reading from the JSON reader. A 'PrimitiveValue' node was expected."

My parameters JObject is structured as follows:

{

    "paramsInput" : {

          "id" : "xxxx-xxxx...", // some guid

          "code" : 1 // int

    }

}

or via code:

Guid guid = Guid.NewGuid();
JObject parameters = new JObject();
parameters.Add("id", guid);
parameters.Add("code", 1);
JObject wrapper = new JObject();
wrapper.Add("paramsInput", parameters);

// send wrapper JObject to CallMessage

I don't want to change the structure of CallMessage function as it is a generic function that may be used multiple times by other sources.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey Pt 2

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,820 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,514 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans