Script/Business Rule to copy Attachments from Child Security Incident to Parent Security Incident

WazzaJC
Tera Expert

Script/Business Rule to copy Attachments from Child 'Security Incident' to Parent 'Security Incident'

 

Dear ServiceNow Community Colleagues, I would greatly appreciate your help/guidance on how to achieve the following.

 

I need to configure for my client, so that when a Child Security Incident is related/linked to a Parent Security Incident, that attachments that are on the child security incident record are copied across as attachments on the Parent Security Incident record.

 

Please could you let me know the Business Rule / Script that I need to achieve this, also minimising any performance issues when doing this. Does it need to be an Insert, Update or Async Business Rule? 

 

It is all related to 'Security Incident Response', so the table is 'sn_si_incident'. 

 

Both the child ticket and parent ticket would be Security Incident, on the same table: sn_si_incident

 

Thanks very much for your help.

 

Kind Regards.

 

2 ACCEPTED SOLUTIONS

Hi @WazzaJC ,

 

(function executeRule(current, previous /*null when async*/) {
    // Check if the record has a parent
    if (!current.parent_incident) {
        return;
    }

    // Get the parent Security Incident record
    var parent = new GlideRecord('sn_si_incident');
    if (!parent.get(current.parent_incident)) {
        return;
    }

    // Copy attachments from the child to the parent
    var attachments = new GlideRecord('sys_attachment');
    attachments.addQuery('table_name', 'sn_si_incident');
    attachments.addQuery('table_sys_id', current.sys_id);
    attachments.query();

    while (attachments.next()) {
        GlideSysAttachment.copy('sn_si_incident', current.sys_id, 'sn_si_incident', parent.sys_id);
    }
})(current, previous);

 

If you found my response helpful, please mark it as ‘Accept as Solution’ and ‘Helpful’. This helps other community members find the right answer more easily and supports the community.

 

Thanks and Regards,
Kaushal Kumar Jha - ServiceNow Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/

View solution in original post

@WazzaJC

the business rule is meant to work when you add file on Child, it will copy to parent

What's your business requirement?

1) when child incident is created from parent then file from Parent should be copied -> for this I already shared logic above

AnkurBawiskar_0-1756997747981.png

 


OR

2) when file is attached to child, add that same to parent -> for this I already shared logic above

AnkurBawiskar_1-1756997776092.png

 

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

View solution in original post

23 REPLIES 23

kaushal_snow
Mega Sage

Hi @WazzaJC ,

 

1.You need to create a Business Rule on the Child Table (sn_incident)

 

When: After

Insert: True

Update: True

Condition: Ensure the record has a parent (e.g., parent_incident is not empty).

 

(function executeRule(current, previous /*null when async*/) {
    // Check if the record has a parent
    if (!current.parent_incident) {
        return;
    }

    // Get the parent Security Incident record
    var parent = new GlideRecord('sn_incident');
    if (!parent.get(current.parent_incident)) {
        return;
    }

    // Copy attachments from the child to the parent
    var attachments = new GlideRecord('sys_attachment');
    attachments.addQuery('table_name', 'sn_incident');
    attachments.addQuery('table_sys_id', current.sys_id);
    attachments.query();

    while (attachments.next()) {
        GlideSysAttachment.copy('sn_incident', current.sys_id, 'sn_incident', parent.sys_id);
    }
})(current, previous);

 

If you found my response helpful, please mark it as ‘Accept as Solution’ and ‘Helpful’. This helps other community members find the right answer more easily and supports the community.

 

Thanks and Regards,
Kaushal Kumar Jha - ServiceNow Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/

Hi @kaushal_snow thanks so much. Sorry, I needed to correct the 'Table' name in my original post. The correct Table name is: 'sn_si_incident', my apologies. Would you be able to correct your script above, using my corrected Table name, then I believe your suggested solution will work for me and I will give it a try, many thanks 🙂

Hi @WazzaJC ,

 

(function executeRule(current, previous /*null when async*/) {
    // Check if the record has a parent
    if (!current.parent_incident) {
        return;
    }

    // Get the parent Security Incident record
    var parent = new GlideRecord('sn_si_incident');
    if (!parent.get(current.parent_incident)) {
        return;
    }

    // Copy attachments from the child to the parent
    var attachments = new GlideRecord('sys_attachment');
    attachments.addQuery('table_name', 'sn_si_incident');
    attachments.addQuery('table_sys_id', current.sys_id);
    attachments.query();

    while (attachments.next()) {
        GlideSysAttachment.copy('sn_si_incident', current.sys_id, 'sn_si_incident', parent.sys_id);
    }
})(current, previous);

 

If you found my response helpful, please mark it as ‘Accept as Solution’ and ‘Helpful’. This helps other community members find the right answer more easily and supports the community.

 

Thanks and Regards,
Kaushal Kumar Jha - ServiceNow Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/

G Ponsekar
Mega Guru

Hi @WazzaJC ,

 

The most efficient way to achieve this is to create an Async Business Rule on the sn_incident table. This approach minimizes performance impact by running the copy process in the background after the attachment is inserted.

 

Name: Copy Attachment to Parent Security Incident
Table: sn_incident
Advanced: true
When: async
Insert: true
Filter Condition: Parent is not empty and parent changes from empty

Script:

(function executeRule(current, previous /*null when async*/) {
     //make use of GlideSysAttachment.copy(sourceTable, sourceSysId, sourceTable, parentSysId);
 
})(current, previous);

 

 

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

 

Thanks, GP