<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>question Delete duplicate attachment script not working in Developer forum</title>
    <link>https://www.servicenow.com/community/developer-forum/delete-duplicate-attachment-script-not-working/m-p/2950181#M1118612</link>
    <description>&lt;P&gt;Hi Everyone, I have written a script action in human resource:core application where i am trying to delete duplicate attachment from case form, i am getting all the logs still attachment is not getting deleted.&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var caseSysID = current.parent.toString();
gs.info("caseSysIDLog " + caseSysID); // getting this log 
var taskAttachGr = new GlideRecord('sys_attachment');
taskAttachGr.addQuery('table_sys_id', current.sys_id.toString());
taskAttachGr.query();
while (taskAttachGr.next()) {
    var taskFile = taskAttachGr.file_name;
    gs.info("taskFileLog " + taskFile); // getting this log 

    var attach_count;
    var attachGa = new GlideAggregate('sys_attachment');

    attachGa.addQuery('table_sys_id', caseSysID.toString());
    attachGa.addQuery('file_name', taskFile.toString());
    attachGa.addAggregate('COUNT');
    attachGa.query();

    gs.info('Entered Script Include duplicate attachment Row Count' + "hello"); // getting this log 
    if (attachGa.next() &amp;amp;&amp;amp; attachGa.getAggregate('COUNT') != 1) {
        attach_count = attachGa.getAggregate('COUNT');
        gs.info('Entered Script Include duplicate attachment Aggregate Count' + attach_count);// getting this log 
        var attachGr = new GlideRecord('sys_attachment');
        attachGr.addQuery('table_sys_id', caseSysID.toString());
        attachGr.addQuery('file_name', taskFile.toString());
        attachGr.query();
        if (attachGr.next()) {
            gs.info('Delete Record duplicate attachment'); // getting this log
            attachGr.deleteRecord();
            attachGr.update();
        }
    }

}&lt;/LI-CODE&gt;&lt;P&gt;Below is the business rule from where i am calling the script action after 5 seconds&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var caseSysID = current.parent;

    var caseGr = new GlideRecord('sn_hr_core_case_talent_management');
    caseGr.addQuery('sys_id', caseSysID.toString());
    caseGr.query();
    if (caseGr.next()) {
        var hrService = caseGr.hr_service;
    }

    var complianceReview = gs.getProperty('sn_hr_core.compliance_review');

    if (hrService == complianceReview) { //Delete duplicate attachments only for compliance 

        var wait5sec = new GlideDateTime();
        wait5sec.addSeconds(5); // 5 sec in the future.
        gs.eventQueueScheduled('sn_hr_core.duplicate.attachment.on.case', current, '', '', wait5sec);
}&lt;/LI-CODE&gt;&lt;P&gt;Any help will be appreciated ..Thanks&lt;/P&gt;</description>
    <pubDate>Sun, 02 Jun 2024 02:13:12 GMT</pubDate>
    <dc:creator>Maddysunil</dc:creator>
    <dc:date>2024-06-02T02:13:12Z</dc:date>
    <item>
      <title>Delete duplicate attachment script not working</title>
      <link>https://www.servicenow.com/community/developer-forum/delete-duplicate-attachment-script-not-working/m-p/2950181#M1118612</link>
      <description>&lt;P&gt;Hi Everyone, I have written a script action in human resource:core application where i am trying to delete duplicate attachment from case form, i am getting all the logs still attachment is not getting deleted.&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var caseSysID = current.parent.toString();
gs.info("caseSysIDLog " + caseSysID); // getting this log 
var taskAttachGr = new GlideRecord('sys_attachment');
taskAttachGr.addQuery('table_sys_id', current.sys_id.toString());
taskAttachGr.query();
while (taskAttachGr.next()) {
    var taskFile = taskAttachGr.file_name;
    gs.info("taskFileLog " + taskFile); // getting this log 

    var attach_count;
    var attachGa = new GlideAggregate('sys_attachment');

    attachGa.addQuery('table_sys_id', caseSysID.toString());
    attachGa.addQuery('file_name', taskFile.toString());
    attachGa.addAggregate('COUNT');
    attachGa.query();

    gs.info('Entered Script Include duplicate attachment Row Count' + "hello"); // getting this log 
    if (attachGa.next() &amp;amp;&amp;amp; attachGa.getAggregate('COUNT') != 1) {
        attach_count = attachGa.getAggregate('COUNT');
        gs.info('Entered Script Include duplicate attachment Aggregate Count' + attach_count);// getting this log 
        var attachGr = new GlideRecord('sys_attachment');
        attachGr.addQuery('table_sys_id', caseSysID.toString());
        attachGr.addQuery('file_name', taskFile.toString());
        attachGr.query();
        if (attachGr.next()) {
            gs.info('Delete Record duplicate attachment'); // getting this log
            attachGr.deleteRecord();
            attachGr.update();
        }
    }

}&lt;/LI-CODE&gt;&lt;P&gt;Below is the business rule from where i am calling the script action after 5 seconds&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var caseSysID = current.parent;

    var caseGr = new GlideRecord('sn_hr_core_case_talent_management');
    caseGr.addQuery('sys_id', caseSysID.toString());
    caseGr.query();
    if (caseGr.next()) {
        var hrService = caseGr.hr_service;
    }

    var complianceReview = gs.getProperty('sn_hr_core.compliance_review');

    if (hrService == complianceReview) { //Delete duplicate attachments only for compliance 

        var wait5sec = new GlideDateTime();
        wait5sec.addSeconds(5); // 5 sec in the future.
        gs.eventQueueScheduled('sn_hr_core.duplicate.attachment.on.case', current, '', '', wait5sec);
}&lt;/LI-CODE&gt;&lt;P&gt;Any help will be appreciated ..Thanks&lt;/P&gt;</description>
      <pubDate>Sun, 02 Jun 2024 02:13:12 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/delete-duplicate-attachment-script-not-working/m-p/2950181#M1118612</guid>
      <dc:creator>Maddysunil</dc:creator>
      <dc:date>2024-06-02T02:13:12Z</dc:date>
    </item>
    <item>
      <title>Re: Delete duplicate attachment script not working</title>
      <link>https://www.servicenow.com/community/developer-forum/delete-duplicate-attachment-script-not-working/m-p/2950187#M1118614</link>
      <description>&lt;P&gt;Remove the&amp;nbsp;&lt;/P&gt;
