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.
-
In NetSuite, go to Customization > Lists, Records, & Fields > Transaction Body Fields > New
-
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
-
-
To edit the basic script in NetSuite, go to Documents > Files > SuiteScripts.
-
Find Example Scriptable Cart in the list and click Edit.
-
On the File page, find the Media Item field and click Edit.
-
Add the following function below the existing
customPageInit
function in the script.function customSaveRecord () { nlapiSetFieldValue('custbody_used_scriptable_cart', 'T'); return true }
-
Click Save.
-
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.
-
In the Sever Record Function field, enter
customSaveRecord
. -
Click Save.
-
Test the updated script as follows:
-
Go to your Commerce website, and place an order.
-
In NetSuite, look up that order.
-
Go to the Custom tab for the order and verify that the custom field contains the value defined by the script.
-