SUITESCRIPT 2.0 The content in this help topic pertains to Suite Script 2.0.
The N/action module APIs let you execute a business logic to update the state of records in view mode. To execute business logic on records that you are editing, use the record macro APIs, which are included in the N/record Module module. Action and Macro APIs are the programmatic equivalents to clicking a button in the UI.
The changes that you make to records with N/action module APIs are persisted in the database immediately. For example, consider the time bill record. After you click the Approve button in the UI, the time bill and its entries are saved in an approved state, and this change is immediately updated in the database.
Governance for action module APIs varies for actions and record types. See the action help for governance information specific to actions and record types.
A limited number of individual actions for specific record types are supported.
N/action Module Members
| Member Type | Name | Return Type / Value Type | Supported Script Types | Description |
|---|---|---|---|---|
| Object | action.Action | Object | Client and server-side scripts | Encapsulates a NetSuite record action. |
| Plain JavaScript Object | Object | Client and server-side scripts | A plain JavaScript object of actions available for a record type. | |
| Method | action.execute(options) | Object | Client and server-side scripts | Executes the record action and returns action results in an object. |
| action.execute.promise(options) | Promise | Client scripts | Asynchronously executes the record action and returns the action results in an object. | |
| action.executeBulk(options) (Beta)(Beta) | string | Client and server-side scripts | Executes an asynchronous bulk record action and returns its task ID for later status inquiry. | |
| action.find(options) | Object | Client and server-side scripts | Returns a plain JavaScript object of available record actions for the given record type. | |
| action.find.promise(options) | Promise | Client scripts | Asynchronously returns a plain JavaScript object of available record actions for the given record type. | |
| action.get(options) | action.Action | Client and server-side scripts | Returns an executable record action for the given record type. | |
| action.get.promise(options) | Promise | Client scripts | Asynchronously returns an executable record action for the given record type. |
Action Object Members
The following members are called on action.Action.
| Member Type | Name | Return Type/Value Type | Supported Script Types | Description |
|---|---|---|---|---|
| Method | Action(options) | Object | Client and server-side scripts | Executes the action and returns the action results in an object. |
| Action.promise(options) | Promise | Client scripts | Executes the action asynchronously and returns the action results in an object. | |
| Action.execute(options) | Object | Client and server-side scripts | Executes the action and returns the action results in an object. | |
| Action.execute.promise(options) | Promise | Client scripts | Executes the action asynchronously and returns the action results in an object. | |
| Action.executeBulk(options) (Beta)(Beta) | string | Client and server-side scripts | Executes an asynchronous bulk record action and returns its task ID for later status inquiry. | |
| action.getBulkStatus(options) (Beta)(Beta) | Object | Client and server-side scripts | Returns the current status of action.executeBulk(options) (Beta)with the given task ID. | |
| Property | Action.description | string | Client and server-side scripts | The action description. |
| Action.id | string | Client and server-side scripts | The ID of the action. | |
| Action.label | string | Client and server-side scripts | The action label. | |
| Action.parameters | Object | Client and server-side scripts | The action parameters. | |
| Action.recordType | string | Client and server-side scripts | The type of the record on which the action is to be performed. |
N/action Module Script Samples
These samples use the require function, so that you can copy each script into the debugger and test it. Keep in mind that you must use the define function in your entry point script (the script you attach to a script record).
Important
The samples included in this section are intended to show how actions work in SuiteScript at a high-level.
The following server script sample finds and executes an action on the time bill record without promises.
require(['N/action', 'N/record'], function(action, record) {
// create timebill record
var rec = record.create({type: 'timebill', isDynamic: true});
rec.setValue({fieldId: 'employee', value: 104});
rec.setValue({fieldId: 'location', value: 312});
rec.setValue({fieldId: 'hours', value: 5});
var recordId = rec.save();
var actions = action.find({
recordType: 'timebill',
recordId: recordId
});
log.debug("We've got the following actions: " + Object.keys(actions));
if (actions.approve) {
var result = actions.approve();
log.debug("Timebill has been successfully approved");
} else {
log.debug("The timebill is already approved");
}
});
// Outputs the following:
// We've got the following actions: approve, reject
// Timebill has been successfully approved
The following client-side script sample asynchronously finds actions available for a time bill record and then executes one with promises.
require(['N/action', 'N/record'], function(action, record) {
// create timebill record
var rec = record.create({type: 'timebill', isDynamic: true});
rec.setValue({fieldId: 'employee', value: 104});
rec.setValue({fieldId: 'location', value: 312});
rec.setValue({fieldId: 'hours', value: 5});
var recordId = rec.save();
// find all qualified actions and then execute approve if available
action.find.promise({
recordType: 'timebill',
recordId: recordId
}).then(function(actions) {
console.log("We've got the following actions: " + Object.keys(actions));
if (actions.approve) {
actions.approve.promise().then(function(result) {
console.log("Timebill has been successfully approved");
});
} else {
console.log("The timebill is already approved");
}
});
});
// Outputs the following:
// We've got the following actions:
// The timebill has been successfully approved
The following sample uses action.executeBulk(options).
require(['N/action', 'N/util'] function(action, util) {
// 1a) Bulk execute the specified action on a provided list of record IDs.
// The params property is an array of parameter objects where each object contains mandatory recordId and arbitrary additional parameters.
var handle = action.executeBulk({
recordType: "timebill",
id: "approve",
params: [{ recordId: 1, note: "this is a note for 1" },
{ recordId: 5, note: "this is a note for 5" },
{ recordId: 23, note: "this is a note for 23" }]
})
});
// 1b) Bulk execute the specified action on a provided list of record IDs.
// The parameters in the previous example are very similar and can be generated programatically using the map function.
var searchResults = /* result of a search, e.g. [1, 5, 23] */;
var handle = action.executeBulk({
recordType: "timebill",
id: "approve",
params: searchResults.map(function(v) {
return { recordId: v, note: "this is a note for " + v };
})
});