Share:
Developers » Reference Tools » Unit Tests » Unit Test Script Example » Populate Assignee on Case Based on Amount Field Value

PREVIOUS  |  NEXT

Populate Assignee on Case Based on Amount Field Value


The following steps show how to write a script that will automatically assign a case to a user based on the Amount field value on a form. Then, we will give an example of how to write the unit test to test the script.

  1. Ensure you have already created the Amount target custom field, and that the field has been added to a form. For more information, see Create New Custom Field and Form Designer.
  2. Create the script that you will later add to the workflow action.
  3. Select the Script option from the New menu, or click the Create New link from the instructional text on the Scripts workspace.
  4. Select the Expand icon to expand or collapse additional information.
  5. Complete the form fields:
    • Name: Name the script Set Assignee by Amount for this example.
    • Script Type: Select the Form Event script type.
    • Sub Type: You do not need to select a sub type for this script, since we are testing against a custom field that does not have a table controller.
    • System ID: A unique identifier, automatically generated when you name an object. You can use this identifier to reference an object in scripts, templates, tags, and through the API. You can change the System ID by selecting the pencil icon. Use caution when changing the ID as it can affect existing scripts associated with the object. For more information, see System ID.

    • Language: Defaults to Groovy. You cannot change the scripting language.
    • Description: Add a brief description. This is used as an internal note for administrators.
  6. Use the resource toolbar on the right side of the workspace to look up the system ID for the field:
    • Expand the Find System ID panel.
    • Start typing the name of your custom field in the Custom Field box then select the field to populate the system ID to use in your code.
  7.  Write your script in the Code window using the Groovy scripting language. Place your cursor in the Code window and press F11 to enter full-screen mode. To exit the full-screen mode, press F11 or Esc. Enter the following code: 

    //Populate amount on form: workflow = Approval, status = Created > Created, action = assign
    def amount= form.getField('amount');
    if(amount!=null){
    	def amountValue = amount.getValue();
        if (amountValue > 1000){
        	currentCase.setAssignee(resp.users.getUser('smith' ))
        }
        else {
        	currentCase.setAssignee(resp.users.getUser('admin' ))
        }
        resp.cases.update(currentCase);
    }
    

    Note: It is good practice to use comments in your scripting.

  8. Select Validate to check the code syntax. Workflow displays the results of the script execution in the Output tab below the Code window.
  9. Select Save to save your work without exiting. Workflow saves your script alphabetically in the ROOT folder.
  10. Click the icon, or use your middle mouse button to close the script.
  11. Add the script to the workflow action. See Add New Action and Action Events for more information.