Create new Catalog task and copy all task variables using script

Arshdeep11
Tera Contributor

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.

3 REPLIES 3

Sujatha V M
Kilo Patron
Kilo Patron

@Arshdeep11 Please refer the below links for reference, 

 

https://www.servicenow.com/community/itsm-forum/to-copy-all-variables-information-in-catalog-task-de...

 

https://www.servicenow.com/community/developer-forum/copying-data-from-one-catalog-task-to-another-c...

 

Please mark this as helpful and accept it as a solution if this resolves your query.

Thanks,

Sujatha V.M.

Please mark this as helpful and accept it as a solution if this resolves your query.
Sujatha V.M.

Anubhav24
Mega Sage
Mega Sage

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.

Maddysunil
Kilo Sage

@Arshdeep11 

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