Send Attachments from 1 record to another
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-17-2024 09:09 AM
Hi All,
I have a requirement where I need to send attachments from one record to another. For example, when I create an incident from a case, I want to transfer the attachments from the case to the incident at the time of incident creation or during updates. Additionally, if any attachments are added to the incident, they should be sent back to the case.
Are there any references or best practices to implement this?
Thank you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-17-2024 09:23 AM
I would suggest creating a Related List for attachments for Case and Incident that show the related attachments on the contravening record. That way you do not have to duplicate attachments and use up disk space unnecessarily, and it works really nice. Just go to the Attachments related list and see any attachments on the other record.
Here's info on creating custom related lists.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-17-2024 09:25 AM
Hi @Vamshi_ch123 ,
To transfer attachments between records in ServiceNow, you can use the `GlideSysAttachment` API. Here's a basic implementation to copy attachments from one record to another during incident creation or updates:
Step 1: Copy Attachments from Case to Incident
Use the `GlideSysAttachment.copy()` method to copy attachments from the case to the incident. You can do this during the incident creation process.
Example Business Rule (After Insert on Incident):
(function executeRule(current, previous /*null when async*/) {
var caseSysId = current.u_case; // Assuming you store the case reference in a custom field on the incident
if (caseSysId) {
var attachment = new GlideSysAttachment();
attachment.copy('sn_customerservice_case', caseSysId, 'incident', current.sys_id);
gs.addInfoMessage('Attachments copied from Case to Incident.');
}
})(current, previous);
Step 2: Sync Attachments from Incident to Case
To send attachments from the incident back to the case, you can implement a similar logic. Use a Business Rule (After Insert or After Update on `sys_attachment` table) to detect new attachments on the incident and copy them to the case.
Example Business Rule (After Insert on `sys_attachment`):
(function executeRule(current, previous /*null when async*/) {
// Get the parent record (incident or case)
var parentRecord = new GlideRecord('incident');
if (parentRecord.get(current.table_sys_id)) {
// Check if this incident has a case reference
var caseSysId = parentRecord.u_case;
if (caseSysId) {
// Copy attachment from the incident to the case
var attachment = new GlideSysAttachment();
attachment.copy('incident', parentRecord.sys_id, 'sn_customerservice_case', caseSysId);
gs.addInfoMessage('Attachments copied from Incident to Case.');
}
}
})(current, previous);
Best Practices:
1. Handle Duplicates: Ensure attachments aren't duplicated when syncing between records.
2. Performance: Be cautious with large attachments or a high volume of attachments as it could affect system performance.
3. Security: Ensure that attachment permissions are maintained when transferring between records.
Thanks & Regards,
Siddhesh Jadhav
If this resolves your query, please mark it as helpful and accept the solution.