Make the Attachment variable used in the catalog item add the file to the top of case as when using the paper clip

Gabriel Alowa
Giga Contributor

Recently Servicenow added a new variable type named attachment and it works great. Here is my question. When that variable is used on the catalog item , it attaches the file in the work note in the case created. That location is different from where the files is attached when using the paper clip . Is here a way to make the new variable attachment add the file to the top of the case as when you use the paper clip? 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Gabriel Alowa 

I have shared working solution in this link below

Disable ZZ_YY prefix from attachment when added through record producer

Adding the same here for your reference:

1) I created Async After Insert BR on the sc_req_item table

2) Script as this

a) Remove the ZZ_YY

b) Then use the Copy Attachment functionality

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

	// Add your code here
	var gr = new GlideRecord("sys_attachment");
	gr.addQuery("table_name", "ZZ_YY" + current.getTableName());
	gr.addQuery("table_sys_id", current.sys_id);
	gr.query();
	if (gr.next()) {
		gr.table_name = current.getTableName();
		gr.update();
		new global.VariableUtil().copy(gr.sys_id, current.getTableName(), current.sys_id); 
	}

})(current, previous);

Regards
Ankur

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

View solution in original post

17 REPLIES 17

i have two attachment buttons on my form but this code will only work for one of them while the other attachment shows still as ZZ_YY

It sounds like you have two records that meet the GlideRecord criteria, so try changing the 

	if (gr.next()) {

 to 

	while (gr.next()) {

or else look at the attachment table to see how the record that isn't getting changed differs, then add that as a second GlideRecord query.  Also see the update that the copy function in the referenced Script Include has been renamed to copyAttachment.

 

i tried that and it causing the attachment to double meaning if i have 2 attachemtns from the two buttons it will be 4 attachment. duplicates

but i think i found the solution for it

here is my updated code


(function executeRule(current, previous /*null when async*/) {
// Add your code here
var attachmentIDs = [];

var gr = new GlideRecord("sys_attachment");
gr.addQuery("table_name", "ZZ_YY" + current.getTableName());
gr.addQuery("table_sys_id", current.sys_id);
gr.query();

// Collect the attachment sys_ids
while (gr.next()) {
attachmentIDs.push(gr.sys_id.toString());
}

// Copy attachments using the collected sys_ids
for (var i = 0; i < attachmentIDs.length; i++) {
var attachment = new GlideRecord("sys_attachment");
if (attachment.get(attachmentIDs[i])) {
new global.VariableUtil().copyAttachment(attachment.sys_id, current.getTableName(), current.sys_id);
}
}
})(current, previous);

Gabriel Alowa
Giga Contributor

As you can see below I follow instruction from the scrip but it is getting attached only in the ZZ_YY table_name. What Am I missing ?  should I change the table name in the scripts?

Thank you

find_real_file.png