2
votes

On Dynamics 365, we are trying to close incidents using the client side Web API.

After looking at the doc (in C#), we understand that we first need to create a IncidentResolution activity, which we did successfully. However, we don't understand how to fully close the Incident entity then.

I assume we need to update the record's stateCode and statusCode.. However, if I do so, ajax always return a 500 error.

Other updates are working fine.

Is there anything that we're missing here ?

var entity = {};
entity.statecode = 1; // Resolved
entity.statuscode = 5; // Problem Solved
entity.title = "Title of my case";

var req = new XMLHttpRequest();
req.open("PATCH", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/incidents(Case's guid)", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.onreadystatechange = function() {
    if (this.readyState === 4) {
        req.onreadystatechange = null;
        if (this.status === 204) {
            //Success - No Return Data - Do Something
        } else {
            Xrm.Utility.alertDialog(this.statusText);
        }
    }
};
req.send(JSON.stringify(entity)); 
1

1 Answers

3
votes

You have to use CloseIncident Action & POST method to do this. It's not a simple update request by using PATCH method, basically case closure will create an Incident Resolution entity record.

Normally I will compose the request using CRM REST builder, even that snippet is not executing successfully in this case. The complete working code example:

var incidentresolution = {
    "subject": "Put Your Resolve Subject Here",
    "[email protected]": "/incidents(<GUID>)", //Replace <GUID> with Id of case you want to resolve
    "timespent": 60, //This is billable time in minutes
    "description": "Additional Description Here"
};

var parameters = {
    "IncidentResolution": incidentresolution,
    "Status": -1
};

var context;

if (typeof GetGlobalContext === "function") {
    context = GetGlobalContext();
} else {
    context = Xrm.Page.context;
}

var req = new XMLHttpRequest();
req.open("POST", context.getClientUrl() + "/api/data/v8.2/CloseIncident", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.onreadystatechange = function () {
    if (this.readyState === 4) {
        req.onreadystatechange = null;
        if (this.status === 204) {
            //Success - No Return Data - Do Something
        } else {
            var errorText = this.responseText;
            //Error and errorText variable contains an error - do something with it
        }
    }
};
req.send(JSON.stringify(parameters));

Reference