Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-mini-2024-07-18· OpenAIAi2 WildChat-4.8M (AI2)

in servicenow, i need a backgound script that query the sctask table and then closed all…

  1. User

    in servicenow, i need a backgound script that query the sctask table and then closed all the RITM and Req related to that particular task

  2. Assistant

    You can create a background script in ServiceNow to query the sctask table, and then close all related Request Items (RITMs) and Requests. This is a common operation, but keep in mind that running background scripts can affect live data, so it is advisable to test in a sub-production environment before executing in production.

    Here’s a sample script you can use:

    // Modify this to specify the sys_id of the sctask you want to query
    var sctaskSysId = 'INSERT_SCTASK_SYS_ID_HERE'; 
    
    // Query the sctask record
    var sctaskGr = new GlideRecord('sctask');
    if (sctaskGr.get(sctaskSysId)) {
        // Get the RITM related to this sctask
        var ritmGr = new GlideRecord('sc_req_item');
        ritmGr.addQuery('sys_id', sctaskGr.request_item);
        ritmGr.query();
        
        while (ritmGr.next()) {
            // Close the RITM
            ritmGr.state = 3; // State '3' typically means Closed
            ritmGr.update();
            
            // Get the Request related to the RITM
            var reqGr = new GlideRecord('sc_request');
            reqGr.get(ritmGr.request);
            
            // Close the Request if it’s not already closed
            if (reqGr.state != 3) { // State '3' is also typically Closed
                reqGr.state = 3; // Close the request
                reqGr.update();
            }
        }
    } else {
        gs.info('SCTask not found with sys_id: ' + sctaskSysId);
    }
    

    Notes:

    1. Sys ID: Replace INSERT_SCTASK_SYS_ID_HERE with the actual Sys ID of the SCTask you want to work with.
    2. State IDs: Ensure that the state values (3 for Closed) correspond to your instance's definitions of states. State numbers can differ based on instance configuration.
    3. Testing: Test the script in a development or test environment before executing in production to ensure it behaves as expected.
    4. Logging: You might want to add logging (e.g., gs.info() statements) for tracking purposes.
    5. Check Dependencies: If there are any dependencies or custom logic tied to closing RITMs or Requests, ensure that you handle those appropriately in your script.
    6. Batch Updates: If you're handling many records, consider batch processing to avoid performance impacts.

    This script will find the specified sctask, retrieve its related RITM, close each one, and then close the related Request. Make sure to adjust as needed based on your specific requirements and workflows in ServiceNow.

  3. User

    i have 5 sctask that linked to 5 ritms

  4. Assistant

    If you need to close all records associated with several sctask entries that are linked to RITM records, you can modify the script to loop over those sctask records. Below is an enhanced version of the previous script that can handle multiple sctask records.

    The following code assumes that you want to close all RITM and their respective Requests that are linked to all sctask records. It does not filter for a specific set of sctask records; instead, it closes all linked records:

    // Query all sctasks
    var sctaskGr = new GlideRecord('sctask');
    sctaskGr.addQuery('active', true); // Only consider active tasks, you can modify query as needed
    sctaskGr.query();
    
    while (sctaskGr.next()) {
        gs.info('Processing SCTask: ' + sctaskGr.sys_id); // Log current sctask for tracking
        
        // Get the RITM related to this sctask
        var ritmGr = new GlideRecord('sc_req_item');
        ritmGr.addQuery('sys_id', sctaskGr.request_item);
        ritmGr.query();
        
        while (ritmGr.next()) {
            // Close the RITM
            ritmGr.state = 3; // State '3' typically means Closed
            ritmGr.update();
            gs.info('Closed RITM: ' + ritmGr.number); // Log closed RITM
            
            // Get the Request related to the RITM
            var reqGr = new GlideRecord('sc_request');
            reqGr.get(ritmGr.request);
            
            // Close the Request if it’s not already closed
            if (reqGr.state != 3) { // State '3' is also Closed
                reqGr.state = 3; // Close the request
                reqGr.update();
                gs.info('Closed Request: ' + reqGr.number); // Log closed Request
            }
        }
    }
    

    Important Changes:

    1. Query for All sctask Records: The script now queries all active sctask records (you can adjust the query to filter as needed).
    2. Logging: Added logging statements so you can see the progress and which records are being processed. This can be useful for tracking in the ServiceNow logs.
    3. Batch Processing: The script will process each sctask, close the associated RITM, and then close the related Request.

    Before Running the Script:

    1. Test Thoroughly: Always test the script in a development or staging environment first to ensure that it performs as expected without impacting production data.
    2. Understand Dependencies: If your instance has any specific workflows or conditions that prevent closing RITM or Requests, you'll need to consider those as well in your logic.
    3. Backup: Consider backing up relevant data or checking how many records will be affected before running the script in production to avoid unintended data loss.

    Running the Script:

    You can run this script in the Scripts - Background module in ServiceNow, which you can access by typing scripts - background in the application navigator.

  5. User

    need to set workflow false

  6. Assistant

    If you want to close the Request Items (RITMs) and Requests without triggering any workflows or business rules that may be associated with those records, you can set current.setWorkflow(false) and current.autoSysFields(false) methods within your background script.

    Here's how you can incorporate this into the previous script:

    // Query all active sctasks
    var sctaskGr = new GlideRecord('sctask');
    sctaskGr.addQuery('active', true); // You can adjust the query as needed
    sctaskGr.query();
    
    while (sctaskGr.next()) {
        gs.info('Processing SCTask: ' + sctaskGr.sys_id); // Log current sctask for tracking
        
        // Get the RITM related to this sctask
        var ritmGr = new GlideRecord('sc_req_item');
        ritmGr.addQuery('sys_id', sctaskGr.request_item);
        ritmGr.query();
        
        while (ritmGr.next()) {
            // Close the RITM without triggering workflows
            ritmGr.setWorkflow(false);
            ritmGr.autoSysFields(false);
            ritmGr.state = 3; // State '3' typically means Closed
            ritmGr.update();
            gs.info('Closed RITM: ' + ritmGr.number); // Log closed RITM
            
            // Get the Request related to the RITM
            var reqGr = new GlideRecord('sc_request');
            if (reqGr.get(ritmGr.request)) {
                // Close the Request without triggering workflows
                reqGr.setWorkflow(false);
                reqGr.autoSysFields(false);
                if (reqGr.state != 3) { // State '3' is also Closed
                    reqGr.state = 3; // Close the request
                    reqGr.update();
                    gs.info('Closed Request: ' + reqGr.number); // Log closed Request
                }
            } else {
                gs.info('Request not found for RITM: ' + ritmGr.number);
            }
        }
    }
    

    Explanation of Changes:

    1. Disable Workflows and System Fields:
      • setWorkflow(false): This disables the execution of business rules and workflows when the record is updated.
      • autoSysFields(false): This prevents ServiceNow from updating the system fields (like sys_updated_on and sys_updated_by) when the record is updated.

    Testing and Caution:

    • Test Thoroughly: As always, ensure you test this in a development or staging environment first.
    • Monitor the Logs: Pay special attention to the logs to see if records are being processed correctly.
    • Backup Data: If these records are critical, ensure you have a backup or can restore them if necessary.

    Running the Script:

    You can execute it in the Scripts - Background module as mentioned before, ensuring to monitor the logs for any inconsistencies or issues raised by the script.