web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

Closing Incident in D365 Online via OData

(0) ShareShare
ReportReport
Posted on by 8

I have created a Controller for one of our customers to use. It should run on POST and recieve a JSON body with two parameters: id and statuscode.

The logic is simple - I wish to fetch the incident whose guid equals id and change its statuscode based on the recieved statuscode.

Code of controller:

public async Task CloseIncident([FromBody] MyCustomRequest _request)
{
    try 
    {
        // Some logic here to check if both Id and StatusCode exist in _request ...
        
        if(Guid.TryParse(_request.Id, out Guid guid))
        {
            // Construct OData request
            JObject incidentResolution = new JObject();
            incidentResolution.Add("subject", "testing");
            incidentResolution.Add("incidentid@odata.bind", $"/incidents({guid})");
            incidentResolution.Add("timespent", 2); //This is billable time in minutes
            incidentResolution.Add("description", "description");

            JObject parameters = new JObject();
            parameters.Add("IncidentResolution", incidentResolution);
            if (_request.StatusCode == 1)
            {
                parameters.Add("Status", (int)IncidentStatusCode.ProblemSolved);
            }
            else
            {
                parameters.Add("Status", (int)IncidentStatusCode.SomeOtherRejectedStatusCode);
            }

            RegenerateAccess(); // Connect to Microsoft Online

            string urlAPI = "/api/data/v9.1/CloseIncident";
            using (HttpClient client = new HttpClient())
            {
                client.BaseAddress = new Uri(_serviceUrl);
                client.Timeout = new TimeSpan(0, 2, 0);  //2 minutes  
                client.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0");
                client.DefaultRequestHeaders.Add("OData-Version", "4.0");
                //client.DefaultRequestHeaders.Add("Prefer", "return=representation");
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                HttpMethod method = HttpMethod.Post;

                HttpRequestMessage request = new HttpRequestMessage(method, urlAPI);
                request.Content = new StringContent(parameters.ToString(), Encoding.UTF8, "application/json");

                //Set the access token
                request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _authResult.AccessToken);
                HttpResponseMessage response = await client.SendAsync(request);
                response.EnsureSuccessStatusCode();
                
                return new MyCustomResponse()
                {
                    Status = Status.Success,
                    Message = "..."
                };
            }
            
            return new MyCustomResponse()
            {
                Status = Status.Error,
                Message = "..."
            };
        }
        else throw new Exception("Guid is invalid.");
        
    }
    catch(Exception ex)
    {
        return new MyCustomResponse() { Status = Status.Error, Message = ex.Message };
    }
}

I'm getting a Bad Request from the client.SendAsync line. I think the OData body request is incorrect, but I can't figure out why.

Any help would be appreciated.

I have the same question (0)
  • Verified answer
    Ray Profile Picture
    1,537 on at

    Hi,

    My guess is that your Status is not valid for case resolve.

    You can check the available status in statuscode field edit form.

    You should first select the Status to Resolved and then check the listed status:

    pastedimage1672110936978v1.png

    You may need to check about the Status Reason Transitions if this feature is configured:

    pastedimage1672111268922v2.png

    At the end, I suggest writing down the response content, usually they are useful for RCA:

    pastedimage1672111518990v3.png

  • Suggested answer
    Abdul Wahab Profile Picture
    12,119 Moderator on at

    Hi Kuku17,

    As per the documentation, the cause is: 

    This error occurs when you use the incorrect name for a property. Property names are case sensitive.

    In the example above, there is property called name, but not one named Name.

    For more details: learn.microsoft.com/.../web-api-client-errors

  • Kuku17 Profile Picture
    8 on at

    R@y  Hi, I tested it again with a hardcoded valid statuscode for case resolve and still getting a Bad Request

  • Kuku17 Profile Picture
    8 on at

    Abdul Wahab Hi, it seems if I only send the subject without the other parameters (incidentid, timespent, description) I get back a 204 response so my guess is that something is wrong with the parameter of incidentid as the other two are optional.

    I tried using PascalCase and camelCase but did not help.

  • Ray Profile Picture
    1,537 on at

    Did you get any response content?

  • Kuku17 Profile Picture
    8 on at

    R@y Yes. Apparently the problem was such that the user cannot resolve an incident before attaching required documents to said incident. Once I deleted the documents I was able to close the incident.

    Thank you for your help!

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
ManoVerse Profile Picture

ManoVerse 162 Super User 2026 Season 1

#2
11manish Profile Picture

11manish 134

#3
Jimmy Passeti Profile Picture

Jimmy Passeti 55 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans