Copy Attachment from interaction to case

is_12
Tera Contributor

Hi Community,

 

I need to copy attachment from interaction table to the case table

So I have written one after BR on the interaction related record, in this table only we have the linking for both interaction

is_12_0-1749821921652.png

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

    // Add your code here

    gs.info("HelloAttachment1test");

    var attachment = new GlideSysAttachment();
    gs.info("HelloAttachment2test");
    attachment.copy('interaction', current.interaction.sys_id, 'case table', current.task.sys_id);
    attachment.update();


})(current, previous);
 
BR is not getting triggered
 
Thanks,
 
 
4 ACCEPTED SOLUTIONS

danmjunqueira
Kilo Guru

Here's why your Business Rule is not working and how to fix it:

Business Rule not triggering
If the Business Rule is set on a related record table and that table is not being updated when the interaction is linked to a case, then the rule won't fire. Make sure the rule is set on the correct table and event. Use conditions like "after update" where current.task or current.case becomes populated.

No need to call attachment.update()
The method GlideSysAttachment.copy() already performs the copy. There is no need to call update(), and doing so can cause issues because attachment is not a GlideRecord—it is a helper class.

Correct usage of table names
Replace 'case table' with the exact table name of your case, such as sn_customerservice_case or sn_hr_core_case.

Updated example script:

This should go in an after update Business Rule on the interaction table, assuming the case reference (task) is being updated:

(function executeRule(current, previous) {
if (!previous.task && current.task) {
var attachment = new GlideSysAttachment();
attachment.copy('interaction', current.sys_id, 'sn_customerservice_case', current.task.sys_id);
}
})(current, previous);





This checks that the case was just linked and then copies attachments from the interaction to the case.

If you're still not seeing the rule trigger, check the following:

Is the task field actually being set during the update?

Is the BR active and set to run "after update"?

Are there attachments on the interaction?

Does the user running the action have access to both tables?

 

View solution in original post

Roshnee Dash
Tera Guru

You can trigger this script via a Flow DesignerBusiness Rule depending on your workflow

Option 1: Business Rule

Use this if you want the script to run automatically when a record is updated or inserted.

Steps:

  1. Go to System Definition > Business Rules.
  2. Click New.
  3. Fill in:
    • Name: e.g., Copy Attachments to Case
    • Table: Interaction (or your custom interaction table)
    • When to run: after update or insert
    • Condition: e.g., when a Case is linked or
       current.case.changes() && current.case // assuming 'case' is a reference field
  4. In the Script field, paste the script and replace the sourceSysId and targetSysId with dynamic values like:

    Option 2: Flow Designer

    Use this if you prefer a low-code/no-code approach.

    Steps:

    1. Go to Flow Designer.
    2. Create a new Flow:
      • Trigger: Record created or updated on Interaction
      • Condition: Case is linked or not empty
    3. Add an Action:
      • Choose Script.
      • Paste the script and use inputs to pass sourceSysId and targetSysId.

Script:

(function execute(inputs, outputs) {
    var sourceTable = 'interaction'; // or 'sn_customerservice_case_interaction'
    var targetTable = 'sn_customerservice_case';
    var sourceSysId = 'SOURCE_INTERACTION_SYS_ID'; // Replace with actual Interaction sys_id
    var targetSysId = 'TARGET_CASE_SYS_ID'; // Replace with actual Case sys_id

    var attachmentGR = new GlideRecord('sys_attachment');
    attachmentGR.addQuery('table_name', sourceTable);
    attachmentGR.addQuery('table_sys_id', sourceSysId);
    attachmentGR.query();

    while (attachmentGR.next()) {
        var sa = new GlideSysAttachment();
        var attachmentData = sa.getBytes(attachmentGR);
        sa.write(targetTable, targetSysId, attachmentGR.file_name.toString(), attachmentGR.content_type.toString(), attachmentData);
    }
})();



Your feedback makes the community stronger! If you found this helpful, marking it as the correct answer helps others.
Stay awesome,
Roshnee Dash

View solution in original post

@is_12 

when case is created from interaction a record gets inserted into "interaction_related_record" table.

you should have after insert business rule on that table

(function executeRule(current, previous) {

    var attachment = new GlideSysAttachment();
    attachment.copy('interaction', current.interaction, 'sn_customerservice_case', current.task);

})(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

View solution in original post

@is_12 

Hope you are doing good.

Did my reply answer your question?

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

13 REPLIES 13

danmjunqueira
Kilo Guru

Here's why your Business Rule is not working and how to fix it:

Business Rule not triggering
If the Business Rule is set on a related record table and that table is not being updated when the interaction is linked to a case, then the rule won't fire. Make sure the rule is set on the correct table and event. Use conditions like "after update" where current.task or current.case becomes populated.

No need to call attachment.update()
The method GlideSysAttachment.copy() already performs the copy. There is no need to call update(), and doing so can cause issues because attachment is not a GlideRecord—it is a helper class.

Correct usage of table names
Replace 'case table' with the exact table name of your case, such as sn_customerservice_case or sn_hr_core_case.

Updated example script:

This should go in an after update Business Rule on the interaction table, assuming the case reference (task) is being updated:

(function executeRule(current, previous) {
if (!previous.task && current.task) {
var attachment = new GlideSysAttachment();
attachment.copy('interaction', current.sys_id, 'sn_customerservice_case', current.task.sys_id);
}
})(current, previous);





This checks that the case was just linked and then copies attachments from the interaction to the case.

If you're still not seeing the rule trigger, check the following:

Is the task field actually being set during the update?

Is the BR active and set to run "after update"?

Are there attachments on the interaction?

Does the user running the action have access to both tables?

 

@danmjunqueira Hey Hi !!

I have done all the mentioned things above, once case is created from the interaction. it is coming up in the related 

But the BR is not getting triggered.

 

 

is_12
Tera Contributor

@Roshnee Dash @Ankur Bawiskar @Roshnee Dash @Bert_c1 @danmjunqueira 

 

Thanks everyone, for your response

 

Ankur Bawiskar
Tera Patron
Tera Patron

@is_12 

when the file should be copied?

do you require when case is created from interaction?

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