Built something you're proud of? Tell the story. A quick G2 review of App Engine or Build Agent helps other developers see what's possible on ServiceNow. Share your experience.

Copy custom variable in record producer to OOTB record producer attachment

ronro2
Tera Contributor

Hey! 

In a record producer I have a custom variable of type attachment called 'bifoga_fil':

ronro2_0-1777024047068.png



So I want to copy this to the OOTB attachment on the record producer in the back-end, so that the attachment is visible in the banner above and in the logs. 

ronro2_0-1777023445003.png


I've marked what I mean with red. 

So, I am thinking an after insert business rule of some kind? What would that look like? 

I'm thankful for your help. 

All good, 

1 ACCEPTED SOLUTION

@ronro2 

the file should get attached to the record if you used the record producer script

💡 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  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

29 REPLIES 29

@ronro2 Perfect  

Since you've now mapped the variable to u_attached_file on the Incident table

(function executeRule(current, previous) {

// Get the attachment sys_id from the new field
var attachmentId = current.u_attached_file;

if (!attachmentId) {
return;
}

// Get the source attachment
var sourceAttach = new GlideRecord('sys_attachment');
if (!sourceAttach.get(attachmentId)) {
return;
}

// Create new attachment on the Incident record
var newAttach = new GlideRecord('sys_attachment');
newAttach.initialize();
newAttach.table_name = current.getTableName();
newAttach.table_sys_id = current.getUniqueValue();
newAttach.file_name = sourceAttach.file_name;
newAttach.content_type = sourceAttach.content_type;
newAttach.size_bytes = sourceAttach.size_bytes;
var newId = newAttach.insert();

// Copy the file data
var sourceDoc = new GlideRecord('sys_attachment_doc');
sourceDoc.addQuery('sys_attachment', attachmentId);
sourceDoc.query();

while (sourceDoc.next()) {
var newDoc = new GlideRecord('sys_attachment_doc');
newDoc.initialize();
newDoc.sys_attachment = newId;
newDoc.data = sourceDoc.data;
newDoc.insert();
}

// Optional: Clear the field so it doesn't stay as a reference
// current.u_attached_file = '';
// current.update();

})(current, previous);


Business Rule setup :

  • When: After

  • Insert: Checked

  • Condition: current.u_attached_file != null

 

 

ronro2
Tera Contributor

@pr8172510 I get these two error messages: 

- "Read operation against 'sys_attachment_doc' from scope 'x_vgll_rail' has been refused due to the table's cross-scope access policy" 

- "Create operation against 'sys_attachment' from scope 'x_vgll_rail' has been refused due to the table's cross-scope access policy"

ronro2_0-1777033344680.png


So the business rule, and the target table are in a scoped application, and not in the Global one. 

 

 

Ankur Bawiskar
Tera Patron

@ronro2 

but why not use OOTB attachment type variable and let user upload file to it

Then add this script in record producer script to copy file to target record

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

💡 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  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar Because it is needed to be obligatory in some scenarios (the upload file variable), depending on choices in the record producer. 

 

You understand me? 

So if this is anything, then attachment becomes mandatory, like in the picture: 

ronro2_0-1777033845245.png

 

@ronro2 

you can use catalog UI policy/catalog client script to make attachment variable mandatory based on your condition

what's the challenge?

💡 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  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader