ARTICLE: Sync Attachments bidirectionally between two records of different table.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2023 09:15 AM - edited 03-29-2024 04:55 AM
Scenario: Whenever any Attachments gets added, removed from SCTASK record it should Sync to Story record and vise versa.
Cons: We need to create a custom string field in both the tables.
1) Sync Attachments from SCTASK to Story:
Actions:
- Get Attachments from Trigger -> Catalog Task
- Get Attachments from Trigger -> Catalog Task -> Agile Story
- Loop through each attachments of Agile Story
- LookUp sys_attachment table where Hash is 3-> Attachment record -> Hash and Table sys ID is Catalog task sysid
- If lookup count is less than 1
- Delete attachment from Trigger -> Catalog Task -> Agile Story where File name is 3 -> Attachment record -> File name
- Loop through each attachments of Catalog Task
- LookUp sys_attachment table where Hash is 7-> Attachment record -> Hash and Table sys ID is Agile story sysid
- If lookup count is less than 1
- Copy attachment 7-> Attachment record to Trigger -> Catalog Task -> Agile Story
2) Sync Attachments from Story to SCTASK:
Actions:
- LookUp Catalog Task with Agile story sysid
- Get Attachments from Trigger -> Story
- Get Attachments from 1 -> Catalog Task
- Loop through each attachments of Catalog Task
- LookUp sys_attachment table where Hash is 4-> Attachment record -> Hash and Table sys ID is Story sysid
- If lookup count is less than 1
- Delete attachment from 1-> Catalog Task where File name is 4-> Attachment record -> File name
- Loop through each attachments of Story
- LookUp sys_attachment table where Hash is 8-> Attachment record -> Hash and Table sys ID is 1-> Catalog Task sysid
- If lookup count is less than 1
- Copy attachment 8-> Attachment record to 1-> Catalog Task
3) Create a new String field in both the tables and set Default value as 0. In this example it is named as u_test.
4) Create a Business Rule on Attachment [sys_attachment] table:
Before Insert, Update, Delete
Filter Condition: Table name is sc_task OR Table name is rm_story
(function executeRule(current, previous /*null when async*/ ) {
if (current.table_name='sc_task'){
var g = new GlideRecord('sc_task');
if (g.get(current.table_sys_id)) {
g.u_test = parseInt(g.u_test)+1;
g.update();
}}
else if (current.table_name='rm_story'){
var gr = new GlideRecord('rm_story');
if (gr.get(current.table_sys_id)) {
gr.u_test = parseInt(g.u_test)+1;
gr.update();
}}
})(current, previous);
(Note: This is done as adding or removing attachment from any record does not update the record itself, hence the flow will not get triggered, to trigger the flow this BR is written so that it updates the record if any attachment is added or removed from that record)