Preventing Duplicate Attachments in Bi-Directional Sync (sc_task to rm_story)

mustakim1119
Kilo Explorer
Hi Community, 
 
I am facing an issue with duplicating attachments while syncing files from Catalog Task (sc_task) to Story (rm_story). 
 
Scenario: 
  • Our integration is bi-directional: ServiceNow to Azure DevOps and vice-versa.
  • The flow is: User adds an attachment to sc_taskitsyncs to the linked rm_story via a Business Rulefrom the Story, it flows to Azure via a work item integration.
  • The Issue: When a second attachment is added to the Task, the system is duplicating the first attachment on the Story level.
  • I have tried using GlideSysAttachment.copy(), but it performs a bulk copy of all attachments every time, causing the duplicates. I also attempted writeContentStream, but I am struggling to get it to execute correctly.


    Current Logic (After Insert BR on sys_attachment):
    when to run condition: table name is sc_task.
     

    (function executeRule(current, previous) {

        var taskGR = new GlideRecord('sc_task');

        if (taskGR.get(current.table_sys_id) && taskGR.agile_story) {

     

            var storyID = taskGR.getValue('agile_story');

            var sa = new GlideSysAttachment();

     

            // Copy EVERYTHING

     

            // Immediately find and delete duplicates on the Story

            var dup = new GlideRecord('sys_attachment');

            dup.addQuery('table_name', 'rm_story');

            dup.addQuery('table_sys_id', storyID);

            dup.addQuery('file_name', current.file_name);

            dup.addQuery('size_bytes', current.size_bytes);

            dup.orderByDesc('sys_created_on');

            dup.setLimit(1);

            dup.query();

            sa.copy('sc_task', taskGR.sys_id, 'rm_story', storyID);

            // If more than 1 exists, delete the older one

            if (dup.getCount() > 1) {

                dup.next(); // Keep the newest one

                while (dup.next()) {

                    dup.deleteRecord();

                    gs.log("ATTACHMENT_DEBUG: Triggered by " + gs.getSession().getInteractiveStack());

     

                    // if (!check.hasNext()) {

                    // // 3. COPY ONLY THIS FILE

                    // var sa = new GlideSysAttachment();

                    // // This method creates a single copy of the current record only

                    // sa.writeContentStream(

                    //     'rm_story',

                    //     storyID,

                    //     current.file_name,

                    //     current.content_type,

                    //     sa.getContentStream(current.sys_id)

                    // );

                    // }

                }

            }

        }

    })(current, previous);



    What I need:
    1. A way to copy only the current attachment being added to the sc_task over to the rm_story.
    2. A check to ensure that if the attachment is coming from the Azure integration, it doesn't loop back (avoiding infinite loops).

    Has anyone successfully implemented a "single-file sync" between these tables that handles bi-directional logic without creating duplicates?

     
    Any help or code snippets using writeContentStream or copyAttachment for a single record would be greatly appreciated!

    Regards,
    Mustakim

0 REPLIES 0