validateLine Function

A validateLine event occurs when a line in a transaction changes. This change can occur when a shopper adds an item, changes an item's quantity, or removes an item.

In all cases, validateLine functions run before a change is made, making them the perfect mechanism for a wide range of preventative actions. For example, you can write a function to prevent shoppers from adding a particular item to their cart or to prevent shoppers from increasing the quantity of a particular item over a certain amount.

Because functions like these are checking certain conditions, they must return a value of true or false. (Returning no value is interpreted as false). Returning true allows a change to happen, while a value of false blocks the change.

The following steps describe how to add a validateLine function to the basic script, Example Scriptable Cart, that you previously created by following the instructions in Start Using Scriptable Cart with SuiteCommerce or SCA.

  1. To edit the basic script in NetSuite, go to Documents > Files > SuiteScripts.

  2. Find Example Scriptable Cart in the list and click Edit.

  3. On the File page, find the Media Item field and click Edit.

  4. Add the following function below the existing customRecalc function in the script.

                  function customValidateLine (type)
    {
      if (type = 'item')
      {
        var itemId = nlapiGetCurrentLineItemValue('item', 'item');
        debug('Line validated: ' + itemId);
      }
    
      return true
    } 
    
                
  5. Click Save.

  6. Make sure this customValidateLine function is associated with the NetSuite Validate Line function.

    Go to Customization > Scripting > Scripts, click the Edit option for Example Scriptable Cart.

  7. In the Validate Line Function field, enter customValidateLine.

  8. Click Save.

  9. Test the updated script as follows:

    1. Go to your Commerce website and modify the contents of the shopping cart by adding an item to the cart or changing the quantity of an item already in the cart.

    2. Go to Customization > Scripting > Scripts to check the script execution log for the messages generated by your script.

    3. In the Execution Log tab, check for messages like these from the new function you added to the script: Line Validated:<Item_ID>.

Note:

The parameters that this new function accepts are not the same as the parameters you see in the script record. The parameters in the script record can more accurately be described as custom fields that you can pass to the function. You typically use the script record parameters to make part of the script configurable. For example, if you want to pass the script an external value or if there’s a value the user needs to input that affects how the code should behave.

Related Topics

Basic Custom Functions
pageInit Function and Checking Context
saveRecord and Updating a Custom Transaction Body Field

General Notices