The CreatorCon Call for Content is officially open! Get started here.

UI Action that copies attachments from one record to another

JJG
Kilo Guru

Hello, 

I have a UI action that creates a new record in a new table, it then copies all of the old info and inputs it into the new record. However, I would like the attachments to transfer as well. How can I achieve this? 

Here is what I have so far:

var gr = new GlideRecord('x_utsll_candidate_candidate_tracking');

gr.first_name = current.first_name;
gr.email_address = current.email_address;
gr.last_name = current.last_name;

gr.insert();

 

How can I also transfer the attachments?

1 ACCEPTED SOLUTION

This worked for me:

var gr = new GlideRecord('x_utsll_candidate_candidate_tracking');

gr.first_name = current.first_name;
gr.email_address = current.email_address;
gr.last_name = current.last_name;

var sysID = gr.insert();


GlideSysAttachment.copy('x_utsll_candidate_candidate_outreach', current.sys_id, 'x_utsll_candidate_candidate_tracking', sysID);


var mySysID = current.update();
current.deleteRecord();

 

View solution in original post

11 REPLIES 11

Oke, but is moving the attachments from the old record to the new record also acceptable? Or should it really be copied / duplicated?

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Either works as long as the new record has the attachments. The old record will end up getting deleted anyway at the end of the script.

Oke I did a quick quick quick test. Not the nicest scripting, I really have to run now 🙂

Though see results below. For testing I just setup an "old" and "new" table. Old table with some records, 1 record multiple attachments, 1 record none attachments. Plus mapping field u_description from old table, to u_comments on new table.

Also opened the PDF. Is not corrupt or something, simply downloads and opens.

Fix Script I used:

(function() {

	var grOld = new GlideRecord('u_jjg_old_table');
	grOld._query();

	while(grOld._next()) {
		var grNew = new GlideRecord('u_jjg_new_table');
		grNew.initialize();
		grNew.u_comments = grOld.u_description;
		grNew.insert();
		
		var grAttachment = new GlideRecord('sys_attachment');
		grAttachment.addQuery('table_name', 'u_jjg_old_table');
		grAttachment.addQuery('table_sys_id', grOld.getUniqueValue());
		grAttachment._query();

		while(grAttachment._next()) {
			grAttachment.table_name = 'u_jjg_new_table';
			grAttachment.table_sys_id = grNew.getUniqueValue();
			grAttachment.update();
		}
	}

})();

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Oke I did a quick quick quick test also for your other question. Not the nicest scripting, I really have to run now 🙂

Though see results below. For testing I just setup an "old" and "new" table. Old table with some records, 1 record multiple attachments, 1 record none attachments. Plus mapping field u_description from old table, to u_comments on new table.

Also opened the PDF. Is not corrupt or something, simply downloads and opens.

Fix Script I used below. Probably you want to change this a bit, like you are on a UI Action on a current record, so grOld can be current I guess. And addQuery with current.getUniqueValue(), so the current script is not 100% for this topic. Though this should give you an idea.


For your other question with this script you could simply copy all records and remap all attachments.

(function() {

	var grOld = new GlideRecord('u_jjg_old_table');
	grOld._query();

	while(grOld._next()) {
		var grNew = new GlideRecord('u_jjg_new_table');
		grNew.initialize();
		grNew.u_comments = grOld.u_description;
		grNew.insert();
		
		var grAttachment = new GlideRecord('sys_attachment');
		grAttachment.addQuery('table_name', 'u_jjg_old_table');
		grAttachment.addQuery('table_sys_id', grOld.getUniqueValue());
		grAttachment._query();

		while(grAttachment._next()) {
			grAttachment.table_name = 'u_jjg_new_table';
			grAttachment.table_sys_id = grNew.getUniqueValue();
			grAttachment.update();
		}
	}

})();

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Here is what I have, it does not seem to be working just yet:

var gr = new GlideRecord('x_utsll_candidate_candidate_tracking'); //This is the new table

gr.first_name = current.first_name;
gr.email_address = current.email_address;
gr.last_name = current.last_name;

gr.insert();


var grOut = new GlideRecord('x_utsll_candidate_candidate_outreach'); //This is the old table
grOut._query();
var grAttachment = new GlideRecord('sys_attachment');
grAttachment.addQuery('table_name', 'x_utsll_candidate_candidate_outreach');
grAttachment.addQuery('table_sys_id', grOut.getUniqueValue());
grAttachment._query();

while (grAttachment._next()) {
grAttachment.table_name = 'x_utsll_candidate_candidate_tracking';
grAttachment.table_sys_id = gr.getUniqueValue();
grAttachment.update();
}