in servicenow, i need a backgound script that query the sctask table and then closed all…
- 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
- Assistant
You can create a background script in ServiceNow to query the
sctasktable, 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:
- Sys ID: Replace
INSERT_SCTASK_SYS_ID_HEREwith the actual Sys ID of the SCTask you want to work with. - State IDs: Ensure that the state values (
3for Closed) correspond to your instance's definitions of states. State numbers can differ based on instance configuration. - Testing: Test the script in a development or test environment before executing in production to ensure it behaves as expected.
- Logging: You might want to add logging (e.g.,
gs.info()statements) for tracking purposes. - 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.
- 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. - Sys ID: Replace
- User
i have 5 sctask that linked to 5 ritms
- Assistant
If you need to close all records associated with several
sctaskentries that are linked toRITMrecords, you can modify the script to loop over thosesctaskrecords. Below is an enhanced version of the previous script that can handle multiplesctaskrecords.The following code assumes that you want to close all
RITMand their respectiveRequeststhat are linked to allsctaskrecords. It does not filter for a specific set ofsctaskrecords; 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:
- Query for All
sctaskRecords: The script now queries all activesctaskrecords (you can adjust the query to filter as needed). - 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.
- Batch Processing: The script will process each
sctask, close the associatedRITM, and then close the relatedRequest.
Before Running the Script:
- Test Thoroughly: Always test the script in a development or staging environment first to ensure that it performs as expected without impacting production data.
- Understand Dependencies: If your instance has any specific workflows or conditions that prevent closing
RITMorRequests, you'll need to consider those as well in your logic. - 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 - backgroundin the application navigator. - Query for All
- User
need to set workflow false
- 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)andcurrent.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:
- 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 (likesys_updated_onandsys_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.
- Disable Workflows and System Fields: