Developers » Script Examples by Type » Utils Script Example


Utils Script Example

Quick Links

Workflow provides a Utils script type that enables you to use the same code to execute the same operation in various places. You need to call the Utils script from another script using the resp.importScript() method. 

In this example, we create two Utils scripts and demonstrate how to call them from a Workflow type script. Then, we add the Workflow script to a workflow action.

Create Utils Scripts

[Back to top]

In this step, we create the two Utils type scripts. The first script changes the status of a case to Closed, depending on the workflow type, and the second doesn't change the status. These scripts support the resp.importScript methods that are later used in the Workflow script, as seen in lines 6 and 9 in the Workflow script example below. When you create the Utils scripts, note the system IDs that are used as arguments in the methods.

  1. Create the first script using the name Util Script - Change Status with the following code:

  2. //get the value of a "caze" globalValue which is defined in the Workflow type script created above
    def currentCase = resp.getGlobalValue('caze'); 
    //get the "Closed" status from currentCase
    def status = resp.getStatus('closed');
    //set the status of the currentCase to "Closed"
    //update the case
    //log the status activity
    logger.debug('Status', 'Status changed.');

  3. Create the second script using the name Util Script - No Status Change with the following code:
  4. //log the status activity
    logger.debug('Status', 'No status change.');

Create Workflow Script

[Back to top]

In this step, we check if our case type is RMA and we import the Utils scripts. If the case type is true for RMA, we will change the status; otherwise, we will not change the status.

  1. Go to Setup > Development > Scripts and select the Script option from the New menu, or click the Create New link from the instructional text on the Scripts workspace.
  2. Name the script Main Workflow Script.
  3. Select the Workflow script type.
  4. Enter the following code in the code window:
//check the case type def type = currentCase.getCaseType();

if(type == 'rma')
//Setting currentCase in "caze" global value which will be passed to the Utils script
//execute Utils script - change status
//execute Utils script - no status change
  1. Save your script.

Add Workflow Script to Action

[Back to top]

In the final step of this example, we add the newly created Workflow script to a workflow action.

  1. Go to Setup > Processes > Workflows and click the Edit button near the desired workflow.
  2. Select one of the existing actions or create one to which you will add the newly created Workflow script. See Add New Action for more information on creating a new action. 
  3. Select the After Workflow Action link in the Workflow Events section, in the Events panel.
  4. Add the Main Workflow Script created above to the Script Name field.
  5. Save your action.

After an end user executes this action, if the case is the RMA workflow type, the case will be moved to the Closed status.