Debugging Deployed SuiteScript 2.1 User Event Scripts

SuiteScript 2.1 user event scripts can be debugged using the 2.1 Debugger similar to other supported server script types (scheduled scripts and Suitelets) as described in Debugging Deployed SuiteScript 2.1 Scheduled Scripts and Suitelets. However, because a user event script can have multiple entry points, there are some special steps to take when debugging SuiteScript 2.1 user event scripts.

You can debug every entry point included in your SuiteScript 2.1 user event script within the same browser session, however, you must reattach to Chrome DevTools for each entry point included in the script.

Note:

SuiteScript does not support read-only sublists. If you are debugging a script that loads a record with a custom child record as a sublist, make sure the Allow Child Record Editing setting is checked for the child record in SuiteBuilder. If this box is not checked, the sublist is read-only and will not load in the parent record. See Creating Custom Record Types for additional information about creating custom records.

Following is an example of debugging a user event script deployed on the Customer record. You first need to create the user event and start the debugger before you can debug the script.

To create the user event script and start the debugger:

  1. Create your user event script file and upload it to the File Cabinet. An example script is shown here:

                    /**
     * @NApiVersion 2.1
     * @NScriptType UserEventScript
     */
    define (['N/record'], function (record) {
        function beforeLoad(context) {
            if (context.type !== context.UserEventType.CREATE)
                return;
            
            var customerRecord = context.newRecord;
    
            customerRecord.setValue('phone', '555-555-5555');
    
            if (!customerRecord.getValue('salesrep')) {
                customerRecord.setValue('salesrep', 59); // replace 59 with a value specific to your account
            }
        }
    
        function beforeSubmit(context) {
            if (context.type !== context.UserEventType.CREATE)
                return;
    
            var customerRecord = context.newRecord;
    
            customerRecord.setValue('comments', 'Please follow up with this customer!');
        }
    
        function afterSubmit(context) {
            if (context.type !== context.UserEventType.CREATE)
                return;
    
            var customerRecord = context.newRecord,
    
            if (customerRecord.getValue('salesrep')) {
                var call = record.create({
                    type: record.Type.PHONE_CALL,
                    isDynamic: true
                });
    
                call.setValue('title', 'Make follow-up call to new customer');
                call.setValue('assigned', customerRecord.getValue('salesrep'));
                call.setValue('phone', customerRecord.getValue('phone'));
    
                try {
                    var callId = call.save();
                    log.debug('Call record created successfully', 'Id: ' + callId);
                } catch (e) {
                    log.error(e.name);
                }
            }
        }
    
        return {
            beforeLoad: beforeLoad,
            beforeSubmit: beforeSubmit,
            afterSubmit: afterSubmit
        }
    }); 
    
                  
  2. Create a script record for your script. For this example, set the Name field to Debug Customer w/ 2.1 UE and set the Applies To: field to Customer.

  3. To have the script appear on the debugger tab, it must be executed by creating and saving a new Customer record (List > Relationships > Customers > New).

  4. Go to Customization > Scripting > Script Debugger.

  5. Select Debug Existing and select the Debug Customer w/ 2.1 UE user event script.

    Debug Existing SuiteScript 2.1 script options.
  6. Click Select and Close. The Script Debugger will wait for a user action to proceed:

    Script Debugger Waiting for User Action indicator.
  7. Follow specific instructions for debugging each entry point.

To debug the beforeLoad entry point:

  1. Follow the steps above to create the user event script and start the debugger.

  2. To trigger the beforeLoad entry point, either create a new Customer or edit an existing Customer.

  3. When the customer record is loaded, a new Chrome DevTools tab opens in your browser showing your user event script ready to be debugged using the 2.1 Debugger:

    Chrome DevTools with script initially loaded and ready to be debugged.
  4. The execution of your script stops at the top of the script. You can now add breakpoints, watches, etc. and begin debugging your script. You can also add a debugger; statement at the top of the beforeLoad entry point code to specifically stop the execution of the script at that entry point.

  5. When you have finished stepping over/playing through the last line of the beforeLoad entry point code, the Script Debugger tab will show:

    Script Debugger Completed Deployed Script Execution indicator for SuiteScript 2.1 script.

    And you will see a message in the Chrome DevTools tab that indicates the debugging connection was closed:

    Script Debugger Waiting for User Action indicator for SuiteScript 2.1 scripts.

    You do not need to click the Reconnect DevTools button on this message. If you want to reexecute the beforeLoad entry point in your script, save the Customer record to restart the debugging session

To debug the beforeSubmit and afterSubmit entry points:

  1. Go to Customization > Scripting > Script Debugger, or re-execute the script if you are already in the Script Debugger from debugging the beforeLoad entry point.

  2. Create a new Customer or edit an existing Customer. Do not save the record yet. You first need to get ready to debug the script, as shown in the next steps, before clicking Save.

  3. Select Debug Existing and select the Debug Customer w/ 2.1 UE user event script:

    Debug Existing SuiteScript 2.1 script options.
  4. Click Select and Close. The Script Debugger will wait for a user action to proceed:

    Debugging Deployed SuiteScript 2.1 User Event Scripts page
  5. To trigger the beforeSubmit entry point, save the Customer record. A new Chrome DevTools tab opens in your browser showing your user event script ready to be debugged using the 2.1 Debugger:

    Script Debugger with indication of waitng to devtools to reattach.
  6. The execution of your script stops at the top of the script. You can now add breakpoints, watches, etc. within the beforeSubmit entry point and begin debugging your script. You can also add a ‘debugger;’ statement at the top of the beforeSubmit and afterSubmit entry point code to specifically stop the execution of the script at that entry point.

  7. After the last line of the beforeSubmit function executes, the following message will appear in the Chrome DevTools tab:

    Debugging connection closed notification.

    And the Script Debugger shows:

    Debugging Deployed SuiteScript 2.1 User Event Scripts page
    Note:

    This reattach phase only appears if you have both a beforeSubmit and an afterSubmit entry point in your user event script. If you have only the beforeSubmit entry point or the afterSubmit entry point, your debugging session will end at the completion of the entry point code.

  8. To reattach the debugger and continue to debug your afterSubmit entry point code, click the Reconnect DevTools button on the message. The Chrome DevTools tab will reload your script and execution will stop at the beginning of the afterSubmit entry point code. You can now add breakpoints, watches, etc. and begin debugging your script.

  9. When you have finished stepping over/playing through the last line of the afterSubmit entry point code, the Script Debugger tab will show:

    Script Debugger Completed Deployed Script Execution.

    And you will see a message in the Chrome DevTools tab that indicates the debugging connection was closed:

    Debugging connection closed notification.

    You do not need to click the Reconnect DevTools button on this message. If you want to re-execute the any entry point in your script, you will need to reload a customer record and follow the steps to debug the beforeLoad entry point or to debug the beforeSubmit and afterSubmit entry points.

Related Topics

SuiteScript Debugger
2.1 Script Debugger Overview
Debugging Deployed SuiteScript 2.1 Server Scripts
Debugging Deployed SuiteScript 2.1 Scheduled Scripts and Suitelets

General Notices