Buttons are showing but Reopen Button is not showing if the RITM state is Closed…
- User
Buttons are showing but Reopen Button is not showing if the RITM state is Closed complete. This is the server side script I'm using, could you please correct if did anything wrong in the script? Based on function showRITMReopenButton it is showing Cancel Button but not showing Reopen button based on function showRITMReopenButton Server Side Script: (function() { //initialize the Request Item Cancel button, by default hidden: data.showRITMCancelButton = false; data.showRITMReopenButton = false; // Get table & sys_id
data. Table = input.table || sp.getParameter("sys_id"); // Valid GlideRecord
var gr = new GlideRecord(data. Table); if (!gr.isValid()) return; // Valid sys_id
if (!gr.get(data.sys_id)) return; // Get current record values var ritm_state = gr.getValue('state'); //var ritm_stateLabel = gr.getDisplayValue('state'); var ritm_active = gr.getValue('active'); //Check each RITM and verify if they still in the Open state. RITM state: 1 = Open, the initial state if (ritm_state == 1 && ritm_active == true && data.table == "sc_req_item"){ //gs.addInfoMessage(gs.getMessage("RITM is in Open state, RITM can be cancelled")); data.showRITMCancelButton = true; } else { //gs.addErrorMessage(gs.getMessage("RITM is in different than Open state ")); data.showRITMCancelButton = false; } if (ritm_state == 3 && data. Table == "sc_req_item") { data.showRITMReopenButton = true; } else { data.showRITMReopenButton = false; }
if (input && input.action) { if (input.action === "cancelRequestItem") { if (action == "cancelRequestItem") { // Cancel Request Item
gr.setValue('state', 4); gr.setValue('approval', 'withdrawn'); gr.setValue('stage', 'Request Cancelled'); gr.comments="Requested Item cancelled by "+gs.getUserDisplayName()+ " via Service Portal"; gr.update(); // ${Cancel this Request Item} //gs.addInfoMessage("Requested Item "+gr.number+" was withdrawn."); }if (input.action === "ReopenRequestItem") { if (action == "ReopenRequestItem") { gr.setValue('state', 1); gr.comments="Requested Item Reopend by "+gs.getUserDisplayName()+ " via Service Portal"; gr.update(); } } } } - Assistant
It seems there are a few issues with the script that could potentially cause the behavior you're experiencing, especially regarding the 'Reopen' button not showing up. Let me point out some corrections and suggestions to improve the script:
-
Case Sensitivity: JavaScript is case-sensitive. There's inconsistency in your variables, especially with
data. Table. It's declared with a space and sometimes used without a space. Stick to one variable naming convention (preferablydata.tableto avoid confusion). -
String Comparison: When comparing the table names, ensure that they are in the correct case. ServiceNow uses lower-case table names by default.
-
Reading 'input' Object: Before accessing properties of
input, ensureinputis defined. This helps avoid runtime errors ifinputis not provided. -
Action Check Redundancy: Inside your
if (input && input.action)block, there's redundancy in checking the action value (if (input.action === "cancelRequestItem") followed by an identicalifstatement. You can remove the innerifcheck since it's already determined by the outer one.
Here's a revised version of your server-side script with these issues addressed:
(function() { data.showRITMCancelButton = false; data.showRITMReopenButton = false; // Get table & sys_id from input or URL parameter data.table = input && input.table ? input.table : $sp.getParameter("table"); data.sys_id = input && input.sys_id ? input.sys_id : $sp.getParameter("sys_id"); // Verify table and sys_id var gr = new GlideRecord(data.table); // Ensure this matches the exact case as the table name in ServiceNow if (!gr.isValid()) return; if (!gr.get(data.sys_id)) return; // Extract the current record's state and active flag var ritm_state = gr.getValue('state'); var ritm_active = gr.getValue('active'); // Determine if cancel button should be shown if (ritm_state == '1' && ritm_active == 'true' && data.table == "sc_req_item") { data.showRITMCancelButton = true; } // Determine if reopen button should be shown if (ritm_state == '3' && data.table == "sc_req_item") { data.showRITMReopenButton = true; } // Perform actions based on input if (input && input.action) { if (input.action === "cancelRequestItem") { gr.setValue('state', 4); gr.setValue('approval', 'withdrawn'); gr.setValue('stage', 'Request Cancelled'); gr.comments = "Requested Item cancelled by " + gs.getUserDisplayName() + " via Service Portal"; gr.update(); } else if (input.action === "ReopenRequestItem") { gr.setValue('state', 1); gr.comments = "Requested Item Reopened by " + gs.getUserDisplayName() + " via Service Portal"; gr.update(); } } })();This revised script simplifies and corrects the issues, including formatting and logical redundancies. Make sure the states (
1for "Open" and3for whatever state implies it can be reopened) match your instance's configuration, as these values can sometimes vary between ServiceNow installations. -
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.