The CreatorCon Call for Content is officially open! Get started here.

Copy Attachments From Req to sc_task

Benjamin Charle
Tera Contributor

Hi,

I had a use case to create a catalog task from an email request that always generated attachments. I wanted the attachments to be put on to the task not left on the request. What I did was create a BR that updated the location of the attachments on the sys_attachment table. That way I was not creating duplicate attachments in the system. Here is the script I used which works if you set up a timer on the workflow:

 

(function executeRule(current, previous /*null when async*/ ) {
var att = new GlideRecord('sys_attachment');
att.addQuery('table_sys_id', current.request);
att.query();
while (att.next()) {
att.table_name = 'sc_task';
att.table_sys_id = current.sys_id;
att.update();
}
})(current, previous);

 

I am curious to see if there is any draw backs to doing it this way as I did not see any posts about this and I am new to SN?

 

Thanks,

 

Ben

 

 

3 REPLIES 3

SanjivMeher
Mega Patron
Mega Patron

You can create a Relationship record and show it in the catalog task as embedded list.

You can also check following few links, which has solution

https://www.servicenow.com/community/developer-forum/show-all-attachments-in-catalog-task/m-p/203352...

https://www.servicenow.com/community/it-service-management-forum/need-to-show-the-ritm-attachments-i...

https://www.servicenow.com/community/developer-forum/display-parent-attachments-in-sc-task/m-p/20212...


Please mark this response as correct or helpful if it assisted you with your question.

Shekhar Navhak1
Kilo Sage

Hi @Benjamin Charle ,

 

Try below code:

 

GlideSysAttachment.copy('sc_req_item', <sys_id of RITM>, 'sc_task', <sys_id of task>);

 

Please mark the answer correct/helpful based on Impact.
Regards, Shekhar

Community Alums
Not applicable

Hi @Benjamin Charle ,

 

It's recommended by ServiceNow that we should not be writing BR to copy attachments. Instead, we have to create relationship under system definition. 

 

I have done this for one of my use case, all you need to do amend the table names to achieve your requirement. 

 

below is the code snippet I used to achieve the requirement to show attachments attached in catalog task to RITM. As an additional, requirement I have considered Work order and Work order task. Obviously, you can do the vice versa as well by modifying the code.

 

1. First, I created a relationship under system definition. Here I have named the relationship as RITM attachments and selected Applies to table - sc_req_item and Queries from table - sys_attachment. This will act as the related list where you see all the attachments from Catalog task, work order/work order task. 

 

***Note - Here Applies to table is the Parent and the Query from table act as current. In case, if you select Applies to table as Global, then the Related list will be visible Globally.

 

GaganKris_0-1671922452810.png

 

 

Then, write the below code

 

(function refineQuery(current, parent) {

//Current record Attachments
var qc = current.addQuery('table_sys_id', parent.sys_id);

 

//Request Attachments
qc.addOrCondition('table_sys_id', parent.request.sys_id);

 

//Catalog Task Attachments
var tsk = new GlideRecord('sc_task');
tsk.addQuery('request_item', parent.sys_id);
tsk.query();
var tskIDArr = [];
while (tsk.next()) {
tskIDArr.push(tsk.sys_id.toString());
}
var tskIDStr = tskIDArr.join();
qc.addOrCondition('table_sys_id', 'IN', tskIDStr);

 

//Work Order Attachments
var woe = new GlideRecord('wm_order');
woe.addQuery('initiated_from', parent.sys_id);
woe.query();
var wskIDArr = [];
while (woe.next()) {
wskIDArr.push(woe.sys_id.toString());
}
var wskIDStr = wskIDArr.join();
qc.addOrCondition('table_sys_id', 'IN', wskIDStr);

 

//Work Order task Attachments
var woet = new GlideRecord('wm_task');
woet.addEncodedQuery('parent.ref_sm_order.initiated_from='+parent.sys_id);
woet.query();
var wstkIDArr = [];
while (woet.next()) {
wstkIDArr.push(woet.sys_id.toString());
}
var wstkIDStr = wstkIDArr.join();
qc.addOrCondition('table_sys_id', 'IN', wstkIDStr);

 

//Do not include attachments not associated with a record
current.addNotNullQuery('table_sys_id');


})(current, parent);

 

GaganKris_1-1671922452808.png

 

And this how the new related list created will have all the attachments. This will provide just the link to the attachments and will not be copying the attachments itself.

 

Please mark as helpful/click like button, if the solution was in line with your expectations.

 

Regards,

Gagan k