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.

Copy an attachment from RITM to Sc Task and Vice Versa

Sharan Ellendul
Tera Contributor

I have a requirement which should automatically attach an attachment to all sc tasks of an ritm and vice versa

3 REPLIES 3

Samaksh Wani
Giga Sage

Hello @Sharan Ellendul 

 

Plz go through the Below link :-

https://www.servicenow.com/community/now-platform-forum/copy-attachment-from-ritm-to-sc-task-on-requ...

 

 

Plz Mark my Solution as Accept and Give me thumbs up, if you find it Helpful.

 

Regards,

Samaksh

 

Sohail Khilji
Kilo Patron

Hi @Sharan Ellendul ,

 

Please find the below script which will help you...

 

 

Copy RITM Attachments to task

(function executeRule(current, previous /*null when async*/ ) {
var flag = 0;
var reqtask1 = new GlideRecord('sc_task');
reqtask1.addQuery('request_item', current.table_sys_id);
reqtask1.query();
while (reqtask1.next()) {

var canAttach = new GlideRecord('sys_attachment');
canAttach.addEncodedQuery('UPDATE_QUERY_HERE');
canAttach.query();
if (canAttach.next()) {
flag = 1;
}
}

if (flag == 0) {
var reqtask = new GlideRecord('sc_task');
reqtask.addQuery('request_item', current.table_sys_id);
reqtask.query();
while (reqtask.next()) {
var att = new GlideRecord('sys_attachment');
att.addQuery('table_sys_id', reqtask.sys_id);
att.query();
while (att.next()) {
att.deleteRecord();
}
GlideSysAttachment.copy('sc_req_item', current.table_sys_id, 'sc_task', reqtask.sys_id);
}
}
})(current, previous);

Copy Task Attachments to RITM

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

var flag = 0;
var reqItem1 = new GlideRecord('sc_task');
if (reqItem1.get(current.table_sys_id)) {

var canAttach = new GlideRecord('sys_attachment');
canAttach.addEncodedQuery('UPDATE_QUERY_HERE');
canAttach.query();
if (canAttach.next()) {
flag = 1;
}
}

if (flag == 0) {

var reqItem = new GlideRecord('sc_task');
if (reqItem.get(current.table_sys_id)) {
var att = new GlideRecord('sys_attachment');
att.addQuery('table_sys_id', reqItem.request_item.sys_id);
att.query();
while (att.next()) {
att.deleteRecord();
}
GlideSysAttachment.copy('sc_task', current.table_sys_id, 'sc_req_item', reqItem.request_item.sys_id);
//att.setWorkflow(false);
}
}
})(current, previous);

 

 

I HOPE IT HELPS....

 

 


☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect

Hi Sohail,

 

Where would I put this script to get it to work?

 

Regards

Mike