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:
- View the script record (don’t click on edit).
- Go to the “Scripts” tab
- Click “edit” next to the “Script File”
- Copy your new code and paste it in the window that pops up to replace the old code.
- 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!
I have found that script log entries disappear once I have logged off. Is this normal?
In your tutorial “SuiteScript Logging and Debugging”. Added log.debug () or other logs to the client script. How do you see log without deploy client script log in some scenarios. My understanding you can only see such log in script running on server side. Did I misunderstand this?
Need more tutorial videos like. please upload more.!
I have applied script on a custom record but name of employee is not displaying rather than id is displayed i need to display the name of employee, by the way very helpful topic and YouTube channel.
Please upload more it will help us to understand NetSuite easily.