How to attach files using suitescript

To attach a record or file to another record.

Here’s an example of how you can attach a file to a Vendor Bill using SuiteScript 2.0:

/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 */
define(['N/record'], function(record) {
   
    function afterSubmit(context) {
        if (context.type !== context.UserEventType.CREATE) {
            return;
        }

        var newRecord = context.newRecord;

        // Get the ID of the newly created Shared Vendor Bill
        var sharedVendorBillId = newRecord.id;

        // Assume you have the file ID and the target Vendor Bill ID
        var fileId = 56212;
        var vendorBillId = 276532;

        // Attach the file to the Vendor Bill
        var attachment = record.attach({
            record: { type: 'file', id: fileId },
            to: { type: 'vendorbill', id: vendorBillId }
        });

        log.debug('Attachment Result', attachment);
    }

    return {
        afterSubmit: afterSubmit
    };
});

Leave a comment

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