Sunday, April 21, 2019

Trigger Client Scripts before Workflow Actions fire

When buttons created from a Workflow (Add Button action) is clicked, the next Workflow Action is immediately triggered.  However, there are instances where we require some client-side validations before allowing them to fire.

To workaround this, both script and Workflow functionalities are required.  Instead of creating the button from Workflow alone, use Before Load (User Event) script to create it, do the client-side validation and then call the Workflow State from a Suitelet.

Custom Accounting Approval for Sales Order is used on this article for demonstration purposes.

1. Design the Workflow

      1.a) Navigate to Customization > Scripting > Scripts>  New> Workflow Action
      1.b) Define the settings on the Workflow Summary as illustrated below

              [Screenshot Missing]

      1.c) Add a State named "Add Accounting Approval Button" 

      1.d) Create an "Add Button" action as follows:

              [Screenshot Missing]

 

   Note: Add Button action is required because it creates a 'Script ID', which will be the SuiteScript's reference to call the Workflow

               1.d.1) Set Formula Condition to FALSE as it does not need to be seen by users.

               1.d.2) Make sure that "Check condition before execution" is unchecked.  Otherwise, it will not move onto the next state.

               1.d.3) Type '_approve_sales_order' in Script ID box.  Note that this will be prefixed with 'workflowaction' after saving.

 

      1.e) Add the second State, named "Approve Sales Order".  Then create new Set Field Value actions to indicate that the transaction is already approved.

         [Screenshot Missing]

 

      1.f) Transition State 1 to State 2 on button click

         [Screenshot Missing]


 

2. Write the Suitelet script that will invoke the Workflow

       function mySuitelet(request, response) {
          var wfInstance = nlapiTriggerWorkflow('salesorder', request.getParameter('soid'), 143, 'workflowaction_approve_sales_order');
          nlapiSetRedirectURL('RECORD', 'salesorder', request.getParameter('soid'));
       }

       Suitelet's Script ID (Text) must be 'customscript_suitelet_so_approval_wf' and deployment ID must be 'customsdeploy_suitelet_so_approval_wf' as it will be the Client Scripts reference on the next step (Step #3).

       Note that Script IDs are automatically prefixed with 'customscript' and deployment IDs with 'customdeploy'

 

3. Create a Client Script that will do the validation.

       function myClientValidation() {
          // For instance, Accountant Role Internal ID is 1000
          if ( nlapiGetRole() != 1000 ) { // Check if current user is not having role 1000, then return an error.
             alert('You are not allowed to approve this transaction.');

          } else { // Otherwise call Suitelet that will trigger the Workflow

             if ( confirm('This will mark the Sales Order as Accounting Approved.  Do you want to proceed?') ) {
                window.location = nlapiResolveURL('SUITELET', 'customscript_suitelet_so_approval_wf', 'customdeploy_suitelet_so_approval_wf') + '&soid=' + nlapiGetRecordId();;
             }
          }
       }

       Note: Client scripts cannot directly call a Workflow.  Thus, a server-side script (on this case, Suitelet) is needed to make the call on behalf of the client script.

       Name the Script ID with 'customscript_workflow_approval_cs'

       This does *not* require to be deployed on a record nor any built-in client-side function (e.g. Page Init, Save Record, etc).

 

4. Create a Before Load (User Event) script to add a button and deploy it on Sales Order:

       function myUserEventBeforeLoad(type, form) {
          form.addButton('custpage_acctg_approved', 'Approve (Accounting)', 'myClientValidation()');
          form.setScript('customscript_workflow_approval_cs');
       }

 

Finally, pull up a Sales Order.  If validation fails, script throws an error

         [Screenshot Missing]

 

But if successful, confirmation box appears 

         [Screenshot Missing]

 

After the Workflow fires, approval Set Field Values take place.

         [Screenshot Missing]

 

 

No comments:

Post a Comment