Copy Attachment from File attachment field to same record so it will appear in Manage attachment

Pritam Mangrulk
Mega Contributor

Hi All,

I want to copy attachment from File attachment field created on incident table to same record so it will show in manage attachment at the top of the record, can you please help.

I tried GlideSysAttachment.copy function but it didn't work.

 

Thanks,

Pritam

1 ACCEPTED SOLUTION

Hi,

please update the script as this

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
	GlideSysAttachment.copy('ZZ_YY' + current.getTableName(), current.sys_id, current.getTableName(), current.sys_id);

	// rename the table_name
	var gr = new GlideRecord("sys_attachment");
	gr.orderByDesc('sys_created_on');
	gr.addQuery("table_sys_id", current.sys_id);
	gr.setLimit(1);
	gr.query();
	if (gr.next()) {
		gr.table_name = current.getTableName();
		gr.update();
	}

})(current, previous);

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

9 REPLIES 9

Ankur Bawiskar
Tera Patron
Tera Patron

@Pritam Mangrulkar 

you can use before insert/update BR as this

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
	GlideSysAttachment.copy('ZZ_YY' + current.getTableName(), current.sys_id, current.getTableName(), current.sys_id);

})(current, previous);

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur, 

I noticed when i am using this function, it is adding ZZ_YY in destination table as well with attachment state as Pending, so it is not adding attachment in incident table but ZZ_YYincident table.

 

Thanks,

Pritam

Hi,

please update the script as this

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
	GlideSysAttachment.copy('ZZ_YY' + current.getTableName(), current.sys_id, current.getTableName(), current.sys_id);

	// rename the table_name
	var gr = new GlideRecord("sys_attachment");
	gr.orderByDesc('sys_created_on');
	gr.addQuery("table_sys_id", current.sys_id);
	gr.setLimit(1);
	gr.query();
	if (gr.next()) {
		gr.table_name = current.getTableName();
		gr.update();
	}

})(current, previous);

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Thanks Ankur, Its working now.

 

Best Regards,

Pritam