SuiteScript 2.0 Dialog Prompt + On SaveRecord
One of the new modules in Suitescript 2.0 is the “N/ui/dialog” module but unfortunately it doesn’t include a prompt method. I did some digging and found that the dialog module uses Sencha Ext JS. Since Ext JS doesn’t require the dialog module it can run on both SS 1.0 and 2.0!
Here’s how they look like side by side:
Native JS
Ext JS
Note: This is a hack.
Here’s the code sample on how to achieve this (Works on both SS 1.0 and SS 2.0):
1 2 3 4 5 6 7 8 |
function showPrompt(){ Ext.Msg.prompt("My Prompt Title", "Please enter your text:", handlePromptResult, this); function handlePromptResult(buttonClicked, text) { if (buttonClicked !== 'ok') { return; } console.log("You entered: " + text); } } |
Additionally, thanks to this post from Abaci I was able to run this code right before the record is saved, which can be very useful to set the value of a field right before saving.
Here’s the code sample running on SaveRecord in SS 2.0:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
/** * @NApiVersion 2.0 * @NScriptType ClientScript * @NModuleScope Public */ define(['N/ui/dialog'],runClientScript); function runClientScript(dialog){ var DIALOGMODULE = dialog; var IS_CONFIRMED; //This global is used to detect if user has pressed "OK" in the prompt box var returnObj = {}; returnObj.saveRecord = saveRecord; return returnObj; //*********** HELPER FUNCTIONS *********** function saveRecord(context) { //If user clicked "OK" in prompt box then save, else show the confirmation box if record passes validation if(IS_CONFIRMED){ return true; } else { if(!validateSaveRecord()){ //Here you do your own saveRecord validation/automation. return false; } Ext.Msg.prompt("My Prompt Title", "Please enter your text:", handlePromptResult, this); } //*********** HELPER FUNCTIONS *********** function handlePromptResult(buttonClicked, text) { if (buttonClicked !== 'ok') { return; } console.log("You entered: " + text); IS_CONFIRMED = true; getNLMultiButtonByName('multibutton_submitter').onMainButtonClick(this); //NS Hack to call simulate user clicking Save Button } function validateSaveRecord(){//Here you do your own saveRecord validation/automation. return true; //Return true if you want to proceed with the save. } } } |
I hope that was useful for you. Don’t forget to leave a comment if you have any questions!