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

@WazzaJC 

yes it should be after insert BR

The BR will copy files from parent to child only when child is getting created.

You should also update the parent field

(function executeRule(current, previous /*null when async*/ ) {
	
    new GlideSysAttachment().copy('sn_si_incident', current.parent_security_incident, 'sn_si_incident', current.sys_id);

})(current, previous);

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

Hi @Ankur Bawiskar thanks again. But it is Attachments on Child Record that I want to copy to Parent Record. Will your script work for copying Child Record attachments to Parent Record? Many thanks again Ankur.

@WazzaJC 

then in that case have after insert business rule on sys_attachment

Condition: current.table_name == 'sn_si_incident'

Script:

(function executeRule(current, previous /*null when async*/ ) {

    var rec = new GlideRecord('sn_si_incident');
    rec.addQuery('sys_id', current.table_sys_id);
    rec.addNotNullQuery('parent_security_incident');
    rec.query();
    if (rec.next()) {
        new GlideSysAttachment().copy('sn_si_incident', rec.sys_id, 'sn_si_incident', rec.parent_security_incident);
    }

})(current, previous);

Note: GlideSysAttachment copy will copy all files and not individual one which is attached, you will have to customize the above to handle that

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

Hi @Ankur Bawiskar  thanks very much. Just one more thing to check, Do I also in this case, still need to select a Condition that Parent Security Incident = Is Not Empty ? Or is this not required in this case? Thank You.

@WazzaJC 

the script already takes care of that.

It's querying only those incidents which have parent so that copy logic works.
if parent is empty then it won't copy as query won't satisfy.

I believe I have answered your original question and subsequent questions.

If my response helped please mark my response(s) as correct so that it benefits future readers.

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