We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

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

Not applicable
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

1 ACCEPTED SOLUTION

@Community Alums 

Glad to help.

Please mark my response as correct and close the thread.

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

View solution in original post

5 REPLIES 5

Ankur Bawiskar
Tera Patron

@Community Alums 

GlideSysAttachment().copy will copy all files and not specific ones.

I shared solution how to copy particular file 6-7 years ago

Is there any way to copy single attachment from multiple attachments ?? 

To avoid loop and sync from file coming from Azure, the script which copies there you can check who is the sys_created_by on sys_attachment. If it's API user then that file is coming from Azure

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

@Community Alums 

Hope you are doing good.

Did my reply answer your question?

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

Not applicable

Hi @Ankur Bawiskar ,

Thanks for providing the solution. However, the scenario in my case was slightly different. I ended up using gsa.writeContentStream to copy a single attachment without duplicating it, and it’s working fine now.

Thank you for the effort you put into resolving the query. I appreciate your timely response.

Thanks,
Mustakim

 

@Community Alums 

Glad to help.

Please mark my response as correct and close the thread.

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