Having covered getting record field values in the previous scripting tutorial, in this tutorial we will talk about the basics of SuiteScript logging and debugging, which enable you to figure out problems that come up when you are scripting. If you have been following along with this tutorial series, you may already have run into script errors or other issues that caused your script not to work. So before we get much deeper into suitescripting, let’s look at some ways to troubleshoot your script when you run into an issue.

SuiteScript Logs

The best way to troubleshoot a script is to add logs throughout the script to help you understand what is really going on. To add logs to your script, you will need to use the log module. This module is automatically available in every script, but we will add it to this script just like the other modules. Below is where we left off in the last tutorial with the “N/log” module added in:

define(['N/record','N/search','N/ui/dialog', 'N/log'], function (record, search, dialog, log) {
    /**
    *@NApiVersion 2.0
    *@NScriptType ClientScript
    */
    
    function pageInit(context) {
        var currentRecord = context.currentRecord;
        
        var firstName = currentRecord.getValue({
            fieldId: 'firstname'
        });
        
        dialog.alert({
            title: 'Announcement',
            message: 'You are viewing ' + firstName
        });
    }
    
    return {
        pageInit: pageInit
    }
    
});

There are different type of logs you can use:

  • Audit
  • Debug
  • Emergency
  • Error

The one you choose when creating a log depends on what you are logging. For the purpose of this tutorial, we will use the debug log. The structure of the debug log looks like this:

log.debug({
    title: 'enter the title of the log', 
    details: 'Enter the log content: ' + logContent
});

There is a shortcut that I usually take when logging, and you can use this shortcut, but you may want to get used to the full suitescript structure of modules before using the shortcuts to save time and space in the code. The shortcut equivalent to the sample code above looks like this: log.debug('enter the title of the log','Enter the log

content: ' + logContent);

Adding a Log to the Script

In the script we have been creating throughout this tutorial, we are causing a popup box to appear that includes the customer’s first name. And we are getting the first name from a field on the customer’s record. Let’s say you are testing the script, and you don’t see the customer’s first name in the popup. This may not be the best example because the popup kind of acts like a log. But let’s create a log that gives us the value of whatever is in the first name field of the customer. After modifying the log sample from above and adding the “firstName” variable as the log content, it should look something like this:

log.debug({
    title: 'Customer First Name', 
    details: firstName
});

Adding that to the rest of the script will make it look like this:

define(['N/record','N/search','N/ui/dialog', 'N/log'], function (record, search, dialog, log) {
    /**
    *@NApiVersion 2.0
    *@NScriptType ClientScript
    */
    
    function pageInit(context) {
        var currentRecord = context.currentRecord;
        
        var firstName = currentRecord.getValue({
            fieldId: 'firstname'
        });
        log.debug({
            title: 'Customer First Name', 
            details: firstName
        });
        
        dialog.alert({
            title: 'Announcement',
            message: 'You are viewing ' + firstName
        });
    }
    
    return {
        pageInit: pageInit
    }
    
});

Notice that I added the log directly after the “firstName” variable was created in the script so that we show the value in the log directly after we get the value from the field. That’s all you need to do when adding a log to a script. Now you just need to update the script record in NetSuite with the latest script changes. Here are the steps to update the script:

  1. View the script record (don’t click on edit).
  2. Go to the “Scripts” tab
  3. Click “edit” next to the “Script File”
  4. Copy your new code and paste it in the window that pops up to replace the old code.
  5. Save

Viewing the Logs in NetSuite

Now that the script is updated, you can run a test so that you see the log that you created. Navigate to a customer record and edit it. If the code is working correctly, you should still see the popup box. Now that the script has run with the latest script updates, you can navigate back to the script record and then click on the “Execution Log” subtab. You should see a line that shows the log information you added in the script.

Conclusion

If this tutorial on SuiteScript logging and debugging was helpful, be sure to check out the next SuiteScript tutorial on setting record field values. And don’t forget to subscribe to our mailing list so you can receive our latest developing blogs directly in your inbox each week!