logo

NJP

Copy Individual Attachment from 1 record to another

Import · Jul 22, 2019 · article

Hi,

In the last article, as you have seen, I have shown you how to copy all attachments from 1 record to another set of record(s). In this article, I will show you how you can copy a specific attachment from 1 record to another set of record(s).

To do this, we will need the following.

  • Attachment_entry (UI Macro): This is OOB UI Macro. So open this, click on insert and stay so that the OOB Record is not edited. Then deactivate the original record. We will add Copy link in our UI Macro attachment_entry
  • A popup view to select target incidents
  • A UI Button (list choice) to trigger copy action
  • A script include to actually copy the attachment.

Attachment_entry (UI Macro)

In this macro, we will add a copy link as shown below

image

Below, in the file add this javascript function onCopy which is being called by that copy link.

In the file, you will see a tag. In that, you can add this function</p> <pre><code>function onCopy(source_table,source_table_sys_id,source_file_id) { console.log(&quot;test &quot;+source_table+&quot;---&quot;+source_table_sys_id); //open the popup view of incidents var dialog = new GlideDialogWindow(&quot;copy_incidents_attach_view&quot;); dialog.setTitle(&quot;Select Attachments to Copy&quot;); dialog.setPreference(&#39;table&#39;, &#39;incident_list&#39;); dialog.setPreference(&#39;sysparm_view&#39;, &#39;copy_incident_attachments_view&#39;); dialog.setPreference(&#39;sysparm_force_row_count&#39;, 10); dialog.setPreference(&quot;source_table_name&quot;, source_table); dialog.setPreference(&quot;source_table_sys_id&quot;, source_table_sys_id); dialog.setPreference(&quot;source_file_id&quot;, source_file_id); dialog.render(); } </code></pre> <p>Once this is done, you shall see a link Copy beside each attachment in your incident record as you see below.</p> <p><strong>A popup View to select Target Records</strong></p> <p><img src="https://community.servicenow.com/community/image/serverpage/image-id/147239i4B6CC747D372189D/image-size/large?v=v2&px=999" alt="image"></p> <p><strong>UI Action Copy Attachment</strong></p> <p>Create a UI action as per this screenshot on the incident table.</p> <p><img src="https://community.servicenow.com/community/image/serverpage/image-id/147241iD876F5D636DAF93B/image-size/large?v=v2&px=999" alt="image"></p> <p>And in the script, add this code.</p> <pre><code>function copyAttachment() { var gdw = GlideDialogWindow.get(); var source_table_name = gdw.getPreference(&#39;source_table_name&#39;); var source_table_sys_id = gdw.getPreference(&#39;source_table_sys_id&#39;); var source_file_id = gdw.getPreference(&#39;source_file_id&#39;); var target_table_name = g_list.getTableName(); var selected_elem = g_list.getChecked(); var selected_ids = selected_elem.split(&quot;,&quot;); if(selected_ids.length&gt;1) { for(i=0;i&lt;selected_ids.length;i++) { if(selected_ids[0]==source_table_sys_id) { alert(&quot;You seem to have selected the source record also here. kindly check &quot;); return false; } } } else { if(selected_elem==source_table_sys_id) { alert(&quot;The selected record is the same as source record. kindly check &quot;); return false; } } console.log(&quot;Copy attachments&quot;+source_table_name+&quot;---&quot;+source_table_sys_id+&quot;---&quot;+g_list.getChecked()+&quot;--&quot;+g_list.getTableName()+&quot;---&quot;+source_file_id+&quot;--&quot;+selected_ids.length); //call script include to copy attachment. var ga = new GlideAjax(&#39;Copy_Individual_Attachment&#39;); ga.addParam(&#39;sysparm_name&#39;,&#39;copySpecificAttachment&#39;); ga.addParam(&#39;sysparm_source_table_name&#39;,source_table_name); ga.addParam(&#39;sysparm_source_table_sys_id&#39;,source_table_sys_id); ga.addParam(&#39;sysparm_source_file_id&#39;,source_file_id); ga.addParam(&#39;sysparm_target_table_name&#39;,target_table_name); ga.addParam(&#39;sysparm_target_table_sys_id&#39;,selected_elem); ga.addParam(&#39;sysparm_target_ids_count&#39;,selected_ids.length); ga.getXML(callback); function callback(response) { var answer = response.responseXML.documentElement.getAttribute(&quot;answer&quot;); if(answer) { gdw.destroy(); g_form.addInfoMessage(&quot;Attachment copied successfully&quot;); } else { g_form.addErrorMessage(&quot;Error: Attachment is not copied &quot;); } } } </code></pre> <p><strong>Script Include (Copy Attachment)</strong></p> <pre><code>var Copy_Individual_Attachment = Class.create(); Copy_Individual_Attachment.prototype = Object.extendsObject(AbstractAjaxProcessor, { copySpecificAttachment: function() { var donorTable = this.getParameter(&#39;sysparm_source_table_name&#39;); var donorID = this.getParameter(&#39;sysparm_source_table_sys_id&#39;); var recipientTable = this.getParameter(&#39;sysparm_target_table_name&#39;); var recipientID = this.getParameter(&#39;sysparm_target_table_sys_id&#39;); var fileID = this.getParameter(&#39;sysparm_source_file_id&#39;); var recordCount = this.getParameter(&#39;sysparm_target_ids_count&#39;); for(i=0;i&lt;recordCount;i++) { var donorAttSysID; var newAttRecord; var linkToNewRecord; var attDataRecord; var newDocRecord; var attRecord = new GlideRecord(&#39;sys_attachment&#39;); attRecord.addQuery(&#39;sys_id&#39;, fileID); attRecord.query(); if (attRecord.next()) { donorAttSysID = attRecord.getValue(&#39;sys_id&#39;); newAttRecord = this.copyRecord(attRecord); newAttRecord.setValue(&#39;table_name&#39;, recipientTable); if(recordCount&gt;1) { var target_ids = recipientID.split(&quot;,&quot;); newAttRecord.setValue(&#39;table_sys_id&#39;, target_ids[i]); } else { newAttRecord.setValue(&#39;table_sys_id&#39;, recipientID); } newAttRecord.update(); linkToNewRecord = gs.getProperty(&#39;glide.servlet.uri&#39;) + newAttRecord.getLink(); attDataRecord = new GlideRecord(&#39;sys_attachment_doc&#39;); attDataRecord.addQuery(&#39;sys_attachment&#39;, donorAttSysID); attDataRecord.query(); while (attDataRecord.next()) { newDocRecord = this.copyRecord(attDataRecord); newDocRecord.setValue(&#39;sys_attachment&#39;, newAttRecord.getValue(&#39;sys_id&#39;)); newDocRecord.update(); } } } return 1; }, copyRecord: function(record) { var recordElement; var recordElementName; var recordTable = record.getTableName(); var recordFields = record.getFields(); var newRecord = new GlideRecord(recordTable); newRecord.initialize(); for (var i = 0; i &lt; recordFields.size(); i++) { recordElement = recordFields.get(i); if(recordElement.getName() != &#39;sys_id&#39; &amp;&amp; recordElement.getName() != &#39;number&#39;) { recordElementName = recordElement.getName(); if(recordTable == &#39;sys_attachment&#39;) newRecord.setValue(recordElementName, record.getValue(recordElementName)); } } var newSysid = newRecord.insert(); if(recordTable == &#39;sys_attachment&#39;) return newRecord; }, type: &#39;Copy_Individual_Attachment&#39; }); </code></pre> <p>Let me know if you have any questions in the comments.</p> <p>Mark the article as helpful and bookmark if you found it useful.</p> <p>Note: The server-side code is taken from this blog <a href="https://snprotips.com/blog/2016/2/25/understanding-and-using-glideattachment" target="_blank" rel="noopener">https://snprotips.com/blog/2016/2/25/understanding-and-using-glideattachment</a></p>

View original source

https://www.servicenow.com/community/developer-articles/copy-individual-attachment-from-1-record-to-another/ta-p/2325219