<?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 Re: Copy currently inserted attachment in ITSM forum</title>
    <link>https://www.servicenow.com/community/itsm-forum/copy-currently-inserted-attachment/m-p/2851853#M516057</link>
    <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://www.servicenow.com/community/user/viewprofilepage/user-id/616512"&gt;@Petr Pastuszek1&lt;/a&gt;&amp;nbsp;,&lt;BR /&gt;&lt;BR /&gt;Please try with the modified code below,&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;(function executeRule(current, previous /*null when async*/) {

    // Get the current task record
    var grTask = new GlideRecord('sc_task');
    if (grTask.get(current.table_sys_id)) {

        // Create a new sys_attachment record
        var grAttachment = new GlideRecord('sys_attachment');
        grAttachment.initialize();

        // Set values for the new attachment record
        grAttachment.setValue('file_name', current.file_name);
        grAttachment.setValue('content_type', current.content_type);
        grAttachment.setValue('size_bytes', current.size_bytes);
        grAttachment.setValue('table_name', 'sc_req_item');
        grAttachment.setValue('table_sys_id', grTask.request_item);

        // Insert the new attachment record
        var newAttachmentSysId = grAttachment.insert();

        // Copy the content stream from the original attachment to the new attachment
        var gsa = new GlideSysAttachment();
        gsa.writeContentStream(
            grAttachment,
            current.file_name,
            current.content_type,
            gsa.getContentStream(current.sys_id)
        );
        
        // Optionally, log the new attachment sys_id for debugging
        gs.info('New Attachment Sys ID: ' + newAttachmentSysId);
    }

})(current, previous);
&lt;/LI-CODE&gt;</description>
    <pubDate>Wed, 06 Mar 2024 07:30:54 GMT</pubDate>
    <dc:creator>AniketC85155510</dc:creator>
    <dc:date>2024-03-06T07:30:54Z</dc:date>
    <item>
      <title>Copy currently inserted attachment</title>
      <link>https://www.servicenow.com/community/itsm-forum/copy-currently-inserted-attachment/m-p/2850180#M515932</link>
      <description>&lt;P&gt;Hello Team&lt;/P&gt;&lt;P&gt;When attachment is added to sc task we need to copy it to ritm.&lt;/P&gt;&lt;P&gt;but only currently inserted file/files,not those inserted yesterday and so on&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;any advice on script in BR on attachment table.&lt;/P&gt;</description>
      <pubDate>Tue, 05 Mar 2024 07:52:50 GMT</pubDate>
      <guid>https://www.servicenow.com/community/itsm-forum/copy-currently-inserted-attachment/m-p/2850180#M515932</guid>
      <dc:creator>Petr Pastuszek1</dc:creator>
      <dc:date>2024-03-05T07:52:50Z</dc:date>
    </item>
    <item>
      <title>Re: Copy currently inserted attachment</title>
      <link>https://www.servicenow.com/community/itsm-forum/copy-currently-inserted-attachment/m-p/2850201#M515934</link>
      <description>&lt;P&gt;Hi Petr,&lt;/P&gt;&lt;P&gt;There is one ServiceNow function for the copy attachment you can try this&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;STRONG&gt;'GlideSysAttachment.copy'&lt;/STRONG&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt;&lt;A href="https://developer.servicenow.com/dev.do#!/reference/api/vancouver/server_legacy/GlideSysAttachmentGlobalAPI" target="_blank" rel="noopener"&gt;https://developer.servicenow.com/dev.do#!/reference/api/vancouver/server_legacy/GlideSysAttachmentGlobalAPI&lt;/A&gt;&lt;/P&gt;&lt;P&gt;To copy the only 1st attachment you can try with if loop, so it will take only 1st attachment.&lt;/P&gt;&lt;P&gt;If you find the solution useful&amp;nbsp;&lt;STRONG&gt;&lt;I&gt;Please mark this response as correct&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/I&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Thanks and regard,&lt;/P&gt;&lt;P&gt;Siddhesh&lt;/P&gt;</description>
      <pubDate>Tue, 05 Mar 2024 08:07:12 GMT</pubDate>
      <guid>https://www.servicenow.com/community/itsm-forum/copy-currently-inserted-attachment/m-p/2850201#M515934</guid>
      <dc:creator>Siddhesh Wani1</dc:creator>
      <dc:date>2024-03-05T08:07:12Z</dc:date>
    </item>
    <item>
      <title>Re: Copy currently inserted attachment</title>
      <link>https://www.servicenow.com/community/itsm-forum/copy-currently-inserted-attachment/m-p/2850211#M515936</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://www.servicenow.com/community/user/viewprofilepage/user-id/616512"&gt;@Petr Pastuszek1&lt;/a&gt;&amp;nbsp;,&lt;BR /&gt;&lt;BR /&gt;Please give a try to the code below and see how it works for you,&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;(function executeRule(current, previous /*null when async*/) {

    // Get the current task record
    var taskRec = new GlideRecord('sc_task');
    if (taskRec.get(current.table_sys_id)) {

        // Query for the most recent attachment with the current timestamp
        var attachment = new GlideRecord('sys_attachment');
        attachment.addQuery('table_sys_id', current.table_sys_id);
        attachment.orderByDesc('sys_created_on');
        attachment.setLimit(1); // Limit the result set to 1 record
        attachment.query();

        if (attachment.next()) {

            // Get the associated RITM record
            var ritmRec = new GlideRecord('sc_req_item');
            if (ritmRec.get(taskRec.request_item)) {

                // Copy the latest attachment to the RITM
                var gsa = new GlideSysAttachment();
                gsa.writeContentStream(
                    ritmRec,
                    attachment.file_name,
                    attachment.content_type,
                    gsa.getContentStream(attachment.sys_id)
                );
            }
        }
    }

})(current, previous);
&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;&lt;STRONG&gt;Please Mark &lt;/STRONG&gt;&lt;/EM&gt;&lt;span class="lia-unicode-emoji" title=":white_heavy_check_mark:"&gt;✅&lt;/span&gt;&lt;EM&gt;&lt;STRONG&gt;Correct if this solves your query and also mark &lt;/STRONG&gt;&lt;/EM&gt;&lt;span class="lia-unicode-emoji" title=":thumbs_up:"&gt;👍&lt;/span&gt;&lt;EM&gt;&lt;STRONG&gt;Helpful if you find my response worthy based on the impact.&lt;/STRONG&gt;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;&lt;STRONG&gt;Thanks &amp;amp; Regards,&lt;/STRONG&gt;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;&lt;STRONG&gt;Aniket.&lt;/STRONG&gt;&lt;/EM&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Mar 2024 08:09:38 GMT</pubDate>
      <guid>https://www.servicenow.com/community/itsm-forum/copy-currently-inserted-attachment/m-p/2850211#M515936</guid>
      <dc:creator>AniketC85155510</dc:creator>
      <dc:date>2024-03-05T08:09:38Z</dc:date>
    </item>
    <item>
      <title>Re: Copy currently inserted attachment</title>
      <link>https://www.servicenow.com/community/itsm-forum/copy-currently-inserted-attachment/m-p/2850213#M515937</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://www.servicenow.com/community/user/viewprofilepage/user-id/616512"&gt;@Petr Pastuszek1&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As already suggested by our colleagues to use GlideSysAttachment ,you can try that but if you have already tried something we would request you to paste the code you are trying to execute.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please mark correct/helpful if my response helped you.&lt;/P&gt;</description>
      <pubDate>Tue, 05 Mar 2024 08:10:45 GMT</pubDate>
      <guid>https://www.servicenow.com/community/itsm-forum/copy-currently-inserted-attachment/m-p/2850213#M515937</guid>
      <dc:creator>Anubhav24</dc:creator>
      <dc:date>2024-03-05T08:10:45Z</dc:date>
    </item>
    <item>
      <title>Re: Copy currently inserted attachment</title>
      <link>https://www.servicenow.com/community/itsm-forum/copy-currently-inserted-attachment/m-p/2850244#M515938</link>
      <description>&lt;P&gt;Thank you!!!&lt;BR /&gt;I will give it try today in few hours,not at pc now.&lt;/P&gt;&lt;P&gt;user can insert 1 or more attachments at one time and those I need to duplicate to rirm always.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;when I tried yesterday glidesysattachcopy function from ServiceNow,it always copy attachments uploaded in past whcih is not what we want. Only those files uploaded at current time.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/Petr&lt;/P&gt;&lt;P&gt;/Petr&lt;/P&gt;</description>
      <pubDate>Tue, 05 Mar 2024 08:33:49 GMT</pubDate>
      <guid>https://www.servicenow.com/community/itsm-forum/copy-currently-inserted-attachment/m-p/2850244#M515938</guid>
      <dc:creator>Petr Pastuszek1</dc:creator>
      <dc:date>2024-03-05T08:33:49Z</dc:date>
    </item>
    <item>
      <title>Re: Copy currently inserted attachment</title>
      <link>https://www.servicenow.com/community/itsm-forum/copy-currently-inserted-attachment/m-p/2850332#M515944</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://www.servicenow.com/community/user/viewprofilepage/user-id/616512"&gt;@Petr Pastuszek1&lt;/a&gt;&amp;nbsp;,&lt;BR /&gt;&lt;BR /&gt;Okay then please give a try to the script below, which will allow you to copy multiple attachments for the current time&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;(function executeRule(current, previous /*null when async*/) {

    // Get the current task record
    var taskRec = new GlideRecord('sc_task');
    if (taskRec.get(current.table_sys_id)) {

        // Query for attachments with the current timestamp
        var attachment = new GlideRecord('sys_attachment');
        attachment.addQuery('table_sys_id', current.table_sys_id);
        attachment.addQuery('sys_created_on', '&amp;gt;=', gs.daysAgo(0)); // Attachments created today
        attachment.orderByDesc('sys_created_on');
        attachment.query();

        // Loop through the attachments
        while (attachment.next()) {

            // Get the associated RITM record
            var ritmRec = new GlideRecord('sc_req_item');
            if (ritmRec.get(taskRec.request_item)) {

                // Copy each attachment to the RITM
                var gsa = new GlideSysAttachment();
                gsa.writeContentStream(
                    ritmRec,
                    attachment.file_name,
                    attachment.content_type,
                    gsa.getContentStream(attachment.sys_id)
                );
            }
        }
    }

})(current, previous);
&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 05 Mar 2024 09:23:29 GMT</pubDate>
      <guid>https://www.servicenow.com/community/itsm-forum/copy-currently-inserted-attachment/m-p/2850332#M515944</guid>
      <dc:creator>AniketC85155510</dc:creator>
      <dc:date>2024-03-05T09:23:29Z</dc:date>
    </item>
    <item>
      <title>Re: Copy currently inserted attachment</title>
      <link>https://www.servicenow.com/community/itsm-forum/copy-currently-inserted-attachment/m-p/2850686#M515991</link>
      <description>&lt;P&gt;Hello and thank you,&lt;/P&gt;&lt;P&gt;tested code and problem is that it control data created on today.&lt;/P&gt;&lt;P&gt;If I insert attachment 'A' now and 'B' 1 min later, end result is that RITM will have these attachments: A + B + B and this is not wanted.&lt;/P&gt;&lt;P&gt;is there really no way to copy only those which are currently inserted (current object) without need to query table?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/Petr&lt;/P&gt;</description>
      <pubDate>Tue, 05 Mar 2024 12:26:10 GMT</pubDate>
      <guid>https://www.servicenow.com/community/itsm-forum/copy-currently-inserted-attachment/m-p/2850686#M515991</guid>
      <dc:creator>Petr Pastuszek1</dc:creator>
      <dc:date>2024-03-05T12:26:10Z</dc:date>
    </item>
    <item>
      <title>Re: Copy currently inserted attachment</title>
      <link>https://www.servicenow.com/community/itsm-forum/copy-currently-inserted-attachment/m-p/2851002#M516019</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://www.servicenow.com/community/user/viewprofilepage/user-id/616512"&gt;@Petr Pastuszek1&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can you try this in a BR:&lt;/P&gt;
&lt;P&gt;Table - Catalog Task&lt;BR /&gt;When to Run - Before Update&lt;BR /&gt;Scripts -&amp;nbsp;&lt;BR /&gt;(function executeRule(current, previous /*null when async*/ ) {&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; // Add your code here&lt;BR /&gt;&amp;nbsp; &amp;nbsp; var copy = new GlideRecord('sc_req_item');&lt;BR /&gt;&amp;nbsp; &amp;nbsp; copy.get(&amp;lt;field name of RITM on sctask table&amp;gt;);&lt;BR /&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;if (copy.next()) {&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; GlideSysAttachment.copy('sc_task', current.getUniqueValue(), 'sc_req_item', copy.getUniqueValue());&lt;BR /&gt;&amp;nbsp; &amp;nbsp; }&lt;BR /&gt;})(current, previous);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please mark helpful/correct if my response helped you.&lt;/P&gt;</description>
      <pubDate>Tue, 05 Mar 2024 15:27:17 GMT</pubDate>
      <guid>https://www.servicenow.com/community/itsm-forum/copy-currently-inserted-attachment/m-p/2851002#M516019</guid>
      <dc:creator>Anubhav24</dc:creator>
      <dc:date>2024-03-05T15:27:17Z</dc:date>
    </item>
    <item>
      <title>Re: Copy currently inserted attachment</title>
      <link>https://www.servicenow.com/community/itsm-forum/copy-currently-inserted-attachment/m-p/2851030#M516022</link>
      <description>&lt;P&gt;Hello, the code doesnt work, I modified 'copy.get' part and doesnt work.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I was able today to figure out solution this way, could anyone confirm this is ok technically?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It is working really good and as expected, doesnt create duplicates ...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;var grTask = new GlideRecord('sc_task');
	grTask.get(current.table_sys_id);


	var grAttachment = new GlideRecord('sys_attachment');
		grAttachment.initialize();

		grAttachment.setValue('file_name',current.file_name);
		grAttachment.setValue('content_type',current.content_type);
		grAttachment.setValue('size_bytes',current.size_bytes);
		grAttachment.setValue('table_name','sc_req_item');
		grAttachment.setValue('table_sys_id', grTask.request_item);

		grAttachment.insert();&lt;/LI-CODE&gt;&lt;P&gt;/Petr&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Mar 2024 15:42:07 GMT</pubDate>
      <guid>https://www.servicenow.com/community/itsm-forum/copy-currently-inserted-attachment/m-p/2851030#M516022</guid>
      <dc:creator>Petr Pastuszek1</dc:creator>
      <dc:date>2024-03-05T15:42:07Z</dc:date>
    </item>
    <item>
      <title>Re: Copy currently inserted attachment</title>
      <link>https://www.servicenow.com/community/itsm-forum/copy-currently-inserted-attachment/m-p/2851033#M516023</link>
      <description>&lt;P&gt;This code which I have done is working well, could you confirm it is ok technically?&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;var grTask = new GlideRecord('sc_task');
	grTask.get(current.table_sys_id);


	var grAttachment = new GlideRecord('sys_attachment');
		grAttachment.initialize();

		grAttachment.setValue('file_name',current.file_name);
		grAttachment.setValue('content_type',current.content_type);
		grAttachment.setValue('size_bytes',current.size_bytes);
		grAttachment.setValue('table_name','sc_req_item');
		grAttachment.setValue('table_sys_id', grTask.request_item);

		grAttachment.insert();&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 05 Mar 2024 15:43:28 GMT</pubDate>
      <guid>https://www.servicenow.com/community/itsm-forum/copy-currently-inserted-attachment/m-p/2851033#M516023</guid>
      <dc:creator>Petr Pastuszek1</dc:creator>
      <dc:date>2024-03-05T15:43:28Z</dc:date>
    </item>
    <item>
      <title>Re: Copy currently inserted attachment</title>
      <link>https://www.servicenow.com/community/itsm-forum/copy-currently-inserted-attachment/m-p/2851068#M516024</link>
      <description>&lt;P&gt;Taking this back.&lt;/P&gt;&lt;P&gt;working well, but when its copied, its not usable. Its not real e.g. image object, when I download it to local disc, it doesnt open as it missing something from original file&lt;/P&gt;&lt;P&gt;Still trying to figure out next step.&lt;/P&gt;</description>
      <pubDate>Tue, 05 Mar 2024 15:56:37 GMT</pubDate>
      <guid>https://www.servicenow.com/community/itsm-forum/copy-currently-inserted-attachment/m-p/2851068#M516024</guid>
      <dc:creator>Petr Pastuszek1</dc:creator>
      <dc:date>2024-03-05T15:56:37Z</dc:date>
    </item>
    <item>
      <title>Re: Copy currently inserted attachment</title>
      <link>https://www.servicenow.com/community/itsm-forum/copy-currently-inserted-attachment/m-p/2851853#M516057</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://www.servicenow.com/community/user/viewprofilepage/user-id/616512"&gt;@Petr Pastuszek1&lt;/a&gt;&amp;nbsp;,&lt;BR /&gt;&lt;BR /&gt;Please try with the modified code below,&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;(function executeRule(current, previous /*null when async*/) {

    // Get the current task record
    var grTask = new GlideRecord('sc_task');
    if (grTask.get(current.table_sys_id)) {

        // Create a new sys_attachment record
        var grAttachment = new GlideRecord('sys_attachment');
        grAttachment.initialize();

        // Set values for the new attachment record
        grAttachment.setValue('file_name', current.file_name);
        grAttachment.setValue('content_type', current.content_type);
        grAttachment.setValue('size_bytes', current.size_bytes);
        grAttachment.setValue('table_name', 'sc_req_item');
        grAttachment.setValue('table_sys_id', grTask.request_item);

        // Insert the new attachment record
        var newAttachmentSysId = grAttachment.insert();

        // Copy the content stream from the original attachment to the new attachment
        var gsa = new GlideSysAttachment();
        gsa.writeContentStream(
            grAttachment,
            current.file_name,
            current.content_type,
            gsa.getContentStream(current.sys_id)
        );
        
        // Optionally, log the new attachment sys_id for debugging
        gs.info('New Attachment Sys ID: ' + newAttachmentSysId);
    }

})(current, previous);
&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 06 Mar 2024 07:30:54 GMT</pubDate>
      <guid>https://www.servicenow.com/community/itsm-forum/copy-currently-inserted-attachment/m-p/2851853#M516057</guid>
      <dc:creator>AniketC85155510</dc:creator>
      <dc:date>2024-03-06T07:30:54Z</dc:date>
    </item>
    <item>
      <title>Re: Copy currently inserted attachment</title>
      <link>https://www.servicenow.com/community/itsm-forum/copy-currently-inserted-attachment/m-p/2852393#M516099</link>
      <description>&lt;P&gt;Thank you, I used your code and updated below way which is after many tests working really as expected.&lt;/P&gt;&lt;P&gt;It will copy always only currently uploaded attachment or attachments from ritm to sc_task. Thank you&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/Petr&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;(function executeRule(current, previous /*null when async*/) {

	
		/******************************************************
		 * Access task into which attachment has been uploaded
		 *****************************************************/
		var grTask = new GlideRecord('sc_task');
		if (grTask.get(current.table_sys_id)) {
		/******************************************************************
		 * Access RITM to which attachment will be copied from above task
		 ******************************************************************/
		var ritm = new GlideRecord('sc_req_item');
		ritm.get(grTask.request_item);
        
		/*******************************************************************************************
		 * Method to copy attachment from sc_task to RITM
		 * Do not use other method because it will copy all attachments and not currently uploded
		 *******************************************************************************************/
		var targetGlideRecord = ritm;
		var fileName = current.file_name;
		var contentType = current.content_type;
		var sourceAttachmentSysId = current.sys_id;					
					
				var gsa = new GlideSysAttachment();
				gsa.writeContentStream(
				targetGlideRecord,
				fileName,
				contentType,
				gsa.getContentStream(current.sys_id)
				);   
		}
})(current, previous);&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 06 Mar 2024 12:27:31 GMT</pubDate>
      <guid>https://www.servicenow.com/community/itsm-forum/copy-currently-inserted-attachment/m-p/2852393#M516099</guid>
      <dc:creator>Petr Pastuszek1</dc:creator>
      <dc:date>2024-03-06T12:27:31Z</dc:date>
    </item>
  </channel>
</rss>

