Don’t Miss it: Top Strategies for Driving Diversity

days / hours / minutes / seconds

Register Here »
NetSuite logo
NETSUITE RESOURCES
workplace by meta logo
FUTURE
OF WORK WORKPLACE BY META
University icon
CUSTOMER EDUCATION blog
Atlassian Logo
Adoption blog
How-to
November 2, 2023
|
9.5 min
reading time

SuiteScript 2.0: Using Scripting to Attach Files to Records and Emails

Share this article
/*SoMe buttons*/ <-- Facebook Share Button Code --> <-- Facebook Share Button Code --> <-- Twitter Share Button Code --> <-- Twitter Share Button Code --> <-- Linkedin Share Button Code --> <-- Linkedin Share Button Code -->

If you want a hassle-free approach to attaching files programmatically, NetSuite SuiteScript might be for you. With SuiteScript, you can attach single or multiple file types to several kinds of NetSuite records. Attachments can be added directly to a record or through emails.

At this time, 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 and Work Order Completion.

Entities

Customer, Contact, Employee, Partner, Vendor, Project and Group.

Activities

Task, Event, Phone Call and Project Task.

Files

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 and ZIP.

Script Examples

Below, you will see script samples for several scenarios. The samples are also available 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 in the script. 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 difference here 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

If you want to attach multiple files, all you have to do is 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
                }
            });
        }

Find the Right Partner for Your NetSuite Needs

With ServiceRocket, you’ll get certainty, expertise and success built into your NetSuite implementation, configuration and custom development. When you work with us, you get access to NetSuite-trained Certified Developers with over 400+ NetSuite scripts and 50+ Suitelet workflows combined. Rest assured that ServiceRocket will help you find success and provide you with the opportunities you need to transform your business.

Do you have NetSuite license management, implementation, administration, support or custom development needs? We’ve got your back! Contact our NetSuite Certified Experts today.

Ensure the highest return on your NetSuite investment with our custom development or administrative services.

Learn MoreServiceRocket Backed - We've got your back badge