saveRecord and Updating a Custom Transaction Body Field

Scriptable Cart is often used to update some part of a sales order, such as a body field or column, or some other custom record. This type of update typically occurs after an order has been successfully placed, so the best function for this use case is saveRecord.

Like the validateLine function, the saveRecord function can be used as a blocker. This means a user can complete all the steps to submit a sales order and then you can run business logic to check whether the order should go through. In this use case, the business logic validates the order.

The following steps describe how to write a custom saveRecord function that adds a custom transaction body field to an order that a customer places.

  1. In NetSuite, go to Customization > Lists, Records, & Fields > Transaction Body Fields > New

  2. Set the following options:

    • Label: Used Scriptable Cart?

    • ID: _used_scriptable_cart

    • Type: Free-Form Text

    • Applies To: Check the Sale box

    • Leave all other fields set to default values

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

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

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

  6. Add the following function below the existing customPageInit function in the script.

                  function customSaveRecord ()
    {
      nlapiSetFieldValue('custbody_used_scriptable_cart', 'T');
      return true
    } 
    
                
  7. Click Save.

  8. Make sure this customSaveRecord function is associated with the NetSuite Save Record function.

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

  9. In the Sever Record Function field, enter customSaveRecord.

  10. Click Save.

  11. Test the updated script as follows:

    1. Go to your Commerce website, and place an order.

    2. In NetSuite, look up that order.

    3. Go to the Custom tab for the order and verify that the custom field contains the value defined by the script.

Related Topics

Basic Custom Functions
validateLine Function
pageInit Function and Checking Context

General Notices