To accommodate the requirements you provided, we need to make several enhancements to the Server Side Script to support the display logic for the Cancel and Reopen buttons based on the state of the sc_req_item record and the elapsed time since the item was closed.
I'll outline the necessary changes below:
- For the Cancel button to appear if the RITM state is open.
- To configure the Reopen button, making it appear if the state is
closed_complete and only for 5 days from the closed_complete date.
Server Side Script Adjustments
To implement the Reopen logic with a 5-day condition, you'll need to calculate the difference between the current date and the closed date, then decide whether to show the Reopen button based on this difference. Here’s how you can adjust your server-side script to include these requirements:
(function() {
// Initially hide buttons
data.showCancel = false;
data.showReopen = false;
data.table = input.table || $sp.getParameter("table");
data.sys_id = input.sys_id || $sp.getParameter("sys_id");
var gr = new GlideRecord(data.table);
if (!gr.isValid()) return;
if (!gr.get(data.sys_id)) return;
var ritm_state = gr.getValue('state');
var ritm_closed_at = gr.getValue('closed_at'); // Assuming 'closed_at' is the field name
// Show Cancel if state is open
if (ritm_state == 1) { // Assuming '1' indicates the Open state
data.showCancel = true;
}
// Calculate difference between now and closed_at date for Reopen condition
if (ritm_state == 'closed_complete' /* Replace with actual value indicating closed_complete state */ ) {
var closedAt = new GlideDateTime(ritm_closed_at); // Convert closed_at string to GlideDateTime
var now = new GlideDateTime(); // Current DateTime
var duration = GlideDateTime.subtract(now, closedAt); // Find difference
var daysSinceClosed = duration.getDurationValue().substring(1, 2); // Assuming format of "d HH:mm:ss"
if (parseInt(daysSinceClosed) <= 5) {
data.showReopen = true; // Show Reopen if within 5 days
}
}
if (input && input.action) {
if (data.table == "sc_req_item") {
if (input.action == 'cancelRequestItem') {
// Cancel logic here
}
else if (input.action == 'reopenRequestItem') { // Action to handle reopen
// Reopen logic here, ensure you also handle state and possibly clear closed_at
}
}
}
})(data);
Handling the Reopen Action
When implementing the logic for reopening a request item, remember to properly set the state back to an appropriate value that indicates the item is reopened. Also, consider if you need to clear or adjust the closed_at date to reflect this change.
Adjusting the Client Controller Script
Your Client Controller Script seems adequately set up to handle calling the server with specified actions and then refreshing itself upon receiving a response. Make sure your UI properly triggers the uiAction function with the correct action ('cancelRequestItem' or 'reopenRequestItem') based on user interaction.
This guide provides the necessary logic for extending your widget as per your requirements but remember to adjust field names ('closed_at', state values, etc.) and logic to fit your exact ServiceNow setup, as these can vary between instances and configurations.
Always test thoroughly in a sub-production environment before deploying changes to your production instance.