- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-23-2022 10:31 AM
Hi all,
I followed this post and have a business rule that is copying attachments from Task to RITM. It copies all attachments from Task to RITM each time a new attachment is added to Task. For my scenario, I would only like the newest attachment to be copied over. So currently for example if I add "Attach1.pdf" file to Task, it copies over to the RITM, but if I add a newer "Attach2.pdf" to the Task, both the previous "Attach1.pdf" and new "Attach2.pdf" are copied over to the RITM, creating duplicate of "Attach1.pdf". Is there any way to have only the newest attachment get copied over from Task to RITM?
Below is the current script I am using in the BR:
var taskRec = new GlideRecord('sc_task');
taskRec.addQuery('sys_id', current.table_sys_id);
taskRec.query();
if(taskRec.next()){
var ritmRec = new GlideRecord('sc_req_item');
ritmRec.addQuery('sys_id',taskRec.request_item);
ritmRec.query();
if(ritmRec.next()){
GlideSysAttachment.copy('sc_task', current.table_sys_id, 'sc_req_item', ritmRec.sys_id);
}
}
Anyone have any script to help me out? Thanks in advance for any help.
-Chris
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-23-2022 12:57 PM
Hi Chris,
You can try the following script.
var taskRec = new GlideRecord('sc_task');
taskRec.addQuery('sys_id', current.table_sys_id);
taskRec.query();
if (taskRec.next()) {
var attachment = new GlideRecord('sys_attachment');
attachment.addQuery('table_sys_id', current.table_sys_id);
attachment.orderByDesc('sys_created_on');
attachment.query();
if (attachment.next()) {
var ritmRec = new GlideRecord('sc_req_item');
ritmRec.addQuery('sys_id', taskRec.request_item);
ritmRec.query();
if (ritmRec.next()) {
var gsa = new GlideSysAttachment();
gsa.writeContentStream(
ritmRec,
attachment.file_name,
attachment.content_type,
gsa.getContentStream(attachment.sys_id));
}
}
}
Hopefully, this will resolve your query.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-25-2022 07:25 AM
Thank you Muhammad! I am testing out the script you provided and it looks like it's what I needed for my scenario. Will mark as correct soon.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-25-2022 08:10 AM
I marked your answer as correct Muhammad, thanks again. I am new to scripting, could you educate me a little about what the script is doing :)? Is it copying ALL attachments on the Task to the RITM? It seems like it is only copying the new attachment on the Task to the RITM, which is what I want, just want to understand better. I see you use this line below which seems key to making it work.
attachment.orderByDesc('sys_created_on');
Thanks,
Chris
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-25-2022 09:10 AM
Appreciated, orderByDesc('sys_created_on') basically reorders the found attachments in the descending order. See the below images for reference.
Ascending Order Descending Order
Try reading comments in the below script for better understanding.
/*
After an attachment record is created, then querying the Task table to find the actual task
on which the attachment has been added.
*/
var taskRec = new GlideRecord('sc_task');
taskRec.addQuery('sys_id', current.table_sys_id); // Query/Find task which has this new attachment.
taskRec.query();
/*
If a task record is found on which the attachment has been added, then querying the attachments table to find all the attachments for this task, and ordering the found attachments in descending order (Means latest attachment will be available first e.g. we found 3 attachments as below
1- Attached on 2022-10-10 10:10:10 'yyyy-MM-dd hh:mm:ss'
2- Attached on 2022-10-10 10:20:10
3- Attached on 2022-10-11 10:10:10
After attachment.orderByDesc('sys_created_on') the retrieved attachments will be in the below order
1- Attached on 2022-10-11 10:10:10
2- Attached on 2022-10-10 10:20:10
3- Attached on 2022-10-10 10:10:10 'yyyy-MM-dd hh:mm:ss')
based on "sys_created_on" field.
*/
if (taskRec.next()) { // If a task which has this new attachment found, then proceed.
var attachment = new GlideRecord('sys_attachment');
attachment.addQuery('table_sys_id', current.table_sys_id); // Query all attachments which were/are attached to task record.
attachment.orderByDesc('sys_created_on'); // Reorder to make the latest attached attachment be appear/available first.
attachment.query();
/*
if an attachment (it will be latest attachment) is found, then querying RITM table to find a requested item which has the task (Which we found above) attached.
*/
if (attachment.next()) { // Using If rather than While because we only want the first attachment from the list of all found attachments.
var ritmRec = new GlideRecord('sc_req_item');
ritmRec.addQuery('sys_id', taskRec.request_item); // Query RITM which has the task.
ritmRec.query();
/*
if a requested item (which has the task attached) is found, then writing/creating a new record in Attachments table (Basically using the latest task attachment to attach it to the found RITM).
*/
if (ritmRec.next()) { // If RITM is found, then proceed.
var gsa = new GlideSysAttachment();
gsa.writeContentStream( // Write/Create new attachment record in Attachments table.
ritmRec, // RITM record to which this attachment will be attached.
attachment.file_name, // File name of the attachment to be attached.
attachment.content_type, // Content type of the attachment to be attached.
gsa.getContentStream(attachment.sys_id) // Write the content of the attachment.
);
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-25-2022 02:42 PM
Thanks again Muhammad! Your commented code is very informative.