Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0125-preview· openaiHugging Face Suzhen/CodeChat

Buttons are showing but Reopen Button is not showing if the RITM state is Closed…

  1. 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("table");data.sysid=input.sysid∣∣sp.getParameter("table"); data.sys_id = input.sys_id || 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();              
      }
           
        }
        }
    }
    
  2. 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:

    1. 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 (preferably data.table to avoid confusion).

    2. String Comparison: When comparing the table names, ensure that they are in the correct case. ServiceNow uses lower-case table names by default.

    3. Reading 'input' Object: Before accessing properties of input, ensure input is defined. This helps avoid runtime errors if input is not provided.

    4. 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 identical if statement. You can remove the inner if check 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 (1 for "Open" and 3 for whatever state implies it can be reopened) match your instance's configuration, as these values can sometimes vary between ServiceNow installations.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.