Copy Attachments Child to Parent Security Incident and vice versa to ensure synced attachments

WazzaJC
Tera Expert

Copy Attachments Child to Parent Security Incident and vice versa to ensure synced attachments

 

Dear ServiceNow Community Colleagues,

 

Please can I request help/guidance with the following use case requirement:

 

What is the best configuration I can use to ensure Attachments are copied from Child Security Incident to Parent Security Incident and vice versa? (Table: 'sn_si_incident')

 

I basically need to ensure all related 'Attachments' are always in sync, for related records, so that whether you view the attachments on the child or related/linked parent Security Incident, all related attachments will be visible and in sync on both the child and parent record.

 

Is this possible via the combination of a scripted Business Rule and Flow/Subflow for example?

 

I would appreciate any advice/guidance/working example of how best to achieve this configuration.

 

Many Thanks.

3 REPLIES 3

G Ponsekar
Giga Guru

Hi @WazzaJC ,

 

You will need to create two Async Business Rules on the sys_attachment table.
 
Business Rule 1: Copy from child to parent
  1. Create a new Async Business Rule on the Attachment [sys_attachment] table.
  2. Name the rule Sync Attachments Child to Parent - SI.
  3. Set the rule to run on Insert when Table name is Security Incident.
  4. The script will retrieve the child incident, check if it has a parent, and then use GlideSysAttachment.copy() to copy the attachment from the child to the parent. 
 
Business Rule 2: Copy from parent to all children
  1. Create another new Async Business Rule on the Attachment [sys_attachment] table.
  2. Name the rule Sync Attachments Parent to Children - SI.
  3. Set the rule to run on Insert when Table name is Security Incident.
  4. The script will find all child incidents related to the parent and then use GlideSysAttachment.copy() within a loop to copy the attachment from the parent to each child. 

 

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!

 

Thanks, GP

 

vignesh parthib
Tera Expert

Hi @WazzaJC 

 

Use a Business Rule on sys_attachment to detect when a file is added or removed from a Security Incident, and then:

  • Copy it to the parent/child if it doesn't already exist there.
  • Avoid creating duplicates.
  • Avoid infinite loop by checking a custom flag or metadata.

Business Rule: Sync Attachments between SI Parent/Child

  • Table: sys_attachment
  • When: after
  • Insert: Enabled
  • Condition: current.table_name == 'sn_si_incident'

(function executeRule(current, previous /*null on insert*/) {

    var incidentGR = new GlideRecord('sn_si_incident');

    if (!incidentGR.get(current.table_sys_id)) {

        return;

    }

 

    // Skip if already marked as copied (to avoid loops)

    if (current.file_name.endsWith('__copied')) {

        return;

    }

 

    var isParent = incidentGR.parent.nil(); // If parent is empty, this is a parent

    var targets = [];

 

    if (isParent) {

        // Find all children

        var childGR = new GlideRecord('sn_si_incident');

        childGR.addQuery('parent', incidentGR.sys_id);

        childGR.query();

        while (childGR.next()) {

            targets.push(childGR.sys_id.toString());

        }

    } else {

        // Has a parent

        targets.push(incidentGR.parent.toString());

    }

 

    // Copy attachment to each target record

    for (var i = 0; i < targets.length; i++) {

        var copied = new GlideSysAttachment();

        var newName = current.file_name + '__copied'; // Add suffix to prevent loops

        copied.copy(

            current.table_name, current.table_sys_id,  current.table_name, targets[i], newName );

    }

 

})(current, previous);

Thanks,
Vignesh
"If this solution resolves your issue, kindly mark it as correct."

 

Ankur Bawiskar
Tera Patron
Tera Patron

@WazzaJC 

you should not keep on syncing attachments between Parent and Child as it will lead to increase in attachment table size.

Instead why not use Related list feature to show attachments

check this

TNT: "Related Attachments" Related List 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader