SuiteScript 2.0 dialog.confirm Issue Workaround

dialog.confirm

dialog.confirm is SuiteScript 2.0 implementation of native JavaScript confirm function. This enhanced version contains the following new features:

  • Developers can use HTML to format the text shown to users
  • It inherits NetSuite look and feel for display
Native Javascript Version SuiteScript 2.0 Version

JavaScript Native Confirm Box

JavaScript Native Confirm Box

SS2.0 dialog.confirm box

SS2.0 dialog.confirm box

SuiteScript 2.0 confirm function Issue

One of the primary usage of JavaScript confirm function in NetSuite is to get the user to provide final confirmation prior to saving the record. It effectively pauses the execution of the script until the user provides an answer to the question. When SuiteScript 2.0 version of the confirm function is used to achieve the same goal, it fails.

The underlying reason for the failure is that SuiteScript 2.0 version returns a promise object and continues to execute the rest of the script instead of pausing the execution of the script until the answer is provided. When it is used in saveRecord client script trigger, SuiteScript 2.0 version will always return false regardless of option selected by the user and prevents them from saving the record.

Native Confirm process with saveRecord Trigger

When native Javascript confirm is used in saveRecord trigger, execution is paused until the answer is provided by the user. This allows developers to control the logic flow.

native-confirm-process-on-saverecord-trigger

native-confirm-process-on-saverecord-trigger

SuiteScript 2.0 Confirm process with saveRecord Trigger

When SS2.0 confirm is used, instead of pausing execution, it shows the dialog box with confirm options while continuing to execute the rest of saveRecord function.

SS2.0 Confirm Process on Save

SS2.0 Confirm Process on Save

Workaround Solution

As of this posting, this behavior is NOT considered as a defect.  Easy workaround is to use native Javascript confirm function, however, here is our alternate solution:

SS2.0 Confirm Process on Save Workaround

SS2.0 Confirm Process on Save Workaround

Below is sample code.

/**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 */
define(['N/ui/dialog'],
/**
 * @param {dialog} dialog
 */
function(dialog) {
    //finalResult and finalResultSet are global variables
    //accessible by all functions

        //User provided final answer from confirm box
    var finalResult = false
	
        //Flag to indicate if user provided final answer or not
	finalResultSet = false;
	
    //Save record trigger
    function saveRecord(context) 
    {
    	//If user have never provided a final answer,
    	if (!finalResultSet)
    	{
    		//Here you do your own saveRecord validation/automation.
        	//.....
        	//.....
        	//.....
        	//During validation/automation, 
    		//	if something fails, you would return false
    		//	which will never get to below line.
    		
    		//If everything pases, show the dialog box

    		dialog.confirm({
    			'title':'SS2.0 Confirm',
    			'message':'This is SuiteScript 2.0 version'+
                                  'Do you wish to continue?'
    		}).then(success).catch(fail);
    	}
    	//If user provided a final answer from confirm box, return out
    	else
    	{
    		//Reset the finalResultSet flag to false 
    		//	in case user selected "Cancel" on the confirm box.
    		finalResultSet = false;
    		
    		//return will either give the control back to user
    		//	or continue with saving of the record
    		return finalResult;
    	}
    }

    //Helper function called when user 
    //	selects Ok or Cancel on confirm box
    function success(result)
    {
    	//Sets value of finalResult to user provided answer
    	finalResult = result;
    	//Updates the finalResultSet flag to true
    	//	to indicate that user has made his/her choice
    	finalResultSet = true;
    	
    	//getNLMultiButtonByName function is undocumented client side
    	//	function call used by NetSuite when save button is clicked
    	//	triggering this function will simulate clicking Save button
    	//	programmatically and force NetSuite to trigger saveRecord again
    	//	with global variables finalResult and finalResultSet altered!
    	
    	//NS Hack to call simulate user clicking Save Button
    	getNLMultiButtonByName('multibutton_submitter').onMainButtonClick(this);
    }
    
    function fail(reason)
    {
    	return false;
    }
    
    return {
        saveRecord: saveRecord
    };
    
});

If you are having trouble with implementing solutions using SuiteScript 2.0, please feel free to contact us!

About Abaci
Abaci is a premier NetSuite Solution Provider with deep knowledge in both business process and technology architecture. Our team is passionate about NetSuite and has an exclusive commitment to NetSuite and to providing the highest level of system expertise on its full suite of cloud-based business management applications.