N/action Module

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 TypeNameReturn Type / Value TypeSupported Script TypesDescription
Objectaction.ActionObjectClient and server-side scriptsEncapsulates a NetSuite record action.
Plain JavaScript ObjectObjectClient and server-side scriptsA plain JavaScript object of actions available for a record type.
Methodaction.execute(options)ObjectClient and server-side scriptsExecutes the record action and returns action results in an object.
action.execute.promise(options)PromiseClient scriptsAsynchronously executes the record action and returns the action results in an object.
action.executeBulk(options) (Beta)(Beta)stringClient and server-side scriptsExecutes an asynchronous bulk record action and returns its task ID for later status inquiry.
action.find(options)ObjectClient and server-side scriptsReturns a plain JavaScript object of available record actions for the given record type.
action.find.promise(options)PromiseClient scriptsAsynchronously returns a plain JavaScript object of available record actions for the given record type.
action.get(options)action.ActionClient and server-side scriptsReturns an executable record action for the given record type.
action.get.promise(options)PromiseClient scriptsAsynchronously returns an executable record action for the given record type.

Action Object Members

The following members are called on action.Action.

Member TypeNameReturn Type/Value TypeSupported Script TypesDescription
MethodAction(options)ObjectClient and server-side scriptsExecutes the action and returns the action results in an object.
Action.promise(options)PromiseClient scriptsExecutes the action asynchronously and returns the action results in an object.
Action.execute(options)ObjectClient and server-side scriptsExecutes the action and returns the action results in an object.
Action.execute.promise(options)PromiseClient scriptsExecutes the action asynchronously and returns the action results in an object.
Action.executeBulk(options) (Beta)(Beta)stringClient and server-side scriptsExecutes an asynchronous bulk record action and returns its task ID for later status inquiry.
action.getBulkStatus(options) (Beta)(Beta)ObjectClient and server-side scriptsReturns the current status of action.executeBulk(options) (Beta)with the given task ID.
PropertyAction.descriptionstringClient and server-side scriptsThe action description.
Action.idstringClient and server-side scriptsThe ID of the action.
Action.labelstringClient and server-side scriptsThe action label.
Action.parametersObjectClient and server-side scriptsThe action parameters.
Action.recordTypestringClient and server-side scriptsThe 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 };
        })
    });

Leave a comment

Your email address will not be published. Required fields are marked *