Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Copy attachment added via service catalog variable

michelhanna
Kilo Guru

Hi,

I'm trying to copy an attachment file from a service catalog attachment variable.

Once the request is submitted, looking at the sys_attachment table, I see the attachment record is added but the table name is ZZ_YYsc_req_item rather than sc_req_item!

I tried both of the below to copy the file with no luck 

// try ZZ_YYsc_req_item
GlideSysAttachment.copy('ZZ_YYsc_req_item','sourceSYSID','targetTable','targetSYSID');
//and try sc_req_item
GlideSysAttachment.copy('sc_req_item','sourceSYSID','targetTable','targetSYSID');

EDIT:The copy happens but the table name on the new attachment copy starts with ZZ_YY, even when I don't use that string.

Any ideas how can I copy the attachment variable to a record without the table name starts with ZZ_YY?

Thanks,

1 ACCEPTED SOLUTION

michelhanna
Kilo Guru

So, the root cause here is GlideSysAttachment.copy will always cause the new copy to have a table name starts with ZZ_YY for attachments variables, which according to this KB0868420 won't show on the record header.

Two options here to resolve this:

  1. Rename the table name field to remove the ZZ_YY
  2. Use this alternative copy: new global.VariableUtil().(attachmentId, targetTable, targetId); which does NOT add ZZ_YY.

View solution in original post

13 REPLIES 13

This is what seems working:

	var attachment = new global.GlideQuery('sys_attachment')
	.where('table_sys_id', current.sys_id)
	.where('file_name', current.<field_name>.getDisplayValue())
	.selectOne('table_sys_id')
	.get();

	new global.VariableUtil().copyAttachment(attachment.sys_id, 'sys_data_source', dataSourceID);

This code does not work on my side (tested in a background script - with relevant values-, in "run script" of the workflow - with relevant references-... This issue is actually refering to the syntax, a function name is expected.


new global.VariableUtil().(attachmentId, targetTable, targetId);

 

XML runtime not available

 

The right function call is :

new global.VariableUtil().copyAttachment(attachmentId, targetTable, targetId);

( https://sn.jace.pro/script-includes/variableutil/ ) 

where do I provide this code? in a BR on the target table or in the record producer script?