Attaching mutliple files to SalesOrder using record.attach

We can attach multiple files to salesorder using record.attach that can be attached in a user event script.


// Track the number of attached files
            var attachedFileCount = 0;


            // Run the search and attach each file found
            fileSearchObj.run().each(function(result) {
                var fileId = result.getValue({ name: 'internalid' });


                // Load the file from the File Cabinet
                var fileObj = file.load({
                    id: fileId
                });


                // Attach the file to the Sales Order record
                record.attach({
                    record: {
                        type: 'file',
                        id: fileObj.id
                    },
                    to: {
                        type: salesOrder.type,
                        id: salesOrder.id
                    }
                });


                log.debug('File Attached', 'File ID ' + fileObj.id + ' attached to Sales Order ID ' + salesOrder.id);


                // Increment the count of attached files
                attachedFileCount++;


                return true; // Continue to the next result to attach all files
            });


            log.debug('Total Files Attached', attachedFileCount + ' files attached to Sales Order ID ' + salesOrder.id);

Leave a comment

Your email address will not be published. Required fields are marked *