In this article, we are going to see how to restrict the customer service associate to close/resolve the case if there are Open Activities or Child Cases still open for a Case.
Here’s how you can implement this using JavaScript and form customization:
Steps to Disable the Resolve case button
- Create a Web Resource for JavaScript
- Navigate to Settings > Customizations > Customize the System.
- Create a new Web Resource for your JavaScript code.
- Write the JavaScript Code: Write JavaScript code to check for open child cases and activities when the form is loaded and enable/disable the "Resolve Case" button accordingly.
JavaScript code
function disableResolveCaseButton(executionContext) {
var formContext = executionContext.getFormContext();
var caseId = formContext.data.entity.getId();
var fetchXmlChildCases = "<fetch no-lock='true'>" +
"<entity name='incident'>" +
"<attribute name='incidentid' />" +
"<filter>" +
"<condition attribute='parentcaseid' operator='eq' value='" + caseId + "' />" +
"<condition attribute='statecode' operator='eq' value='0' />" + // 0 is the state code for Active
"</filter>" +
"</entity>" +
"</fetch>";
var fetchXmlOpenActivities = "<fetch no-lock='true'>" +
"<entity name='activitypointer'>" +
"<attribute name='activityid' />" +
"<filter>" +
"<condition attribute='regardingobjectid' operator='eq' value='" + caseId + "' />" +
"<condition attribute='statecode' operator='eq' value='0' />" + // 0 is the state code for Open
"</filter>" +
"</entity>" +
"</fetch>";
Xrm.WebApi.retrieveMultipleRecords("incident", "?fetchXml=" + encodeURIComponent(fetchXmlChildCases)).then(
function (result) {
if (result.entities.length > 0) {
formContext.ui.controls.get("resolvecasebutton").setDisabled(true);
} else {
Xrm.WebApi.retrieveMultipleRecords("activitypointer", "?fetchXml=" + encodeURIComponent(fetchXmlOpenActivities)).then(
function (result) {
if (result.entities.length > 0) {
formContext.ui.controls.get("resolvecasebutton").setDisabled(true);
} else {
formContext.ui.controls.get("resolvecasebutton").setDisabled(false);
}
},
function (error) {
console.log(error.message);
}
);
}
},
function (error) {
console.log(error.message);
}
);
}
Add the Web Resource to the Case Form
Open the Case form customization.
Now, select the "Case" entity from the list of Entities and select the Form where you need to add the JavaScript to disable the "Resolve Case" button.
Once you open the form, go to the Form Properties and upload the JavaScript under the Form Libraries by clicking the "+ Add" button.
Once you click the new button as shown above you have to map the JavaScript file to add it into the Libraries as shown below.
After adding the JavaScript file, now you need to call the disableResolveCaseButton function from the JavaScript file in the OnLoad event.
Add the JavaScript Web Resource to the form libraries.
Bind the disableResolveCaseButton function to the OnLoad event of the form.
Publish the Customizations
Save and publish all customizations.
Additional Tips
- Form Context: Ensure you have the correct context if you are using the Unified Interface.
- Permissions: Ensure that the user executing this code has the necessary permissions to read child cases and activities.
Conclusion
By using a custom JavaScript function to check for open child cases and activities and then enabling or disabling the "Resolve Case" button based on the results, you can effectively enforce this rule within Dynamics 365 Customer Service.