render.transaction(options) to creae a PDF or HTML file object of a transaction and sending it via email.

render.transaction(options)

Syntax:

//Add additional code
...
var transactionFile = render.transaction({
    entityId: 23,
    printMode: render.PrintMode.HTML,
    inCustLocale: true
    });
...
//Add additional code 

The script given below is an example of creating a pdf file of the transaction record and sending it via email.

Send sales order pdf to the customers daily; the script only considers sales orders created that day. The sender is the admin, and the recipient is each customer. The email must include the pdf print attachment.

/**
 * @NApiVersion 2.1
 * @NScriptType ScheduledScript
 */
define(['N/email', 'N/record', 'N/render', 'N/search'],
    /**
 * @param{email} email
 * @param{record} record
 * @param{render} render
 * @param{search} search
 */
    (email, record, render, search) => {
        /**
         * Defines the Scheduled script trigger point.
         * @param {Object} scriptContext
         * @param {string} scriptContext.type - Script execution context. Use values from the scriptContext.InvocationType enum.
         * @since 2015.2
         */
        const execute = (scriptContext) => {
            
    // Set search filters to fetch sales orders created today
    var salesOrderSearch = search.create({
      type: search.Type.SALES_ORDER,
      filters: [
        ['trandate', 'on', 'today'],'AND',['mainline','is','T'] // Sales orders created today
      ],
      columns: ['internalid', 'entity', 'tranid'] // Columns to retrieve
    });
    
    // Execute the search
    var salesOrderResults = salesOrderSearch.run().getRange({
      start: 0,
      end: 100 // Adjust the range based on your expected number of sales orders
    });
    
    // Loop through each sales order found
    salesOrderResults.forEach(function (result) {
      var salesOrderId = result.getValue({ name: 'internalid' });
      var customerName = result.getValue({ name: 'entity' }); // Get the customer name
      var salesOrderNum = result.getValue({ name: 'tranid' }); // Get the sales order number
     
      
      // Load the sales order record
      var salesOrder = record.load({
        type: record.Type.SALES_ORDER,
        id: salesOrderId
      });
      
      // Generate PDF for the sales order
      var salesOrderPdf = render.transaction({
        entityId: parseInt(salesOrderId),
        printMode: render.PrintMode.PDF
      });
     
     var cusRec= record.load({
        type:record.Type.CUSTOMER,
        id:customerName
      });
      var cusEmail=cusRec.getValue({
        fieldId:'email'
      })
      // Send email to the customer with the sales order PDF attached
      email.send({
        author: -5, // Admin user's internal ID
        recipients: cusEmail, // Use the customer's email field here
        subject: 'Your Sales Order ' + salesOrderNum,
        body: 'Dear Customer,nnPlease find your sales order attached.nnBest regards,nAdmin',
        attachments: [salesOrderPdf],
        relatedRecords: {
          transactionId: salesOrderId // Attach the sales order to the email
        }
      });
    });
        }
        return {execute}
    });

Leave a comment

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