Here are some tips for how to troubleshoot (AKA debug) Scripts that you write in Infoplus.


Script Log table

If you have an Infoplus Script that is failing, you can get information about the errors in the Script Log table in Infoplus.  Searching for Had Error = True will find any Scripts which ended due to a runtime exception.  Such log records will have an error message in the Error column, and these messages will include the line of code within the script that had the error:



Custom Logging

To further understand what a script is doing, one of the most practical options is to add logging statements to the script, which you can then review in the Output field of the Script Log table.  You can call the method:

utils.log("Your log message here");

from anywhere within your script, and your message will appear, after your script runs, in the Output field of the Script Log table. 


Some of the most common use cases for logging like this are:

  • Understanding the flow of a script.  For example, did a particular if statement run or not?  How many times did a for loop execute?
  • Seeing the values of variables.  For example:  utils.log("Order carrier is: " + order.carrierId);


Lists vs. Arrays

Infoplus scripts are written in JavaScript, however, they are executed in a Java runtime.  This means that the variables you use in a Script will sometimes be JavaScript style objects, but other times, will be Java style objects.  Depending on which style of object you have, there are differences to the methods/properties available to you, and if you use the wrong ones, you'll get runtime errors.  One common source of confusion in this area has to do with List or Array variables. 


If you create your own array variable in an Infoplus Script, for example, as var myArray = []; it will be a JavaScript style array, which you can interact with by accessing the .length property, using square-braces to access elements (i.e., myArray[0]), and methods such as .push() or .splice().  See https://www.w3schools.com/js/js_arrays.asp and https://www.w3schools.com/js/js_array_methods.asp for information on working with JavaScript arrays.


However, if you are working with a list returned by an infoplusApi call within a script, or working with a variable provided to the script in context (for example, reportRows in a Report type script), then you will have a Java style List, and you must work with it using methods such as .size() and .add(), and you access elements using the .get() method.  See https://docs.oracle.com/javase/8/docs/api/java/util/List.html for the Java list API reference.


Iterating over a JavaScript style array:
for(var i=0; i<array.length; i++)
{
   var element = list[i];
}
Iterating over a Java style list:
for(var i=0; i<list.size(); i++)
{
   var element = list.get(i);
}


Custom Fields

If you have custom fields on your tables in Infoplus, you can access those in a script, through an object named customFields under the record object.  Custom Fields in scripts are only available in API versions greater than 2.0 (including beta).   


Note that this is object is a Java Map style object, so you must call the get or put methods on this object to access custom field values.  For example:

// log the current color of an Item:
utils.log("This is a " + item.customFields.get("color") + " item.");

// make the item's custom "size" field be "Large":
item.customFields.put("size", "Large");


To help figure out the names of your custom fields, you might try a log line like this:

utils.log("item custom fields: " + record.customFields);


Which will produce output in a Script Log like this:

item custom fields: {color="Red", size=null, rank=3}