One way of customizing Infoplus to fit your unique needs is to use Infoplus Scripts. You can write and run your own Scripts, which are user-defined blocks of JavaScript code, without leaving Infoplus. Scripts give you the power to use source code to customize user reports and build your business rules, however complex or unique they may be, directly in a familiar environment.


Scripts have many uses. For example, you could write a script to:

  • Make sure every new Warehouse Location you build is set to Online.
  • Split Orders between Warehouses if they contain specific SKUs.
  • Increase all of your Item footprints by a quarter of an inch.
  • Change the Billing Type on Warehouse Locations based on their Level.
  • Split each Order's serial number into its own row on a report.

 

Technical overview:

  • Each script is designed to work with a particular type of Record (e.g., Customer, Item).
  • Scripts can be run either as a result of a Trigger or manually.
  • Each time a Script runs, a log entry is created in the Scripts Table.
  • Scripts work with the Infoplus APITo take actions within Infoplus, Scripts have access to functions similar to those that are available in the Infoplus API. This means that Scripts have the ability to conduct searches and to look up individual records, plus the ability to add, update, and delete records within Infoplus, across all record types.

In this article, you will learn how to do the following:

  • Create a Script
  • Run a Script automatically by using a trigger
  • Run a Script manually
  • Review Script Logs

Create a Script

  1. In Infoplus, go to the Script table. Two ways:
    - From the DashboardClick the “Account Management” tab, click the “Scripts” App, then click the “Script” Table.
    - From the Quick Actions shortcut: Press a dot (period) on your keyboard, then select “Script” from the list of Quick Actions.

  2. Click the Create New button. The Creating New Script window appears.


  3. Enter a Name for the Script.
  4. Select the Script Type: Trigger or Report. This indicates whether you will use the script for an Action ran by a Trigger or to customize a User Report. If you select "Trigger", two additional fields will display: Record Type and API Version.


    If you selected the Trigger Report Type:
    - Select the Record Type on which the Script will operate. You may choose any Record Type available in the Infoplus API.
    - Select the API Version with which the Script will be compatible. Each Script you create works with a specific version of the Infoplus API. By choosing a fixed version of the Infoplus API, you can be confident that the fields and methods your Script calls will always be available, so a Script you write today will continue to work in the future as new versions of the Infoplus API become available.
    NOTE:  It is recommended that the latest release version always be used for new production-ready Scripts. For testing new functionality, the Beta version may used, but it is not recommended to use the Beta version for production, as the interfaces on the Beta API may change regularly.

    Add Code

  5. Enter the Code that describes the operation of the Script. This code is written in JavaScript. Here is a sample Script that will set new Warehouse Locations to be Online:
    if(location.online)
    {
        ////////////////////////////////////////////////////////////
        // if the location is already online, exit with no action //
        ////////////////////////////////////////////////////////////
        return;
    }
    location.online = true;
    utils.log("Updating location [" + location.address + "] to be online.");
    infoplusApi.update("location", location);
     


     NOTE: It is possible to exit a script in the middle by using a return; statement (regular exit) or a throw statement (exit with error). Details:
    - When a Scripts runs, it is wrapped inside of an immediately invoked function expression (IIFE). This implies that if a Script needs to exit prematurely (for example, if it evaluates a field on the record that it receives as input and determines that no further action is needed), it can do so by executing a 
    return; statement. Infoplus will discard any value returned from a Script.  The Script Log for a Script which exits in this way will show "Had Error = false". 

    - If you want a Script to exit with an Error state, this can be done by using a throw statement. The value thrown will be recorded in the Script Log's Error field. If a Script is running as a Trigger, and it exits with an Error, the Trigger will issue an Alert. 


    NOTE: The following variables are available in runtime context of an Infoplus Script as global variables:  

    • recordModel (The actual variable name depends on the Script's Record Type)
      • The record which the script receives as input will be available in the Script's context, and it will named after the record type that the Script works with, where the naming convention will match that used for model types in the Infoplus API.  For example, for a Script that runs against Orders, this variable will be named order.  For Item Receipts, it will be named itemReceipt. The general rule for that naming convention is initial-lower-case, camel-back, singular.
      • This object is the API Model representation of the record that the Script is being executed against (whether that's the record that a Trigger's Smart Filter matched, or the record that you selected when manually running a Script). The properties of this object can be found in the Infoplus API reference.
    • infoplusApi
      • This object provides full access to the Infoplus API, to allow a Script to get individual records (by id), search for records, and to add, update, or delete records. The methods in this object take, as their first parameter, the name of the record type that you want to work with. For example, "order", "orderLine", "itemReceipt", etc. 
      • The full list of methods available on this object are:
        • List<recordModel> search (recordType, filter, page, limit, sort)
          • Looks up a list of records from Infoplus that match the specified filter.  
            • recordType must be a string matching one of the resource types listed on the Infoplus API reference, such as "order" or "item"
            • filter must be a string such as "sku eq 'ABCD'"
            • page may be null (in which case it defaults to 1), or an integer starting at 1
            • limit may be null (in which case it defaults to 20), or an integer between 1 and 250.
            • sort may be null or a field name from the resource, optionally prefixed by a '!' character to reverse the sort. 
            • See the Infoplus API reference for further details on the filter, page, limit, and sort parameters.  
            • Example:  var itemList = infoplusAPI.search("item", "sku like 'A%' and status eq 'Active'", null, 100, 'sku');
        • recordModel getById (recordType, id)
          • Looks up a single record from Infoplus, by its primary key / id. 
          • Example:  var order = infoplusAPI.getById("order", 1701);
        • recordModel constructModel (recordType)
          • This method is used to construct a new apiModel object of the specified type, which can be passed in to the add method to insert a new record into Infoplus. 
          • Example:  var newOrder = infoplusAPI.constructModel("order");
        • recordModel duplicate (recordType, recordModel)
          • This method takes a recordModel and returns a copy of it, with the values of certain fields set to null (such as non-editable or system-generated ones). The object that is returned can be readily passed into the add method (after adjusting any values that are needed) to insert a new record as a copy of the input recordModel.  
          • Example: var duplicateQuickReceipt = infoplusAPI.duplicate("quickReceipt", originalQuickReceipt);
        • recordModel add (recordType, recordModel)
          • Saves a new recordModel into Infoplus.
          • Example: var addedThirdPartyParcelAccount = infoplusAPI.add("thirdPartyParcelAccount", newThirdPartyParcelAccount);
        • recordModel update (recordType, recordModel)
          • Saves updates to an existing recordModel into Infoplus. The existing record is identified by the primary key field within the recordModel. For example, orderNo for Orders, id for most other recordTypes.  
          • Example: var updatedOrderSource = infoplusAPI.update("orderSource", orderSource);
        • recordModel updateCustomFields (recordType, recordModel)
          • Saves updates to the customFields in a recordModel into Infoplus. 
          • Example: var updatedVendor = infoplusAPI.update("vendor", vendor);
        • void delete (recordType, id)
          • Deletes the specified record from Infoplus.  
          • Example: infoplusAPI.update("location", location.id);
    • utils
      • This object provides the following utility methods to Scripts:
        • void log (message) generates a log message that can be seen in the Output field of the Script Log that is created when the Script executes. Note that any particular run or execution of a script is limited to 100 KB of logs. If a script attempts to log more data than this, it will generate a runtime error.
        • Example: utils.log("Updated order [" + order.orderNo +"] priority code to [" + order.priorityCode + "]");


Run a Script Automatically by Using a Trigger

A Script can be run as a Trigger by adding an action of "Run Script" to a Trigger. See Triggers for more information.

  1.  

    When creating the action for the Trigger, you will first click on the number that appears in the Actions column for the trigger.

     

     


  2. From the drop-down menu, select Create New. The Create Action window appears.


     
  3. For the Action Type, choose "Run Script".

  4. Choose the Script to be associated with the Trigger.

  5. Click Save.


 

Run a Script Manually from the Script Table

A Script can be run manually from the Script table, as well as from the Run Script process as described later. This can be used for testing a new Script, or to run a Script against a particular Record.

  1. In Infoplus, go to the Script table. Two ways:
    - From the DashboardClick the “Account Management” tab, click the “Scripts” App, then click the “Script” Table.
    - From the Quick Actions shortcut: Press a dot (period) on your keyboard, then select “Script” from the list of Quick Actions.

  2. Click the checkbox next to the Script you want to run. Also take note of the Record type needed for the Script.

  3. From the Actions menu, select Run Script.

     

     
  4. The Setup screen appears.

    05 - Choose Record.png

  5. Enter the Record Id of the Record on which the Script will operate. Be sure that the Record Type matches what was specified when the Script was created. The Record Type is visible within the Script table, as shown above.

  6. Click Next. The Review screen appears.

     

     
  7. Review the planned results. If acceptable, click Submit to run the script. The Results screen appears.

     

     
     
  8. Click OK to continue.

Run a Script Manually using the Run Script Process

A Script can be run manually with the Run Script process, as well as from the Script table as described above. This can be used for testing a new Script, or to run a Script against a particular Record.

  1. In Infoplus, access the Run Script process. Two ways:
    - From the DashboardClick the “Account Management” tab, click the “Scripts” App, then click the “Run Script” Process.
    - From the Quick Actions shortcut: Press a dot (period) on your keyboard, then select “Run Script” from the list of Quick Actions.

     
  2. Click the checkbox next to the Script you want to run. Also take note of the Record type needed for the Script.

     


  3. Click Next. The Setup screen appears.

    05 - Choose Record.png

  4. Enter the Record Id of the Record on which the Script will operate. Be sure that the Record Type matches what was specified when the Script was created. The Record Type is visible within the Run Script listing, as shown above.

  5. Click Next. The Review screen appears.

    06 - Script Run Review.png

  6. Review the planned results. If acceptable, click Submit to run the script. The Results screen appears.

    07 - Script Run Results.png

     
  7. Click OK to continue. 


 

Review Script Logs

Every time a Script runs, it generates a record in the Script Log table of Infoplus. This record contains the following fields:

  • Script - a reference to the Script that generated the Script Log.
  • Run Mode - an indication of how the Script was executed (either as a Trigger or a Manual Run).
  • Record Id - the id of the Record which was passed into the Script.  
  • Had Error - an indication of whether or not the Script exited with an error or not.
  • Output - all logging output produced by the Script (via the utils.log method).
  • Errors - any errors encountered while executing the Script (including syntax errors, undefined references, Infoplus runtime errors, or exceptions thrown within the Script's source code).
     
  • Run Time (ms) - the total runtime of the script, in milliseconds. Note that Infoplus may terminate a Script if it runs for longer than 60 seconds.  

Follow these steps to see the Log for a particular Script:
 
  1. In Infoplus, go to the Script Log table. Two ways:
    - From the DashboardClick the “Account Management” tab, click the “Scripts” App, then click the “Script Log” Table.
    - From the Quick Actions shortcut: Press a dot (period) on your keyboard, then select “Script Log” from the list of Quick Actions.

  2. Click on the Log record that you want to view. 

 

Related Information: