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

pr8172510
Tera Guru

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 value
        var 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 GlideSysAttachment
        var 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 record
            var 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 data
            var 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);


    pr8172510_0-1777028388227.png

     

    pr8172510_1-1777028414479.png

     

     

 

ronro2
Tera Contributor

@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, 

@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)
      OR 

    •  Read from the producer record directly

ronro2
Tera Contributor

@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?