Copy SCTASK 1 vaules to SCTASK 2 and SCTASK 3

shruthisx
Tera Contributor

Hi Community,

 

I have created few custom fields on catalog task form which includes choices, attachment and few multi line fields, now I have a requirement to copy the values the user entered on SCTASK 1 should be copied to SCTASK 2 and SCTASK 3, all these 3 tasks are created for same catalog item.

 

A solution would be helpful.

Thanks in Advance,

Shruthi S

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@shruthisx 

you can use before insert business rule on sc_task

Condition: current.request_item.cat_item.name == 'Your Item Name Here'

Script:

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
    var previousTask = new GlideRecord('sc_task');
    previousTask.orderBy('sys_created_on'); // ascending to pick the 1st task
    previousTask.addQuery('request_item', current.request_item);
    previousTask.setLimit(1);
    previousTask.query();
    if (previousTask.next()) {
        current.u_field1 = previousTask.u_field1;
        current.u_choiceField = previousTask.u_choiceField;
        // do for other fields as well
    }

})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

7 REPLIES 7

Robert H
Mega Sage

Hello @shruthisx ,

 

If you want to do this for all Catalog Tasks, regardless of the Catalog Item they belongs to, then you can create a Business Rule on the sc_task table (type: before insert) with the following script:

 

var customFields = [
    'CUSTOM_FIELD_NAME_1',
    'CUSTOM_FIELD_NAME_2',
    'CUSTOM_FIELD_NAME_3'
];

var grTask = new GlideRecord('sc_task');
grTask.addQuery('request_item', current.getValue('request_item'));
grTask.addQuery('sys_id', '!=', current.getUniqueValue());
grTask.orderBy('sys_created_on');
grTask.setLimit(1);
grTask.query();
if (grTask.next()) {
    customFields.forEach(field => current.setValue(field, grTask.getValue(field)));
}

 

If this logic is only required for a certain Catalog Item then add a Filter condition to the Business Rule, e.g.

Request Item.Item = {your item name}.

 

Regards,

Robert

mohdarbaz
Kilo Guru

@shruthisx .

try this:

mohdarbazma_0-1746695922307.png

mohdarbazma_1-1746695962303.png

If my response helped please mark it correct and close the thread so that it benefits future readers.

 

Regards,

Mohd Arbaz.

 

Daniel Oderbolz
Kilo Sage

Dear @shruthisx 

 

Honestly, I would not do this, unless you have a real good reason.

The way to go IMHO is to use the variables you have on the RITM. 

It is not recommended to add custom fields to sc_task because that is exactly with the Variables are for.

This way, all the fulfillers have a consistent view on those values. If you create a copy of these values at some point in time, the values might change on the individual tasks which might lead to issues down the line.

That is also the reason why UI Policies on the Catalog have settings to control if they apply on tasks or not.

Can you tell us more about the actual use case?

Best
Daniel


If this answer was helpful, I would appreciate if you marked it as such - thanks!

Best
Daniel