This article is relevant if you are producing NetSuite SuiteLet development and you seek better tools to debug your application.
Background
Before I get started, I must say how delighted I get to work with a senior team of professionals. Sean, both a great friend and strong software architect and business analyst, produced the following code elements which were inspiration for me during one of my SuiteScript development efforts. I enhanced his code to bring this to you.
When developing NetSuite Suitelet applications, many times you need to inspect your environment to understand the data and information provided by users input. Sometimes, you need to see http headers. Most of the time, you need see the value of some variables that have been submitted by the user. The challenge is that the common practice to debug NetSuite SuiteScript applications is to periodically litter your software program with statements such as nlapiLogExecution(‘DEBUG’, ‘Value of variable x:’, x); this can be time consuming and cause your software program to bloat.
Wouldn’t it be better if you could simply call a custom function that simply outputs the entire environment to your browser so it is easy to inspect? The following function and code fragment does just that:
Example NetSuite Suitelet Program to Debug Application
The following code segment is broken into three sections:
- A variable to conveniently place at the top of your SuiteScript program
- Illustration of the hook point in your main SuiteLet program
- The custom function that will do the work to output all the known Suitelet inputs
Here are the program elements:
//place this constant at the top of your program //change the following to true to output the environment var CONST_DEBUG_OUTPUT = true; //default is false during normal run-time operations //the following function represents your SuiteLet program main entry point function myCustomSuiteLet(request, response) { //we plug our function statement at the beginning of the program before any other HTML output if (CONST_DEBUG_OUTPUT) { //pass the request and response objects see_all_input(request, response) return; }; //the rest of the script is your main business program //... }; //the following function does the work to output to the environment in a clean fashion function see_all_input(request, response) { // first collect header information var form_debug = nlapiCreateForm('DEBUG'); var html_debug = '<hr />getAllHeaders()<br />'; if (request.getAllHeaders().length > 0){ var prms = request.getAllHeaders(); for (var prm in prms){ html_debug += prm + ':' + prms[prm] + '<br />'; }; }; //then collect the input data html_debug += '<hr />getAllParameters()<br />'; if (request.getAllParameters().length > 0){ var prms = request.getAllParameters(); for (var prm in prms){ html_debug += prm + ':' + prms[prm] + '<br />'; }; }; //now output to the browser with ( form_debug.addField('custpage_debug', 'inlinehtml', '' , null, null) ) { setDefaultValue(html_debug); }; response.writePage( form_debug ); return; };
Enhance Your NetSuite Application
One of the major reasons we love working with NetSuite is the power of the customization platform. Always looking to improve productivity, this tip should help quickly inspect and debug your SuiteScript program. If you would like help developing your own custom applications, contact us.
This is pretty useful, and could easily be modified for suitelets that aren’t meant to generate HTML. I’d like to note, however that with is discouraged from use. You could get the same result with
form_debug.addField('custpage_debug', 'inlinehtml', '' , null, null).setDefaultValue(html_debug);
Thank you Aaron for the thinking. Let’s make this better!
Marty
Sir, I am following your blog…its helpful for my career, sir my client ask the follow thing, Please reply for this.
I am showing alerts for the validations on address sub record, for this i deployed script to all records. on client save i am showing alert, when cookies are there it is not showing, how can i prevent this problem.
How to show alerts with client script without the problem of cookies.