- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Friday - last edited Friday
Hey!
In a record producer I have a custom variable of type attachment called 'bifoga_fil':
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.
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,
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Monday
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! 🙏
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Friday
Hi @ronro2,
Business Rule setup:
Table: Incident (or your target table)
When: After
Insert: Checked
Condition: (leave empty)
(function executeRule(current, previous) {// Get the attachment variable valuevar attachmentVar = current.variables.bifoga_fil;if (!attachmentVar) {gs.info('No attachment found in bifoga_fil variable');return;}gs.info('Found attachment sys_id: ' + attachmentVar);// Method 1: Direct copy using GlideSysAttachmentvar sourceGr = new GlideRecord('sys_attachment');if (sourceGr.get(attachmentVar)) {var destTable = current.getTableName();var destSysId = current.getUniqueValue();gs.info('Copying to: ' + destTable + ' / ' + destSysId);// Create new attachment recordvar newAttach = new GlideRecord('sys_attachment');newAttach.initialize();newAttach.table_name = destTable;newAttach.table_sys_id = destSysId;newAttach.file_name = sourceGr.file_name;newAttach.content_type = sourceGr.content_type;newAttach.size_bytes = sourceGr.size_bytes;var attachId = newAttach.insert();// Copy the actual file datavar sourceDoc = new GlideRecord('sys_attachment_doc');sourceDoc.addQuery('sys_attachment', attachmentVar);sourceDoc.query();while (sourceDoc.next()) {var newDoc = new GlideRecord('sys_attachment_doc');newDoc.initialize();newDoc.sys_attachment = attachId;newDoc.data = sourceDoc.data;newDoc.insert();}gs.info('Attachment copied successfully!');}})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Friday
@pr8172510 hey!
Thanks for this, but keep in mind that bifoga_fil is a producer variable. So its producer.bifoga_fil and not current.
How does that change things? Do I need to create a variable on the target table? And then map that producer variable to the newly created variable on the table?
Greetings,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Friday
@ronro2 You're absolutely right.
If bifoga_fil is a Record Producer variable (not a field on the target table), then current.variables.bifoga_fil won't work because current is the Incident record, and it doesn't have that variable.
Record Producer variables live in the producer instance (the catalog item submission)
The target record (Incident) doesn't automatically get those variables as fields
You need to either:
Store the attachment variable on the Incident (via a field or variable set)
ORRead from the producer record directly
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Friday
@pr8172510 so I've created a new field on the target table, called u_attached_file and in the record producer, the variable called bifogad_fil is mapped to u_attached_file.
So how would the business rule now look? Or can I do this via Script field directly on the record producer?
