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

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

@Ankur Bawiskar Thank you, it's working like a charm😊😊

Hello @shruthisx ,

 

You should also add a line to prevent that the Business Rule updates the first task with its own data, as shown in this solution.

 

grTask.addQuery('sys_id', '!=', current.getUniqueValue());

 

Regards,

Robert

pratikjagtap
Giga Guru

Hi @shruthisx ,

 

Step 1: Script Include — CopySCTaskFields
Go to System Definition > Script Includes → New:

 

var CopySCTaskFields = Class.create();
CopySCTaskFields.prototype = {
initialize: function() {},

copyFieldsFromFirstTask: function(currentTask) {
if (!currentTask || !currentTask.request_item) {
return;
}

// Find the first task for the same RITM
var firstTaskGR = new GlideRecord('sc_task');
firstTaskGR.addQuery('request_item', currentTask.request_item);
firstTaskGR.addQuery('sys_id', '!=', currentTask.sys_id); // Exclude self
firstTaskGR.orderBy('sys_created_on');
firstTaskGR.setLimit(1);
firstTaskGR.query();

if (firstTaskGR.next()) {
// Copy your custom fields here
currentTask.u_choice_field = firstTaskGR.u_choice_field;
currentTask.u_multiline_field = firstTaskGR.u_multiline_field;
currentTask.u_text_field = firstTaskGR.u_text_field;
// ... add more as needed

// Save the updated fields
currentTask.update();

// Copy attachments
var attachment = new GlideSysAttachment();
attachment.copy('sc_task', firstTaskGR.sys_id, 'sc_task', currentTask.sys_id);
}
},

type: 'CopySCTaskFields'
};

 

Step 2: Business Rule — Copy Custom Fields from First SCTASK
Go to System Definition > Business Rules → New:

  • Table: sc_task
  • When: After insert
  • Conditions: Optional (e.g., [Request Item → Catalog Item] is specific item)
  • Script tab:

   (function executeRule(current, gsn) {
var copier = new CopySCTaskFields();
copier.copyFieldsFromFirstTask(current);
})(current, gsn);

 

If my response helped, please hit the ‌‌Thumb Icon and accept the solution so that it benefits future readers.

 

Regards,
Pratik