Skip to main content

Notifications

Announcements

No record found.

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.

  • Kuku17 Profile Picture
    8 on at
    RE: Closing Incident in D365 Online via OData

    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!

  • Ray Profile Picture
    1,482 on at
    RE: Closing Incident in D365 Online via OData

    Did you get any response content?

  • Kuku17 Profile Picture
    8 on at
    RE: Closing Incident in D365 Online via OData

    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.

  • Kuku17 Profile Picture
    8 on at
    RE: Closing Incident in D365 Online via OData

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

  • Suggested answer
    Abdul Wahab Profile Picture
    12,085 Moderator on at
    RE: Closing Incident in D365 Online via OData

    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

  • Verified answer
    Ray Profile Picture
    1,482 on at
    RE: Closing Incident in D365 Online via OData

    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

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 293,209 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,923 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans