logo

NJP

Copy Attachments from 1 record to another

Import · Jul 19, 2019 · article

In this article, I will explain how to copy attachments from 1 incident record to another. You can use the same logic for any other table as well.

To do this, we will use the following

  • A UI action (Form button) to display on the source record
  • A popup view to select target incidents
  • A UI Button (list choice) to trigger copy action
  • A script include to actually copy

UI Action (Form button)

image

image

Popup View

image

UI Action Copy (list choice)

image

In the script, add the below code

function copyAttachments() {
  var gdw = GlideDialogWindow.get();
  var source_table_name = gdw.getPreference('source_table_name');
  var source_table_sys_id = gdw.getPreference('source_table_sys_id');
  var target_table_name = g_list.getTableName();
  var selected_elem = g_list.getChecked();
  var selected_ids = selected_elem.split(",");
  if(selected_ids.length>1) {
    for(i=0;i<selected_ids.length;i++) {
      if(selected_ids[0]==source_table_sys_id) {
        alert("You seem to have selected the source record also here. kindly check ");
    return false;
      }
    }
  } else {
    if(selected_elem==source_table_sys_id) {
      alert("The selected record is same as source record. kindly check ");
      return false;
    }   
  }

  console.log("Copy attachments"+source_table_name+"---"+source_table_sys_id+"---"+g_list.getChecked()+"--"+g_list.getTableName());
  //call script include to copy attachment.
  var ga = new GlideAjax('Copy_Attachments');
  ga.addParam('sysparm_name','copy');
  ga.addParam('sysparm_source_table_name',source_table_name);
  ga.addParam('sysparm_source_table_sys_id',source_table_sys_id);
  ga.addParam('sysparm_target_table_name',target_table_name);
  ga.addParam('sysparm_target_table_sys_id',selected_elem);
  ga.addParam('ssysparm_target_ids_count',selected_ids.length);
  ga.getXML(callback);
  function callback(response)
  {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    if(answer) {
      gdw.destroy();
      g_form.addInfoMessage("Attachments copied successfully");
    } 
    else {
      gdw.destroy();
      g_form.addErrorMessage("Error: Attachments not copied ");
    }       
  }
}

Script Include (Copy Attachments)

image

Let me know if you have any questions in the comments.

Mark the article as helpful and bookmark if you found it useful.

View original source

https://www.servicenow.com/community/developer-articles/copy-attachments-from-1-record-to-another/ta-p/2309431