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