SuiteScript 2.0 Dialog Prompt + On SaveRecord

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

jsprompt

Ext JS

extjsprompt

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):


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:

I hope that was useful for you. Don’t forget to leave a comment if you have any questions!

  • Dilip DiLee

    can i create custom button
    in which
    onclick of that button it should redirect to suitelet
    and suitelet(should open like a modal) ???

  • Dilip DiLee

    how can i create a modal popup on clicking a button in netsuite ( for window.open)

    • Hey, sorry for taking so long to reply, I have been pretty busy lately. I am sure it’s possible but I have never done it. Popups work for me.

  • assem bayahi

    Thks! this is great!

  • Shivam

    Hello Adolfe
    I want to implement the dialog confirmation message at PO Line Item validation on instead of Save Record but its not working ..Could you please suggest how we can implement at line level validation?
    Please see the below code;
    /**
    * @NApiVersion 2.0
    * @NScriptType ClientScript
    * @NModuleScope Public
    */
    define(['N/ui/dialog'],
    function (dialog)
    {
    var DIALOGMODULE = dialog;
    var IS_CONFIRMED; //This global is used to detect if user has pressed "OK" in the prompt box

    //*********** HELPER FUNCTIONS ***********
    function validateLineRecord(context)
    {
    try
    {
    //If user clicked "OK" in prompt box then save, else show the confirmation box if record passes validation
    log.debug("1- Validate On Save!", "DIALOGMODULE = "+JSON.stringify(DIALOGMODULE)+", IS_CONFIRMED= "+IS_CONFIRMED);
    if(IS_CONFIRMED)
    {
    return true;
    }
    else
    {
    if(!validateSaveRecord(context))
    {
    //Here you do your own saveRecord validation/automation.
    return false;
    }
    else
    {
    var msgStr="Please confirm your choice!";

    var cpBtn1 =
    {
    label: 'Yes',
    value: 1
    };
    var cpBtn2 = {
    label: 'No',
    value: 2
    };

    var options = {
    title: 'Please confirm your choice!',
    message: msgStr,
    buttons: [cpBtn1, cpBtn2]
    };
    dialog.create(options).then(handlePromptResult).catch(fail);

    }
    }
    }
    catch (ex)
    {
    log.error({
    title: "Exception raised on VB line-item validate!",
    details: ex.message
    });
    }

    }
    //*********** HELPER FUNCTIONS ***********
    function handlePromptResult(buttonClicked)
    {

    if(buttonClicked !== 1)
    {
    return;
    }

    IS_CONFIRMED = true;
    // getNLMultiButtonByName('multibutton_submitter').onMainButtonClick(this); //NS Hack to call simulate user clicking Save Button
    getNLMultiButtonByName('expense_addedit').onMainButtonClick(this);
    }

    function validateSaveRecord(context)
    {

    var recObj= context.currentRecord
    var recSubList=context.sublistId;
    var boolFlag=false;
    var memo = recObj.getValue('memo');
    if(memo!=null && memo!='')
    {
    boolFlag=true;
    }
    else
    {
    alert("Please enter memo value");
    boolFlag=false;
    }
    log.debug("final Line Validate!", "memo = "+memo+", boolFlag= "+boolFlag);
    return boolFlag;
    }
    function fail(reason)
    {
    return false;
    }

    return {
    validateLine: validateLineRecord,

    };

    });

  • getNLMultiButtonByName(‘multibutton_submitter’).onMainButtonClick(this); //NS Hack to call simulate user clicking Save Button Can i know to how to hack the standard Netsuite function?

    • Sorry, I am not following. What standard Netsuite function? What are you trying to do?

  • Tyn

    Hi ,

    When I use this

    Ext.Msg.prompt("My Prompt Title", "Please enter your text:", handlePromptResult, this);

    portion of the code, I got the error

    org.mozilla.javascript.EcmaError: ReferenceError: “Ext” is not defined.

    can you help me?

    • Seems like the page in which you are running the clientscript doesn’t have the Ext library loaded for some reason. I just tested this on a Sales Order and worked fine. In which record is your code running on?

  • Chidiebere Okwudire

    Use the NetSuite Input Dialog if you care about a native experience.

  • hazel hernandez

    hi Adolfe,
    This works great!
    I just noticed though, when using this, and there are mandatory fields on form that you did not set, your script will get triggered first before the standard netsuite popup. , and if you select yes/ok, will then show the netsuite standard pop up message to fill in the mandatory field. If you try to submit again, the code will no longer show the pop-up message but will go directly on save.

  • ntbm

    Hi, would just like to share my observation in using : getNLMultiButtonByName(‘multibutton_submitter’).onMainButtonClick(this);

    This seems to be working only when using Admin role. I tried using a custom one and the Save button has a different onclick value (which does not seem to work when applied unlike this one)

    Hope you can share if you have solution for non-Admin role.

    Thanks!