Usage of "Attachment" variable for Incident Record Producers

richardyaxl
Tera Expert

I have added an "Attachment" type variable to a Record producer form that is used for raising Incidents. However, when a user adds an attachment via this variable it is not shown on the generated Incident. This seems to be because the "table name" field on the sys_attachment record that is created has a value of "ZZ_YYIncident". If I change this to "Incident" then the attachment is displayed on the record. I have tested this on a PDI and experienced the same behaviour.

Please could someone help me to understand what is the reason for this, and what is the recommended approach for configuring the Attachment variable? 

2 ACCEPTED SOLUTIONS

Brad Bowman
Kilo Patron
Kilo Patron

Attachments using a variable show up in the Activity Stream.  If you want it to appear at the top of the form you can create a Business Rule that runs async on the incident table when a record is inserted.  The script will look for an attachment, and change the table name if it is ZZ_YY...

(function executeRule(current, previous /*null when async*/) {
	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().copyAttachment(gr.sys_id, current.getTableName(), current.sys_id); 
	}

})(current, previous);

 

View solution in original post

Ankur Bawiskar
Tera Patron
Tera Patron

@richardyaxl 

yes the file on attachment variable won't be copied OOTB to target record

I created recently a blog for the same on how to do

Copying Attachments from a Record Producer's Attachment Variable to a Target Record in ServiceNow 

Sharing script here which you can write in record producer script

// pass the attachment variable name
// pass the target table name associated with the record producer

new global.VariableUtil().copyAttachment(producer.<variableName>, '<tableName>', current.sys_id);

// repeat this line again for another variable if required

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

View solution in original post

3 REPLIES 3

Brad Bowman
Kilo Patron
Kilo Patron

Attachments using a variable show up in the Activity Stream.  If you want it to appear at the top of the form you can create a Business Rule that runs async on the incident table when a record is inserted.  The script will look for an attachment, and change the table name if it is ZZ_YY...

(function executeRule(current, previous /*null when async*/) {
	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().copyAttachment(gr.sys_id, current.getTableName(), current.sys_id); 
	}

})(current, previous);

 

Ankur Bawiskar
Tera Patron
Tera Patron

@richardyaxl 

yes the file on attachment variable won't be copied OOTB to target record

I created recently a blog for the same on how to do

Copying Attachments from a Record Producer's Attachment Variable to a Target Record in ServiceNow 

Sharing script here which you can write in record producer script

// pass the attachment variable name
// pass the target table name associated with the record producer

new global.VariableUtil().copyAttachment(producer.<variableName>, '<tableName>', current.sys_id);

// repeat this line again for another variable if required

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

@richardyaxl 

Thank you for marking my response as helpful.

As per new community feature you can mark multiple responses as correct.

💡 If my response helped, please mark it as correct as well so that this helps future readers find the solution faster! 🙏

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