Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-2024-05-13· OpenAIAi2 WildChat-4.8M (AI2)

In servicenow, i have a table and there is one task linked to it and on a task there is…

  1. User

    In servicenow, i have a table and there is one task linked to it and on a task there is sub tasks also. I need to cancel all the main ARR request, HARL task linked to ARR and HART tasks linked to HARL through background script

  2. Assistant

    In ServiceNow, background scripts are used for administrative tasks, such as modifying records in bulk or performing complex workflows that aren't easily done through the standard UI. Given your requirement, you'll need to enumerate through your records in the different tables and update their statuses accordingly.

    Here's a background script to cancel the ARR requests, as well as the HARL and HART tasks linked to them:

    Note: Always test scripts in a development or test environment before running them in production.

    1. Cancel ARR Requests
      • First, identify and cancel the main ARR requests.
    2. Cancel HARL Tasks linked to ARR
      • Identify HARL tasks that are linked to the ARR requests and cancel them.
    3. Cancel HART Tasks linked to HARL
      • Identify HART tasks that are linked to the HARL tasks and cancel them.

    Here is a detailed example of how the script might look:

    // Function to cancel a record.
    function cancelRecord(record) {
        record.state = 'cancelled'; // Assuming 'cancelled' is the state you want to set, this might change based on your implementation.
        record.update();
    }
    
    // First, cancel ARR requests.
    var arrGR = new GlideRecord('u_arr_table'); // Replace 'u_arr_table' with your ARR table name
    arrGR.addQuery('state', '!=', 'cancelled'); // Only target active records
    arrGR.query();
    while (arrGR.next()) {
        var arrSysId = arrGR.sys_id;
        
        // Cancel linked HARL tasks.
        var harlGR = new GlideRecord('u_harl_table'); // Replace 'u_harl_table' with your HARL table name
        harlGR.addQuery('u_arr_reference', arrSysId); // Replace 'u_arr_reference' with the actual reference field in HARL
        harlGR.query();
        while (harlGR.next()) {
            
            var harlSysId = harlGR.sys_id;
            
            // Cancel linked HART tasks.
            var hartGR = new GlideRecord('u_hart_table'); // Replace 'u_hart_table' with your HART table name
            hartGR.addQuery('u_harl_reference', harlSysId); // Replace 'u_harl_reference' with the actual reference field in HART
            hartGR.query();
            while (hartGR.next()) {
                cancelRecord(hartGR);
            }
            
            // Cancel the HARL record.
            cancelRecord(harlGR);
        }
        
        // Cancel the ARR record.
        cancelRecord(arrGR);
    }
    
    gs.print('All relevant tasks have been cancelled.');
    

    Explanation:

    1. cancelRecord: Utility function to set the state to 'cancelled' and update the record.
    2. ** ARR requests cancellation**: First loop through the ARR table and ensure each active ARR request is identified.
    3. HARL tasks cancellation: For each ARR request, find the linked HARL tasks and cancel them.
    4. HART tasks cancellation: For each HARL task, find the linked HART tasks and cancel them.
    5. gs.print: Output a message to the logs indicating the script completed.

    Important Considerations:

    • Table Names: Replace 'u_arr_table', 'u_harl_table', and 'u_hart_table' with the actual names of your tables.
    • State Field: The state field may vary based on your implementation. Ensure it matches your table's field for statuses.
    • Query Optimizations: Depending on the number of records, you might want to add pagination or additional filtering to handle large datasets.
    • Concurrency: If running in a production environment, consider the impact on current operations and database locks.

    By running this script, you should be able to cancel all main ARR requests and their related HARL and HART tasks efficiently.