Hi all,
So I've been trying to via Javascript and the Ribbon Workbench create a quick resolve button for a case without the case resolution dialogue popping up. I've created two web resource scripts:
1. One to act as a trigger to call the actual web resource JS function that closes the incident/case. This function will call my second function and pass along as parameters the case ID and all the incident resolution details.
2. The actual function that performs the closeincident action. It will take the parameter values passed through the 1st web resource script and perform the close incident function.
I built it this way in case I want to reuse the close incident function somewhere else. For example have a button on a email that allows a user to send the email and then close the case.
Thanks to Andrew Butenko for his post on how to do this using Javascript
https://butenko.pro/2017/03/20/close-incident-using-webapi/
Below is the breakdown of each script:
1 Script to trigger the actual close incident function
function triggerCloseIncident (executionContext) { var formContext = executionContext.getFormContext(); // Get the case ID so that I can pass it as a parameter to the actual close incident function var caseIdRaw = formContext.data.entity.getId(); var caseId = caseIdRaw.slice(1, -1); // Create variables for the incident resolution dialogue as well as the case status var subject = 'quick resolve' var billableHours = 60; var resolutionDescription = ' quick resolve'; var resolutionCode = -1; // Call the second function which does the actual closing of the incident closeIncident(subject, caseId, billableHours, resolutionDescription, resolutionCode); // Save and refresh the form after resolving the case formContext.data.refresh(true); }
2 Script to actually close the incident case
function closeIncident(subject, caseId, billableHours, resolutionDescription, resolutionCode) { // Incident Resolution properties var incidentresolution = { "subject": subject, "incidentid@odata.bind": "/incidents(" caseId ")", //Id of incident "timespent": billableHours, //This is billable time in minutes "description": resolutionDescription }; var parameters = { "IncidentResolution": incidentresolution, "Status": resolutionCode }; var context; if (typeof GetGlobalContext === "function") { context = GetGlobalContext(); } else { context = Xrm.Page.context; } var req = new XMLHttpRequest(); req.open("POST", context.getClientUrl() "/api/data/v9.1/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 } else { Xrm.Utility.alertDialog(this.statusText); } } }; req.send(JSON.stringify(parameters)); }
I then tested this function by using a two option field on the case form and it works perfectly.
I now want to instead of adding it as a field on the form itself and triggering the quick resolve from the form to instead create a button on the command bar which will call my javascript function 1 which then calls my javascript function 2. The problem is that I'm not sure how to get the case ID from the form via the ribbon workbench. You can see I'm using
to get the case ID to pass to the other function but I'm assuming you can't do this on the ribbon workbench. Also I'm not sure how to perform the same save and refresh action that I'm able to perform using:
Any help would be greatly appreciated.