Use a Group of Approvers in a Workflow Utilizing a Workflow Action Script

Written by
Manuelito Macalinao
Published on
August 31, 2023 at 9:13:15 AM PDT August 31, 2023 at 9:13:15 AM PDTst, August 31, 2023 at 9:13:15 AM PDT

Scenario

User needs toincorporatea certain group ofApprovers,SupervisorsorEmployeesto aWorkflowto handle the approval process ofa record.

Solution

1. Create aWorkflowactionscriptthat would check if the current user is a member of the Group of Approvers by navigating toCustomization>Scripting>Scripts>New. The logic will make use of an Entity Group search checking, if the user's internal ID belongs to the group. You also need to define aReturn Type(Free-Form Text) on the createdWorkflowaction script. Use the script below:

functioncheckUserIfMember(){var user =nlapiGetUser(); filters =newArray(), columns =newArray(), searchresults =newArray(); filters[0]=newnlobjSearchFilter('internalid',null,'is',14703);//internal id of the static Group filters[1]=newnlobjSearchFilter('internalid','groupmember','is', user); columns[0]=newnlobjSearchColumn('internalid','groupmember');// Member Idtry{// Check for user searchresults =nlapiSearchRecord('entitygroup',null, filters, columns);// Return true if we have resultsif(searchresults){return'T';}}catch(e){nlapiLogExecution('DEBUG', e.name, e.message);return'F';}return'F';}

2. Create aStatewhere you want to initiate the customAction Script. Preferrably the initialStateon theWorkflow.
3. On the createdState, add a stateFieldthat would hold the returned value of the customScriptin Step 2. The returned value will be used as condition on the subsequent actions and conditions on theWorkflow.

Note:

Alternatively, the same logic can also be implemented using SuiteScript 2.0.

/** * @NApiVersion 2.x * @NScriptType workflowactionscript */define(['N/runtime','N/search'],function(runtime, search){functiononAction(scriptContext){var userObj = runtime.getCurrentUser();var user = userObj.id;var entitygroupSearchObj = search.create({ type:"entitygroup", filters:[["internalid","anyof","14703"],//internal id of the static Group"AND",["groupmember.internalid","anyof", user]], columns:[ search.createColumn({ name:"groupname", sort: search.Sort.ASC, label:"Name"})]});try{// Check for uservar searchresults = entitygroupSearchObj.runPaged().count;// Return true if we have resultsif(searchresults){return'T';}}catch(e){ log.debug(e.name, e.message);return'F';}return'F';}return{ onAction : onAction };});