Module 11: Workflow Action Scripts

Week 6 • NetSuite SuiteScript 2.0 Training • ~60 minutes

🎯 Learning Objectives

1. Understanding Workflows

SuiteFlow is NetSuite's visual workflow builder. It automates business processes without code - but Workflow Action Scripts let you extend it with custom logic.

💡 SuiteFlow vs SuiteScript
  • SuiteFlow: Point-and-click automation for business processes
  • SuiteScript: Code-based for complex logic and integrations
  • Together: Use workflows for process flow, scripts for custom actions

2. Workflow Elements

ElementPurposeExample
StateWhere you are in the processPending Approval, Approved, Rejected
ActionWhat to doSend Email, Set Field, Custom Action
TransitionHow to move between statesIf approved → go to Approved state
TriggerWhen to executeOn Entry, On Exit, Button Click

Built-in Actions

3. Workflow Action Scripts

When built-in actions aren't enough, create a Workflow Action Script:

/**
 * @NApiVersion 2.1
 * @NScriptType WorkflowActionScript
 */
define(['N/record', 'N/email'], function(record, email) {
    
    function onAction(context) {
        // context.newRecord = the record being processed
        // context.oldRecord = previous version (if edit)
        // context.workflowId = workflow internal ID
        // context.type = trigger event type
        
        var rec = context.newRecord;
        var total = rec.getValue('total');
        var customerId = rec.getValue('entity');
        
        // Custom logic here
        if (total > 10000) {
            // Send notification for large orders
            email.send({
                author: -5,
                recipients: 'manager@company.com',
                subject: 'Large Order Alert',
                body: 'Order #' + rec.getValue('tranid') + 
                      ' total: $' + total
            });
        }
        
        // Return a value (can be used in workflow conditions)
        return 'SUCCESS';
    }
    
    return { onAction: onAction };
});

4. Creating Custom Actions

Step 1: Create the Script

Create a WorkflowActionScript file as shown above.

Step 2: Create Script Record

  1. Customization → Scripting → Scripts → New
  2. Upload your file
  3. Save (no deployment needed for Workflow Actions)

Step 3: Add to Workflow

  1. Edit your Workflow
  2. Add an Action to a state
  3. Choose "Custom Action"
  4. Select your script
  5. Configure trigger (Entry, Exit, Button)

Returning Values

Your script can return values for workflow conditions:

function onAction(context) {
    var total = context.newRecord.getValue('total');
    
    if (total > 10000) {
        return 'LARGE_ORDER';
    } else if (total > 1000) {
        return 'MEDIUM_ORDER';
    } else {
        return 'SMALL_ORDER';
    }
}

// In workflow, add transitions based on return value:
// If return = 'LARGE_ORDER' → go to Manager Approval
// If return = 'SMALL_ORDER' → go to Auto Approved
⚠️ Execution Context

Workflow Action Scripts run server-side. They cannot access client-side features like alerts or DOM manipulation.

🏋️ Practice Exercises

Exercise 1: Basic Action

Create a Workflow Action Script that logs the record type and ID when triggered.

Exercise 2: Conditional Logic

Create an action that returns different values based on order total (use for workflow branching).

🎯 Key Takeaways

  • SuiteFlow handles business process automation visually
  • Workflow Action Scripts extend workflows with custom code
  • Scripts receive context with newRecord, oldRecord, workflowId
  • Return values can drive workflow transitions
  • No deployment needed - attach directly to workflow states
  • Use for logic too complex for built-in actions