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.

How to create the sc task particular request

Tharun13
Tera Contributor

Hi All,

 

How to create the 2 or 2 sc task particular request through background script.

 

Requests already created our instance, but we have to created manual tasks every request.

 

Regards,

Tharun 

1 REPLY 1

Aniket Chavan
Tera Sage
Tera Sage

Hello @Tharun13 ,

To create Service Catalog tasks (sc_task) for specific requested items (RITM) through a background script, you'll need to follow these steps. The script will target the specific request items you want to associate the tasks with.

Here's a basic outline of the approach:

  1. Identify the Requested Item(s): You'll need to determine the RITMs for which you want to create sc_tasks. This can be done by using an encoded query to filter the RITMs.

  2. Use a GlideRecord to Iterate Over RITMs: You'll query the sc_req_item table and loop through each RITM to create the corresponding sc_tasks.

  3. Set Mandatory Fields: While creating the sc_tasks, ensure you populate all mandatory fields such as request_item, assignment_group, and assigned_to.

 

Here’s a sample script:

// Specify the criteria for the requested items you want to target
var encodedQuery = 'request=YOUR_REQUEST_SYS_ID'; // Replace with your specific request sys_id
var ritmGr = new GlideRecord('sc_req_item');
ritmGr.addEncodedQuery(encodedQuery);
ritmGr.query();

while (ritmGr.next()) {
    var scTask = new GlideRecord('sc_task');
    scTask.initialize(); // Initialize a new sc_task record
    
    // Set the mandatory fields for the sc_task
    scTask.request_item = ritmGr.sys_id; // Link to the requested item
    scTask.assignment_group = 'YOUR_ASSIGNMENT_GROUP_SYS_ID'; // Replace with your assignment group sys_id
    scTask.short_description = 'Task for RITM: ' + ritmGr.number; // Custom short description
    scTask.insert(); // Insert the new sc_task record
}

// Optionally, log the result
gs.info('SC Tasks created for the specified requested items.');

 

If you need any further assistance or have specific requirements, feel free to ask!

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.


Regards,
Aniket