Dynamically Draw and Execute a Form Button using NetSuite SuiteScript 2.0

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.

Be Sociable, Share!

Marty Zigman

Holding all three official certifications, Marty is Southern California's NetSuite expert and leads a team of senior professionals at Prolecto Resources, Inc. He is a former Deloitte & Touche CPA and has held CTO roles. For over 25 years, Marty has produced leadership in ERP, CRM and eCommerce business systems. Contact Marty to setup a conversation.

More Posts - Website - Twitter - Facebook - LinkedIn - Google Plus - YouTube

| Tags: | Category: NetSuite, Technical | Leave a comment

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>