Retrieving the Current State of a Workflow in SuiteScript

Overview

This SuiteScript 2.0 snippet is used to fetch the current state of a workflow for a record. The script searches for the record in a specific workflow and retrieves details, including the workflow’s current state.

How the Script Works

1. Creating the Search to Find the Current Workflow State

let SearchObj = search.create({
    type: "recordType",
    filters: [
        ["workflow.workflow", "anyof", WorkflowID], 
        "AND", 
        ["internalid", "anyof", recordID]
    ],
    columns: [
        search.createColumn({ name: "tranid", label: "Document Number" }),
        search.createColumn({ 
            name: "currentstate", 
            join: "workflow", 
            label: "Current State" 
        }),
        search.createColumn({ 
            name: "applyingtransaction", 
            label: "Applying Transaction", 
            sort: search.Sort.DESC 
        })
    ]
});
  • type: "recordType" → The search is performed on recordType records.
  • Filters applied:
  • Workflow Condition → The record must be associated with the workflow ID.
  • Record Filtering → The search focuses on a specific record using recordId.
  • Retrieving the Workflow State:
  • The script retrieves the current state of the workflow using currentstate from the workflow join.

2. Executing the Search and Extracting the Current Workflow State

let results = SearchObj.run().getRange({ start: 0, end: 1 });
if (results.length > 0) {
    let result = results[0];
    let currentState = result.getText({ name: "currentstate", join: "workflow" });

    let resultObj = {
        tranid : result.getValue("tranid"),
        currentstate : currentState,
        irId : result.getValue("applyingtransaction")
    }
    log.debug('Result Object', resultObj);
} else {
    log.debug("No Record Found", "No data for this Record");
}
  • Fetching the First Matching Record → The script retrieves only one record (start: 0, end: 1).
  • Extracting the Current Workflow State:
  • Uses getText({ name: "currentstate", join: "workflow" }) to fetch the workflow state name instead of the internal ID.
  • Logging the Current State:
  • The script logs the workflow state to help track the process.
  • Handling Missing Records:
  • If no records are found, it logs "No Record Found" for debugging purposes.

Use Case: Why Retrieve the Workflow State?

Retrieving the workflow state is crucial when automating processes that depend on the progress of a record within a workflow. Some common scenarios include:

  1. Conditional Processing Based on State:
  • Only trigger an action (e.g., send an email, update a field) if the record is in a specific workflow state.
  1. Monitoring Workflow Progress:
  • Track how a record moves through workflow states to identify delays or automate transitions.
  1. Debugging and Logging:
  • Helps in debugging workflows by capturing the exact state a record is in.

Conclusion

This SuiteScript search efficiently retrieves the current state of a workflow for a record. By utilizing workflow joins, it allows automation scripts to make informed decisions based on the record’s progress in the workflow.

Leave a comment

Your email address will not be published. Required fields are marked *