Create new Catalog task and copy all task variables using script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-03-2024 06:11 AM
I want to create new catalog task for another team using UI action on catalog task form. On click of this UI action, I will run a script create new catalog task under same RITM and want to copy all RITM variable present on task.
I am able to create new catalog task using GlideRecord API but it is not copying the variables.
Please guide, how can I achieve this.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-05-2024 12:20 AM
@Arshdeep11 Please refer the below links for reference,
Please mark this as helpful and accept it as a solution if this resolves your query.
Thanks,
Sujatha V.M.
Sujatha V.M.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-05-2024 12:56 AM
Hi @Arshdeep11 ,
You need to first query the table "item_option_new" using the catalog item reference to get all the variables associated with that catalog item.
Then you need to query the table "sc_item_variables_task" and there with the generated task number you can insert all the variables available from the above query.
Please mark helpful/correct if my response helped you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-05-2024 01:10 AM
Below is the sample script you can use:
// Step 1: Get the RITM associated with the current catalog task
var ritm = new GlideRecord('sc_req_item');
if (ritm.get(current.request_item)) {
// Step 2: Get all variables associated with the RITM
var ritmVariables = new GlideRecord('sc_item_option_mtom');
ritmVariables.addQuery('request_item', ritm.sys_id);
ritmVariables.query();
// Step 3: Create a new catalog task under the same RITM
var newTask = new GlideRecord('sc_task');
newTask.initialize(); // Initialize a new record
newTask.request_item = ritm.sys_id; // Set the RITM for the new task
// Set other fields for the new task as needed
newTask.short_description = 'Your Short Description Here';
newTask.assigned_to = 'Your Assigned To User or Group';
newTask.insert(); // Insert the new catalog task
// Step 4: Copy all variables from the RITM to the new task
while (ritmVariables.next()) {
var newTaskVariable = new GlideRecord('sc_task_mtom');
newTaskVariable.initialize(); // Initialize a new record
newTaskVariable.task = newTask.sys_id; // Set the new task
newTaskVariable.item_option_new = ritmVariables.item_option_new; // Copy variable details
newTaskVariable.value = ritmVariables.value; // Copy variable value
newTaskVariable.insert(); // Insert the variable into the new task
}
// Optionally, redirect the user to the newly created task
action.setRedirectURL(newTask);
} else {
gs.addErrorMessage('Error: RITM not found.');
}
Please Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Thanks