asifnoor
Kilo Patron

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)

find_real_file.png

find_real_file.png

Popup View

find_real_file.png

UI Action Copy (list choice)

find_real_file.png

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)

find_real_file.png

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

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

Comments
Ryan Healey
Tera Expert

Thanks for the info!  I've been trying to do this for a while.  I had to resort to creating a new relationship between tables and listing all attachments as a related list.  

asifnoor
Kilo Patron

Glad it will help you.

I will also be creating another article where you can specifically copy the single attachments from 1 record to another. 

asifnoor
Kilo Patron

Hi, 

I have added a new article, which will allow you to copy specific attachment (not all) from 1 record to another. Kindly check it.

https://community.servicenow.com/community?id=community_article&sys_id=633352e5db327b402be0a851ca961...

Simon Ciglia
Tera Guru

Thank you could you (or anybody) please post the "copy_incidents_attach_view" ui page?

asifnoor
Kilo Patron

Hello Simon,

It is not a UI page, but a view created on incident table with selected columns. (configure list layout, and in that you can select New under views and create a new view and keep only the columns that you want).

In my above article, if you check, you will find popup view, which is actually the copy_incidents_attach_view that is rendered there. 

And it is rendered as popup using GlideDialogWindow using UI action (check 1st screenshot in the above article).

Mark the comment as helpful if it helps.

 

Simon Ciglia
Tera Guru

made my day, thank you very much 😄

asifnoor
Kilo Patron

Glad it helped 🙂

amitgrewal
Tera Contributor

Hi Mr. Asif,

I've a question. Is it possible to copy the original email logs and its contents from an Incident to a Request using the UI actions script?

Is this possible using getJournalEntry(-1)

Version history
Last update:
‎07-19-2019 07:16 AM
Updated by: