Add attachtments added in a sctask to the RITM above instead of the SCtask itself

Jacob64
Kilo Guru
Hi all, We want to be able to easily see all related attachments on a RITM in the task. This at self we already managed but now when someone adds a new attachment to a task the attachment is at the task an not visible in the RITM above or other sctasks related to the same RITM. We can of course copy all attachements to the RITM above but then we have the attachment double. 1 on the task and 1 on the RITM. We can ask everybody never to add the attachment to the sctask but we are all humans and forget this all the time. So instead we think about not attaching the sctask itself but alway automatically place it directly to the RITM or move it directly after adding. Is this even possible and if so how can we do that? Many thanks in advance Kind Regards Jacob.
1 ACCEPTED SOLUTION

Rajdeep Ganguly
Mega Guru

Yes, it is possible to automatically move or copy attachments from a task to its parent RITM in ServiceNow. You can achieve this by creating a Business Rule or a Scripted REST API. Here are the steps to create a Business Rule: 1. Navigate to System Definition > Business Rules. 2. Click on New to create a new Business Rule. 3. Give it a meaningful name, for example, "Move Attachments to RITM". 4. Select the table as "sys_attachment" (This is the table where all attachments are stored). 5. In the "When to run" section, select "After" and "Insert". 6. In the "Advanced" tab, write a script to move the attachment from the task to the RITM. Here is a sample script: javascript (function executeRule(current, previous /*null when async*/) { var gr = new GlideRecord('sc_task'); if (gr.get('sys_id', current.table_sys_id)) { if (gr.request_item.nil()) { return; } current.table_name = 'sc_req_item'; current.table_sys_id = gr.request_item; current.update(); } })(current, previous); This script checks if the attachment is associated with a task. If it is, it moves the attachment to the associated RITM. Please note that this script is a simple example and may need to be adjusted to fit your specific needs. Always test scripts in a non-production environment before deploying them to production. Also, keep in mind that this will move the attachment from the task to the RITM, not copy it. If you want to keep a copy of the attachment on the task, you would need to modify the script to create a copy of the attachment instead of moving it.

View solution in original post

4 REPLIES 4

priyasunku
Kilo Sage

Hi @Jacob64 

 

you can follow the below link which describes the solution to similar requirement

 

Copy Attachment from RITM to SC Task on request submit and updates from portal. 

 

 

If my answer solved your issue, please mark my answer as Correct & 👍Helpful

Utpal Dutta
Tera Guru

Hi Jacob,

You can do it by changing the Table sys id of attachment which is record sys id of catalog task to RITM sys id. To do so you need to write a After or Async BR for catalog task table like below:

 

var grAttachment = new GlideRecord('sys_attachment');
grAttachment.addQuery('table_sys_id', current.getUniqueValue());
grAttachment.query();

while(grAttachment.next()){
grAttachment.setValue('table_sys_id', current.getValue('request_item'));
grAttachment.update();
}

 

This will update the Attachment record to display on RITM and not on Task. It won't keep the attachment at Task table and will only show in RITM record.

 

Note:

Since you're modifying an OOTB table record please make sure you have all the rights to do it. Open sys_attachment table and see if you're able to modify Table sys id column of any record.

Also this BR make take sometime to run depending upon the instance and records present in Attachment table. (May encounter performance issues)

 

If my answer helps then please mark it correct.

 

Thanks,

Utpal

Rajdeep Ganguly
Mega Guru

Yes, it is possible to automatically move or copy attachments from a task to its parent RITM in ServiceNow. You can achieve this by creating a Business Rule or a Scripted REST API. Here are the steps to create a Business Rule: 1. Navigate to System Definition > Business Rules. 2. Click on New to create a new Business Rule. 3. Give it a meaningful name, for example, "Move Attachments to RITM". 4. Select the table as "sys_attachment" (This is the table where all attachments are stored). 5. In the "When to run" section, select "After" and "Insert". 6. In the "Advanced" tab, write a script to move the attachment from the task to the RITM. Here is a sample script: javascript (function executeRule(current, previous /*null when async*/) { var gr = new GlideRecord('sc_task'); if (gr.get('sys_id', current.table_sys_id)) { if (gr.request_item.nil()) { return; } current.table_name = 'sc_req_item'; current.table_sys_id = gr.request_item; current.update(); } })(current, previous); This script checks if the attachment is associated with a task. If it is, it moves the attachment to the associated RITM. Please note that this script is a simple example and may need to be adjusted to fit your specific needs. Always test scripts in a non-production environment before deploying them to production. Also, keep in mind that this will move the attachment from the task to the RITM, not copy it. If you want to keep a copy of the attachment on the task, you would need to modify the script to create a copy of the attachment instead of moving it.

Jacob64
Kilo Guru

Dear all,

Tanks for your reactions sofar.
I read a lot of negative comments about copying files to the RITM above because that can cause performance problems. So that's not the first solution we want to use.
I also get the feeling that adjusting system properties changing the location is not good for the ootb situation.
So that is also not directly top of mind.

We do however use a related list to show the attachments from the RITM in the sctask and try to make a new one that does the other way around and showing the attachments of all related sctask to that RITM.

 

We once managed to create a query that gives us the right information. But as I'm not a developer at all it is not simple for me to get a working query.


With the following code i'm able to show all attachments related to a sctaks that is not only related to that specific RITM: 

(function refineQuery(current) {
 addQuery('table_sys_id' , parent.sctask);
})(current);
 
I've tried several variations like (current.parent) or just sctask instead of parent.stask etc but no difference at all.

When reading this you may think what a fool this can be done much easier like this or that. As I already mentioned I'm  not a developer at all so you're probably right to think the same.

But is you have a good working query for that please let me know!


Many thanks in advance!!
KR, Jacob52.