Skip to content

Instantly share code, notes, and snippets.

@abacijson
Created November 23, 2019 01:20
Show Gist options
  • Save abacijson/2f4410ed54c0b5085c1cb015008cf48a to your computer and use it in GitHub Desktop.
Save abacijson/2f4410ed54c0b5085c1cb015008cf48a to your computer and use it in GitHub Desktop.
Ss2_0DialogSample.js
/**
* @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
};
});
@gillyspy
Copy link

gillyspy commented Jun 21, 2022

I wrote a trivia question for my team and it looks very much the same. Somone was able to get the answer by finding your blog.

So i thought i would share what we use:

saveRecord(context) {
        const saveKey = Symbol.for('saveRecord');
        /**
         * @description Netsuite native proxy for clicking the save button.
         * @returns {*} N/A.
         */
        const clickSave = () => window.getNLMultiButtonByName('multibutton_submitter').onMainButtonClick(this);
        if (window[saveKey]) return true;

        requireAsync['P:N/ui/dialog']
          .then((dialog) => dialog.confirm({ title: 'Confirm', message: 'do you want to save?' }))
          .then((answer) =>{
               Object.assign(window, { [saveKey]: answer }));
               if( answer) clickSave();
          })
           // just in case OP cancels the native no-change-prompt that can occur
          .then(()=>Object.assign(window, {[saveKey] :false}));

        return window[saveKey] !== true;
      }
  • The saveRecord entry point function cannot be async (i.e. returns {Promise<boolean>} is not supported by netsuite in this case) -- as you implied
  • we always return a boolean (stronger typing for code quality)
  • We don't use named globals because it will pollute the namespace and our linters will bark.
  • You can effectively ignore the requireAsync call -- it's just part of our rapid bootstrapping process which requires modules in a memo-ized, promise pattern (i.e fully async)
  • this implementation doesn't require any external references and thus one can import it this method into their build process directly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment