Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Scheduled Jobs

dixitreddy
Tera Contributor

Hi, 

In catalog item i have following fields,

1.Requested by

2.Requested for

3.Request Type

 

 Now my requirement is  if requested by is 'contract employee = true'(field in user table) and request type(choice variable in catalog item) is Contract termination then one task will be created i should close cancel the task and RITM as well it should be done using scheduled jobs in task level

1 REPLY 1

aryanjain25
Giga Guru

Hi @dixitreddy , 

You can create a scheduled job that runs periodically and checks for the conditions you specified. 

In the Script section, you can write the business logic for checking conditions and closing the task/RITM.

 

Here’s a sample script that will check the conditions and cancel the task and RITM (you can modify it further as per requirement): 

 

 

(function() {
    // Query for tasks where conditions match
    var taskGr = new GlideRecord('sc_task');
    taskGr.addQuery('request_item.requested_by.contract_employee', true); // Check if Requested By is a contract employee
    taskGr.addQuery('request_item.variables.request_type', 'Contract termination'); // Check if Request Type is Contract termination
    taskGr.addQuery('state', '!=', 'closed'); // Ensure the task is not already closed
    taskGr.query();
    
    while (taskGr.next()) {
        // Close the task as Cancelled
        taskGr.state = 'closed_cancelled';
        taskGr.update();

        // Also close the associated RITM (Request Item)
        var ritmGr = new GlideRecord('sc_req_item');
        if (ritmGr.get(taskGr.request_item)) {
            ritmGr.stage = 'closed_cancelled';
            ritmGr.state = 'closed_cancelled';
            ritmGr.update();
        }
    }
})();

 

 

 

I would appreciate if you can mark this response as correct or helpful if it helped you with your question.

 

Thanks,

Aryan Jain