Unable to copy attachment from RITM variable to RITM itself
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-01-2024 02:40 PM
Hi Community,
I have created a attachment variable for catalog item. When I submit the request by attaching a file to variable it is getting stored in sys_attachment table with table name as ZZ_YYsc_cart_item and table sys_id as catalog item sys_id.
I want to copy this same attachment from variable to RITM record itself and as well as to a email notification. But i am unable to Query my RITM record from sys_attachment table and copy the attachment.
Any suggestions would really help me!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-01-2024 04:50 PM
The table name and table sys_id should update to the RITM after the request is submitted. Do you see the attachment in the activity filter of the RITM? Are you attaching the file in an MRVS or Order Guide?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-01-2024 08:18 PM
Hello @Haritej
Once the RITM is created the attachment in ritm variables is stored in sys_attachment table with table name as ZZ_YYsc_req_item and table sys_id as RITM sys_id.
To add the same attachment in RITM, create a new business rule.
Configure the Business Rule:
- Name: Give your business rule a descriptive name (e.g., "Copy Attachments to RITM").
- Table: Set this to Requested Item [sc_req_item].
- When: Set this to after.
- Insert: Check this option (so it runs after the RITM record is created
In the Script field, you can use the following code snippet:
(function executeRule(current, previous /*null when async*/) {
// Query attachments associated with the catalog item
var attachmentGR = new GlideRecord('sys_attachment');
attachmentGR.addQuery('table_name', 'ZZ_YYsc_req_item'); // Your attachment table
attachmentGR.addQuery('table_sys_id', current.sys_id); // Your catalog item sys_id
attachmentGR.query();
// Loop through the attachments and copy them to the RITM
while (attachmentGR.next()) {
var attachment = new GlideRecord('sys_attachment');
attachment.initialize();
attachment.file_name = attachmentGR.file_name;
attachment.content_type = attachmentGR.content_type;
attachment.table_name = 'sc_req_item'; // Set to RITM table
attachment.table_sys_id = current.sys_id; // Set to current RITM sys_id
attachment.insert(); // Insert the attachment into RITM
}
})(current, previous);To add the attachment in email notification, create new notification.
Set the Notification Conditions:
- Define the Table (e.g., sc_req_item for RITM).
- Set conditions for when the notification should be sent.
- Configure who will receive.
Next step is to create a new email script(sys_script_email).
In the Script field, you can use the following code snippet:
(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */
email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */
event) {
// Add your code here
var attachmentGR = new GlideRecord('sys_attachment');
attachmentGR.addQuery('table_name', 'sc_req_item');
attachmentGR.addQuery('table_sys_id', current.sys_id);
attachmentGR.query();
var attachments = '';
while (attachmentGR.next()) {
var attachmentLink = attachmentGR.getLink();
attachments += '<br><a href="' + attachmentLink + '">' + attachmentGR.file_name + '</a>';
}
// Add the attachment links to the email body
email.setBody('<br><strong>Attachments:</strong>' + attachments);
})(current, template, email, email_action, event)Call the script include in notification body:
Check for the attachment in email body section once is notification is sent.
"If you found my answer helpful, please give it a like and mark it as the accepted solution. It helps others find the solution more easily and supports the community!"
Thank You
Juhi Poddar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2024 03:53 AM - edited 10-03-2024 03:55 AM
Hi @Haritej
As a low code solution -
You can create a flow that will be triggered when one ritm is created for your catalog item;
and in the flow you can use copy attachment action .
You can utilize the same flow functionality to send mail and add your attachment there as well .
refer to below doc : https://docs.servicenow.com/csh?topicname=copy-attachment-flow-designer.html&version=latest
Kindly mark the reply as a correct answer / helpful if it helped to solve your question.
Thanks,
Anwesha
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2024 12:45 AM - edited 10-23-2024 05:06 AM
To copy attachments from your catalog item variable to the RITM record, use a GlideRecord query on the sys_attachment table to fetch the relevant attachment. Then, utilize the GlideSysAttachment class to copy the attachment to the RITM record. You can also include the attachment in your email notifications using the same method. Writing a PhD thesis can be a daunting task, and I was struggling to keep up with all the requirements. A friend suggested I try this link here
