N/action Module Script Samples

The following script samples demonstrate how to use the features of the N/action module:

Important:

The samples included in this section are intended to show how actions work in SuiteScript at a high-level. For specific samples, see Supported Record Actions.

Locate and Execute an Action on a Timebill Record

The following sample finds and executes an action on the timebill record without promises.

Note:

This sample script uses the require function so that you can copy it into the SuiteScript Debugger and test it. You must use the define function in an entry point script (the script you attach to a script record and deploy). For more information, see SuiteScript 2.x Script Basics and SuiteScript 2.x Script Types.

            /**
 * @NApiVersion 2.x
 */
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 

          

Find Actions Available for the Timebill Record Asynchronously Using Promise Methods

The following sample asynchronously finds actions available for a timebill record and then executes one with promises.

Note:

This sample script uses the require function so that you can copy it into the SuiteScript Debugger and test it. You must use the define function in an entry point script (the script you attach to a script record and deploy). For more information, see SuiteScript 2.x Script Basics and SuiteScript 2.x Script Types.

            /**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 */
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 

          

Execute a Bulk Action on a Timebill Record

The following sample shows how to execute a bulk approve action on a timebill record using different parameters.

Note:

This sample script uses the require function so that you can copy it into the SuiteScript Debugger and test it. You must use the define function in an entry point script (the script you attach to a script record and deploy). For more information, see SuiteScript 2.x Script Basics and SuiteScript 2.x Script Types.

Important:

The following code samples do not serve as an order of execution, getBulkStatus should be executed later on and not right after the execution of action.executeBulk().

            /**
 * @NApiVersion 2.x
 */
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 required 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 sample are similar and can be generated programatically using the map function.
var searchResults = /* result of a search, for example, [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
        };
    })
});

// 2a) Bulk execute the specified action on a provided list of record IDs.
// This time with homogenous parameters, that is, all parameter objects are equal except recordId.
var handle = action.executeBulk({
    recordType: "timebill",
    id: "approve",
    params: searchResults.map(function(v) {
        return {
            recordId: v,
            foo: "bar",
            name: "John Doe"
        };
    })
});

// 2b) Bulk execute the specified action on a provided list of record IDs.
// This time with homogenous parameters. Equivalent to the previous sample.
var commonParams = {
    foo: "bar",
    name: "John Doe"
};
var handle = action.executeBulk({
    recordType: "timebill",
    id: "approve",
    params: searchResults.map(function(v) {
        return util.extend({
            recordId: v
        }, commonParams);
    })
});

// 3) Bulk execute the specified action on a provided list of record IDs.
// This is the simplest usage with no extra parameters besides the record ID.
var handle = action.executeBulk({
    recordType: "timebill",
    id: "approve",
    params: searchResults.map(function(v) {
        return {
            recordId: v
        }
    })
});

// 4) Bulk execute the specified action on all record instances that qualify.
// Since we don't have a list of recordIds in hand, we only provide the callback
// that will later be used to transform a recordId to the corresponding parameters object.
var handle = action.executeBulk({
    recordType: "timebill",
    id: "approve",
    condition: action.ALL_QUALIFIED_INSTANCES,
    paramCallback: function(v) {
        return {
            recordId: v,
            note: "this is a note for " + v
        };
    }
});

// 5) Get a particular action for a particular record type.
var approveTimebill = action.get({
    recordType: "timebill",
    id: "approve"
});

// 6) Bulk execute the previously obtained action on a provided list of record IDs.
// Params are generated the same way as above in action.executeBulk().
var handle = approveTimebill.executeBulk({
    params: searchResults.map(function(v) {
        return {
            recordId: v,
            note: "this is a note for " + v
        };
    })
});

// 7) Bulk execute the previously obtained action on all record instances that qualify.
var handle = approveTimebill.executeBulk({
    condition: action.ALL_QUALIFIED_INSTANCES,
    paramCallback: function(v) {
        return {
            recordId: v,
            note: "this is a note for " + v
        };
    }
});

// 8) Get status of a bulk action execution.
var res = action.getBulkStatus({
    taskId: handle
}); // returns a RecordActionTaskStatus object
log.debug(res.status);
}); 

          

General Notices