copy attachment variable from catalog item to any table fields
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-08-2024 11:27 PM - edited ‎07-10-2024 05:10 AM
I have a catalog item in ServiceNow with an attachment field named image. I also have a target table with an attachment field named picture. How can I automatically copy any attachment added to the image field of the catalog item to the picture field of the corresponding record in the target table using a workflow and GlideRecord in ServiceNow
how can i achieve this requirement please help ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-09-2024 02:30 AM
Copying attachments is possible with GlideSysAttachment API. Here is an example:
var attachment = new GlideSysAttachment();
attachment.copy('source_table_name', 'source_record_id', 'target_table_name', 'target_record_id');
Note that it copies all attachments from the source record to the target record. Unfortunately, GlideSysAttachment has no method for copying a single attachment.
Blog: https://sys.properties | Telegram: https://t.me/sys_properties | LinkedIn: https://www.linkedin.com/in/slava-savitsky/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-09-2024 03:25 AM
Hello @sanjeet1245 ,
Create a new After Insert and Update Business Rule on your required catalog item table:
(function executeRule(current, previous /*null when async*/) {
// Define the target table and its attachment field
var targetTable = 'target_table'; // Replace with your target table name
var targetField = 'picture'; // Replace with your target field name
// Retrieve attachments from the catalog item
var attachmentGR = new GlideRecord('sys_attachment');
attachmentGR.addQuery('table_sys_id', current.sys_id);
attachmentGR.addQuery('table_name', current.getTableName());
attachmentGR.query();
while (attachmentGR.next()) {
// Read the attachment data
var attachmentData = new GlideSysAttachment().getBytes(attachmentGR);
// Create a new attachment for the target record
var targetRecordGR = new GlideRecord(targetTable);
targetRecordGR.get(current.target_record_sys_id); // Adjust this line to fetch your target record
new GlideSysAttachment().write(targetTable, targetRecordGR.sys_id, attachmentGR.file_name, attachmentData);
}
})(current, previous);
Thank you!!
Dnyaneshwaree Satpute
Tera Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-10-2024 03:23 AM - edited ‎07-10-2024 03:25 AM
I have created a catalog item with an attachment field variable. When I upload an attachment, I want it to be added to an image type field in a table. I've written a workflow run script for this, but when records are inserted, the attachment isn't being added to the image field of that table."
// Querying sys_attachment records related to the current item
var gr = new GlideRecord("sys_attachment");
gr.addQuery("table_name", "ZZ_YY" + 'sc_req_item');
gr.addQuery("table_sys_id", current.sys_id);
gr.query();
if (gr.next()) {
gr.table_name = "sc_req_item";
gr.update();
// Copy the attachment to the 'picture' field in abcd table
new GlideSysAttachment().copy( "sc_req_item", gr.sys_id, 'abcdtable', 'picture');
}