Marty Zigman

Conversations with Marty Zigman

Certified Administrator • ERP • SuiteCloud

Dynamically Draw and Execute a Form Button using NetSuite SuiteScript 2.0

NetSuite Technical

Tags:

This article is relevant if you want to dynamically add a button to a form which will execute more than one NetSuite Suitelet.

Background

On our customer records, we set a terms we call “Retainer” .  I want to have a button draw on the customer record if a retainer is required so that I can generate a request for retainer fees.   However, if no retainer is required, I do not want a button.  The amount of the retainer needs to be updated by inspecting the underlying invoice and payment records.  Thus, when I click the button, I want to run a custom SuiteLet to calculate a value and then I want to run another SuiteLet to pop up a window to draw the custom PDF.  Yes, I can put these two functions in one SuiteLet, but the programs are designed to run independently for other reasons.

SuiteScript 2.0 Draw Button Approach

While I could use workflows to draw buttons, I favor SuiteScript in general as I feel I have maximum capacity over the situation.

Using the BeforeLoad User event, I can test for a situation and add a button.  Yet adding a button is insufficient. The button must do something when clicked.

Using our Content Renderer Engine (CRE) , I can produce a pop up window that displays a nicely formatted PDF file.  The way to create the CRE SuiteLet is covered elsewhere. But how can we get the button to fire the SuiteLet?  In my article, Bind NetSuite SuiteScript Client Side Code in Form View Mode, I offer a technique to lay down some dynamic code that can allow you to hook up a script to a button.  But there is an easier way with SuiteScript 2.0.

Sample SuiteScript 2.0 code to Dynamically Create Button to Fire Two SuiteLets

In the code example below, using SuiteScript 2.0 it is relatively easy to see how you can hook up a button to fire not one, but two SuiteLets.  The first Suitelet does not have a user interface and performs work server side.  The second SuiteLet will draw in a new browser window and display its output.

const CREPROFILE = 65; 

//get the url for the suitelet #1; add customer parameter recId based on this ID
var buildURL = url.resolveScript({
 'scriptId':'customscript_sl_build_inf',
 'deploymentId':'customdeploy_sl_build_inf',
 'returnExternalUrl': true
}) + '&recId=' + REC.getValue("id");

//get the url for the suitelet #2; use similar pattern but add more data 
var creURL = url.resolveScript({
 'scriptId':'customscript_cre_profile_suitelet_exampl',
 'deploymentId':'customdeploy_test_cre',
 'returnExternalUrl': false
}) + '&profileid=' + CREPROFILE + '&id=' + REC.getValue("id");

//hook point for the button that will be fired client side;
var scr = "require(['N/https'], function(https) {
 https.get({'url': '" + buildURL + "'}); //call suitelet #1 
 window.open('"+creURL+"','_blank'); //call suitelet #2
 });";

// log.debug(funcName, scr);

//add button to the form; when clicked, call the scr function 
context.form.addButton({
 id : "custpage_cre_view_agreement",
 label : "Print Retainer",
 functionName: scr
})

Work with Senior NetSuite Professionals

The example was produced by one of our Senior consultants. I appreciate the consultant’s talent and we love to help the NetSuite community get more from their NetSuite investment. If you are strong on NetSuite and would like to work with a small team of passionate professionals that hold high standards for care, let’s have a conversation.

Marty Zigman LinkedIn

Marty Zigman

Holding three official certifications, Marty is widely recognized as a top NetSuite expert and leads a team of senior professionals at Prolecto Resources, Inc. A former Deloitte & Touche CPA and technology executive with CTO roles, he brings over 35 years of leadership in ERP, CRM, and eCommerce business systems. Contact Marty to engage directly.

BiographyYouTubeLinkedInX (Twitter)

7 thoughts on “Dynamically Draw and Execute a Form Button using NetSuite SuiteScript 2.0

  1. Hi MArty,
    In NetSuite, How Do I Print Related Records in Advanced PDF/HTML Template,i used like below,

    type
    total

    ${lineitem.type}
    ${lineitem.total}

    can you please help me.

    Reply

Leave a Reply

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