The CreatorCon Call for Content is officially open! Get started here.

attachment from related list table to parent table

Sweta24
Tera Contributor

hi all

 

we have one custom table under incident related list , we have business rules to update worknotes , requester  has to update for  customer table  from incident table.

when we change worknotes , its update in custom table 

now we want to attach any attachment in incident that has to update in related list custom table form.

kindly help me code , i tried in businessrule in sys_attachment table but not working

2 REPLIES 2

karthiknagaramu
Kilo Sage

Hi,

 

Since you cannot trigger a BR when attachment is added to incident table, you have to create a BR on sys_attachment table.

When to Run: After, Insert. 

Condition: Table name is incident

 

In script use the GlideSysAttachment API.

 

This link provides details on how to use this API with examples.

https://developer.servicenow.com/dev.do#!/reference/api/vancouver/server/no-namespace/c_GlideSysAtta...

 

Regards,

Karthik Nagaramu

Maddysunil
Kilo Sage

@Sweta24 

To achieve the requirement of attaching an attachment to an incident and having that attachment automatically updated in a related custom table, you need to create a script that will copy the attachment from the incident to the related record in your custom table. This can be accomplished using a Business Rule and a Script Include.

Step 1: Create a Script Include to Handle Attachment Copying

 

var AttachmentUtils = Class.create();
AttachmentUtils.prototype = {
    initialize: function() {},

    copyAttachments: function(sourceTable, sourceSysId, targetTable, targetSysId) {
        var grAttachment = new GlideRecord('sys_attachment');
        grAttachment.addQuery('table_name', sourceTable);
        grAttachment.addQuery('table_sys_id', sourceSysId);
        grAttachment.query();
        
        while (grAttachment.next()) {
            var attachmentSysId = grAttachment.sys_id.toString();
            var newAttachment = new GlideSysAttachment();
            var newSysId = newAttachment.copy(sourceTable, sourceSysId, targetTable, targetSysId);
        }
    },

    type: 'AttachmentUtils'
};

 

Step 2: Create a Business Rule to Trigger Attachment Copy

  1. Navigate to System Definition > Business Rules.

  2. Click New to create a new Business Rule.

  3. Configure the Business Rule with the following:

    • Name: Copy Attachment to Custom Table
    • Table: Incident
    • Advanced: Checked
    • When to run:
      • When: After
      • Insert: Checked
      • Update: Checked
      • Delete: Unchecked

 

(function executeRule(current, previous /*null when async*/) {
    if (current.operation() === 'insert' || current.operation() === 'update') {
        // Your custom table name and reference field
        var customTableName = 'u_custom_table'; // Change this to your custom table name
        var customTableRefField = 'u_incident'; // Change this to your custom table's reference field to incident

        var grCustomTable = new GlideRecord(customTableName);
        grCustomTable.addQuery(customTableRefField, current.sys_id);
        grCustomTable.query();

        var attachmentUtils = new AttachmentUtils();
        while (grCustomTable.next()) {
            attachmentUtils.copyAttachments('incident', current.sys_id, customTableName, grCustomTable.sys_id);
        }
    }
})(current, previous);

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks