Using comments and the console to debug scripts
Summarize
Summary of Using comments and the console to debug scripts
This guide explains how ServiceNow CPQ administrators can effectively use comments and the console to debug scripts within CPQ areas such as advanced conditions for rules, advanced actions for rules, and enrichments. The scripting environment supports a JavaScript-like language, enabling admins to prepare and test their code before deploying it in blueprints.
Show less
Key Features
- Console.log for Debugging: The CPQ Admin interface includes a debugger and console output area where admins can run scripts and see real-time outputs. Using
console.log, admins can print text or variable values to this console, facilitating step-by-step validation of script behavior. - Comments in Scripts: Comments are ignored by the script engine but serve important roles in documentation, temporarily disabling code, or preserving code snippets for future use.
- Single-line comments: Use
//to comment out the remainder of a line. - Multi-line comments: Use
/ ... /to comment across multiple lines. Care must be taken not to comment out critical syntax elements such as brackets or return statements.
- Single-line comments: Use
- Using Comments to Store Inputs: For complex rules relying on many inputs, admins can paste JSON input data as multiline comments in the debugger. This practice helps preserve input data for easier modifications and testing later.
Practical Benefits for ServiceNow Customers
- Enables efficient troubleshooting and validation of CPQ scripts by visualizing outputs in the console.
- Improves maintainability and clarity of scripts through well-documented comments.
- Facilitates iterative development by allowing admins to disable chunks of code without deletion.
- Simplifies reusing and testing complex input cases by embedding them as comments within the debugger.
Learn how comments and the console can help you debug your scripts.
CPQ has several areas where the admin can use scripts to define behavior. These include advanced conditions for rules, advanced actions for rules, and enrichments.
Advanced conditions for rules
Advanced actions for rules
Enrichments
This article highlights a few key features to help you test and prepare your code before you deploy it in a blueprint.
Console.log
When the admin starts to write a script, the CPQ Admin looks like this:
Clicking Run Debugger in the lower panel raises the debugger and the Debugger Output section. This section is also called the console.
This box shows the output of the script based on the script and the inputs added to the debugger (if applicable). For example, the BOM enrichment script shows the updated BOM based on the enrichment and inputs put in the debugger.
Lines of code can be logged to the console. So, you can send text to the console, as in the following:
You can also log variables, which is helpful to make sure your script is working correctly. You can add text to the log to help the lines of code stand out:
Comments
Comments are lines of code that the script ignores. Comments have a few uses. First, it is very helpful to future coders (and to you, when you revisit the code much later) if you have commented on how and why you coded lines of the script as you did. Commenting can also be used to save code to use again. And if you do not use a block of code but may want to use it later, you can comment it so it does not affect your current work.
You can write comments on a single line or across multiple lines.
To add single-line comments, use two slashes. Anything written after the slashes on the same line is ignored by the script.
// This comment is ignored by the script
However, any code before the slashes is still executed. For example, in the image below, the variable be4comment remains 12345, as the script ignores the comment "67890" following the slashes.
To create a multiline comment, add a slash and an asterisk before the comment. Add an asterisk and a slash after the comment.
/*
Your comment goes here.
It can span multiple lines.
*/
When you add a multiline comment, be careful not to comment out important elements such as closing brackets, parentheses, or return statements.
When you frequently revisit or modify a rule that takes inputs from many other fields, it can be helpful to paste the input into the debugger section as a multiline comment. This way, when you return to work on the rule later, you won't need to rewrite the inputs.
/* inputs
{"Field1": "testValue1",
"Field2Quantity": 2,
"Field3": "testValue3"
},
*/