Attaching files programmatically is made hassle-free through SuiteScript.

You can choose to attach single or multiple file types to several kinds of NetSuite records. You can attach it directly to a record or through emails.

As of this writing, attaching emails via SuiteScript is supported via these record types:

TRANSACTIONS

Check, Custom Transaction, Expense Report, Inventory Adjustment, Item Fulfillment, Journal Entry, Intercompany Transfer Order, Opportunity, Transfer Order, Sales Order, Customer Payment, Return Authorization, Credit Memo, Cash Refund, Estimate, Invoice, Cash Sale, Purchase Order, Requisition, Customer Payment, Customer Refund, Customer Deposit, Vendor Bill, Vendor Credit, Vendor Return Authorization, Work Order, Work Order Issue, Work Order Close, Work Order Completion

ENTITIES
Customer, Contact, Employee, Partner, Vendor, Project, Group

ACTIVITIES
Task, Event, Phone Call, Project Task

NetSuite also has a wide range of supported files for attachment. Yes, even MP3 or MPEGMOVIE file types are supported. See the full list as of 2019.2 Release below:

APPCACHE, AUTOCAD, BMPIMAGE, CERTIFICATE, CONFIG, CSV, EXCEL, FLASH, FREEMARKER, GIFIMAGE, GZIP, HTMLDOC, ICON, JAVASCRIPT, JPGIMAGE, JSON, MESSAGERFC, MP3, MPEGMOVIE, MSPROJECT, PDF, PJPGIMAGE, PLAINTEXT, PNGIMAGE, POSTSCRIPT, POWERPOINT, QUICKTIME, RTF, SCSS, SMS, STYLESHEET, SVG, TAR, TIFFIMAGE, VISIO, WEBAPPPAGE, WEBAPPSCRIPT, WORD, XMLDOC, XSD, ZIP

Script Examples

Below you will see script samples for several scenarios. The samples are in SuiteScript 2.0 syntax.

Attaching a Single File to a Record Type

In this example, you will notice that the attachfile is called once. Note that for attaching files to record types, we are using a function called attachfile. The attachfile calls the NetSuite API record.attach.

function execute(context) {
     
            var txtfileid = 680; 
            var pdf1 = 483; 
            var pdf2 = 484; 
            var salesorderid = 1060;

            /** Attach Text File to the Sales Order Transaction **/
            attachfile('file',txtfileid, 'salesorder', salesorderid);
            /** End Attach Text File to the Sales Order Transaction **/
        }

        function attachfile(recType, recId, recTypeTo, recIdTo) {
            record.attach({
                record: {
                    type: recType,
                    id: recId
                },
                to: {
                    type: recTypeTo,
                    id: recIdTo
                }
            });
        }

Attaching Multiple Files to a Record Type

Since the record.attach does not support having an array of files, the only different thing we’re doing is calling this API multiple times while supplying a different file id.

        function execute(context) {
         
            var txtfileid = 680; 
            var pdf1 = 483; 
            var pdf2 = 484; 
            var salesorderid = 1060;

            /** Attach PDF File in the Sales Order Transaction **/            
            attachfile('file', pdf1, 'salesorder', salesorderid);
            attachfile('file', pdf2, 'salesorder', salesorderid);
            /** End Attach PDF File in the Sales Order Transaction **/
        }

        function attachfile(recType, recId, recTypeTo, recIdTo) {
            record.attach({
                record: {
                    type: recType,
                    id: recId
                },
                to: {
                    type: recTypeTo,
                    id: recIdTo
                }
            });
        }

Attaching a Single File to an Email

Attaching files to an email is done by calling file.load and supplying the result to the email.send API.

        function execute(context) {

            var pdf1 = 483;
            var pdf2 = 484; 

            var pdffile2 = file.load({id:pdf2});

            email.send({
                author: 343,
                recipients: ['brianestavilla@gmail.com'],
                subject: 'Test Email with Attachements',
                body: 'email body',
                attachments: [pdffile2]
            });

            /** END SEND EMAIL WITH MULTIPLE ATTACHEMENTS **/
        }

        function attachfile(recType, recId, recTypeTo, recIdTo) {
            record.attach({
                record: {
                    type: recType,
                    id: recId
                },
                to: {
                    type: recTypeTo,
                    id: recIdTo
                }
            });
        }

Attaching Multiple Files to an Email

When you want to attach multiple files, all you have to do is to fill the array on the attachments parameter and load the files separately.

        function execute(context) {
      
            var pdf1 = 483;
            var pdf2 = 484; 

            /** SEND EMAIL WITH MULTIPLE ATTACHEMENTS **/
            var pdffile1 = file.load({id:pdf1});
            var pdffile2 = file.load({id:pdf2});

            email.send({
                author: 343,
                recipients: ['brianestavilla@gmail.com'],
                subject: 'Test Email with Attachements',
                body: 'email body',
                attachments: [pdffile1, pdffile2]
            });

            /** END SEND EMAIL WITH MULTIPLE ATTACHEMENTS **/
        }

        function attachfile(recType, recId, recTypeTo, recIdTo) {
            record.attach({
                record: {
                    type: recType,
                    id: recId
                },
                to: {
                    type: recTypeTo,
                    id: recIdTo
                }
            });
        }

LEACC Consulting is a team of NetSuite Certified Developers with IT experience solely dedicated to NetSuite technologies for eight years. We are experts in both SuiteScript 2.0 and SuiteScript 1.0. Our team has worked with 400+ NetSuite scripts and workflows combined.

Do you have NetSuite Development needs? Do you need help with a solution like this? Click on the link below and let’s have a chat about your project requirements!

Contact us for your NetSuite needs!

Leave a Reply