&lt;PRE class="lia-code-sample  language-javascript"&gt;&lt;CODE&gt;attachGr.update();&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;after&lt;/P&gt;
&lt;PRE class="lia-code-sample  language-javascript"&gt;&lt;CODE&gt;attachGr.deleteRecord();&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 02 Jun 2024 03:14:05 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/delete-duplicate-attachment-script-not-working/m-p/2950187#M1118614</guid>
      <dc:creator>Maik Skoddow</dc:creator>
      <dc:date>2024-06-02T03:14:05Z</dc:date>
    </item>
    <item>
      <title>Re: Delete duplicate attachment script not working</title>
      <link>https://www.servicenow.com/community/developer-forum/delete-duplicate-attachment-script-not-working/m-p/2950188#M1118615</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://www.servicenow.com/community/user/viewprofilepage/user-id/116698"&gt;@Maik Skoddow&lt;/a&gt;&amp;nbsp; I tried without attachGr.update() still not working&lt;/P&gt;</description>
      <pubDate>Sun, 02 Jun 2024 03:17:09 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/delete-duplicate-attachment-script-not-working/m-p/2950188#M1118615</guid>
      <dc:creator>Maddysunil</dc:creator>
      <dc:date>2024-06-02T03:17:09Z</dc:date>
    </item>
    <item>
      <title>Re: Delete duplicate attachment script not working</title>
      <link>https://www.servicenow.com/community/developer-forum/delete-duplicate-attachment-script-not-working/m-p/2950190#M1118616</link>
      <description>&lt;P&gt;&lt;a href="https://www.servicenow.com/community/user/viewprofilepage/user-id/477741"&gt;@Maddysunil&lt;/a&gt;&amp;nbsp;Try to put your code responsible for deletion of attachment inside a script include in the &lt;STRONG&gt;Global&lt;/STRONG&gt; scope and call the same script include from your script action in HR Scope. I faced the similar issue in the past and I managed to address the issue by putting the deleteRecord operation the the Global scope.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope this helps.&lt;/P&gt;</description>
      <pubDate>Sun, 02 Jun 2024 03:57:00 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/delete-duplicate-attachment-script-not-working/m-p/2950190#M1118616</guid>
      <dc:creator>Sandeep Rajput</dc:creator>
      <dc:date>2024-06-02T03:57:00Z</dc:date>
    </item>
    <item>
      <title>Re: Delete duplicate attachment script not working</title>
      <link>https://www.servicenow.com/community/developer-forum/delete-duplicate-attachment-script-not-working/m-p/2950432#M1118673</link>
      <description>&lt;P&gt;&lt;a href="https://www.servicenow.com/community/user/viewprofilepage/user-id/145583"&gt;@Sandeep Rajput&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried with that still it was not able to delete..So i tried with an alternative approach and its working, I wrote before insert BR on attachment table itself and for duplicate attachment i aborted the action, below is the working code:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;if (current.table_name == 'sn_hr_core_case_talent_management') {
        var caseSysID = current.table_sys_id.toString();
        var caseGr = new GlideRecord('sn_hr_core_case_talent_management');
        caseGr.addQuery('sys_id', caseSysID);
        caseGr.query();
        if (caseGr.next()) {
            var complianceReview = gs.getProperty('sn_hr_core.compliance_review');
            if (caseGr.hr_service == complianceReview) {
                var attachGr = new GlideRecord('sys_attachment');
                attachGr.addQuery('table_sys_id', caseSysID);
                attachGr.addQuery('file_name', current.file_name.toString());
				attachGr.query();
                if (attachGr.next()) {
                    current.setAbortAction(true);
                }
            }
        }
    }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Jun 2024 02:59:35 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/delete-duplicate-attachment-script-not-working/m-p/2950432#M1118673</guid>
      <dc:creator>Maddysunil</dc:creator>
      <dc:date>2024-06-03T02:59:35Z</dc:date>
    </item>
    <item>
      <title>Re: Delete duplicate attachment script not working</title>
      <link>https://www.servicenow.com/community/developer-forum/delete-duplicate-attachment-script-not-working/m-p/2950455#M1118679</link>
      <description>&lt;P&gt;&lt;a href="https://www.servicenow.com/community/user/viewprofilepage/user-id/477741"&gt;@Maddysunil&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;I tried below code，and it works in global scope, but failed in other&amp;nbsp;&lt;SPAN&gt;scopes。&lt;BR /&gt;※Delete operation against 'sys_attachment' from scope 'x_615438_testx' has been &lt;FONT color="#FF0000"&gt;refused due to the table's cross-scope access policy&lt;/FONT&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Open the table's configration page and check on the "Can delete"&amp;nbsp; in "Application Access " tab.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;   var attachGa = new GlideAggregate('sys_attachment');
   attachGa.addQuery('table_sys_id', '47064b68a9fe19810186793eefffc9b7');
   attachGa.addAggregate('COUNT');  //not necessary
   attachGa.addAggregate('GROUP_CONCAT', 'sys_id');
   attachGa.groupBy('file_name');
   attachGa.query();

   while (attachGa.next()) {
       var count = attachGa.getAggregate('COUNT');  //not necessary
       gs.info("file_name " + attachGa.file_name + " count:" + count)

       if (count &amp;gt;= 2) {       //not necessary
           var sys_ids = attachGa.getAggregate('GROUP_CONCAT', 'sys_id');
           var sys_idsList = sys_ids.split(",");
           for (var i = 1; i &amp;lt; sys_idsList.length; i++) {    //skip the first one
               var gr = new GlideRecord('sys_attachment');
               gr.get(sys_idsList[i]);
               gr.deleteRecord();
               //gs.info(gr.sys_id);
           }

       }
   }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="wenzhenz_0-1717387992958.png" style="width: 400px;"&gt;&lt;img src="https://www.servicenow.com/community/image/serverpage/image-id/361706i65CBFC76E01220D8/image-size/medium/is-moderation-mode/true?v=v2&amp;amp;px=400" role="button" title="wenzhenz_0-1717387992958.png" alt="wenzhenz_0-1717387992958.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Jun 2024 04:15:47 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/delete-duplicate-attachment-script-not-working/m-p/2950455#M1118679</guid>
      <dc:creator>wenzhenz</dc:creator>
      <dc:date>2024-06-03T04:15:47Z</dc:date>
    </item>
  </channel>
</rss>

