The nlapiTriggerWorkflow API can be used to trigger a workflow that is currently running against a record. The actions and transitions of the workflow will be evaluated for the record based on the current state that it is in.
If the workflow is in a state that requires the click of a button to trigger a transition, we can add the button ID as a parameters and this API will simulate the button click.
The steps below show how to create a workflow and use this API to trigger the transition:
1. Create a simple customer workflow with two states:
Name = Customer - nlapiTriggerWorkflow
ID = _cust_nlapitriggerwf
Record Type = Customer
Subtypes = Customer
Execute as Admin = Yes
Release Status = Testing
Enable Logging = Yes
On Create = Yes
Trigger Type = Before Record Load
- Add Button action:
Trigger On = Before Record Load
Script ID = _go_to_state_2
Label = Go to State 2
To = State 2
Button = Go to State 2
Edit State 2 and set Do Not Exit the Workflow = Yes
2. Create a new customer record to trigger the workflow.
3. After saving the customer record, go to the System Information tab > Active Workflows subtab.
4. Notice that the current state of the Customer - nlapiTriggerWorkflow workflow is State 1.
5. Open the script debugger and run the code below. Replace customer_id with the internal ID of the customer that was created before:
nlapiTriggerWorkflow('customer', customer_id , 'customworkflow_cust_nlapitriggerwf', 'workflowaction_go_to_state_2');
6. Go back to the customer record > System Information tab > Active Workflows subtab.
7. Notice that the transition was executed and the current state of the Customer - nlapiTriggerWorkflowworkflow is State 2.