Skip to main content

Groovy Script - Available Options for aMessages

This article will explain the changes you can implement to aMessages via a Groovy Script.

Updated yesterday

To create a system task from the frontend as a user, you can implement a Groovy Script. Groovy Scripts allow you to execute certain actions depending on your requirements.

  • User tasks are completed manually by users.

  • System tasks are automated and completed by the system.


The following Groovy Script demonstrates how to automate tasks with specific reference to aMessages.

Implementing the Groovy Script

  1. Select the required project and navigate to the Workflows tab.

  2. Select +System Task (top right).

  3. In the pop-up, enter:

    • A name for the new System Task.

    • Its context (Document, App or Custom Object).

    • The task you select must always be Execute Groovy Script.

  4. Once Execute Groovy Script is selected, a text box will appear where you can paste your script.

  5. Click Create to save the System Task.


Options Available in this Groovy Script

  • aMessage Fields

    • Title - set a default title and choose whether it's editable.

    • Description - set a default description and choose whether it's editable.

    • Mark as Private - set the default (whether it's checked or unchecked) and choose whether it's editable.

    • Remove default recipients (users, roles, distribution groups or organisations)

    • Add new recipients (users, roles, distribution groups or organisations)

Context: Document

Remark: Accessible in 'On-Load' task mode only.

Script:

def execute(){

// Fetch current document details

DocumentVO document = documentService.getDocumentDetails();

//Prepare ADD/REMOVE Recipients JSON

JsonObject responseJSON = new JsonObject();

// ADD/REMOVE USERS

JsonArray usersList = new JsonArray();

setDistribution(document.getPublisherEmail(), Recipient.USER, Recipient.REMOVE, usersList); // Params: User Email, Type Of Recipient, ADD/REMOVE, listObject.

setDistribution("abc@example.com", Recipient.USER, Recipient.ADD, SystemAction.FOR_ACKNOWLEDGEMENT, "3", usersList); // Params: User Email, Type Of Recipient, ADD/REMOVE, Task to be assigned, Due days, listObject.

// ADD/REMOVE ROLE

JsonArray rolesList = new JsonArray();

setDistribution("Field Administrator", Recipient.ROLE, Recipient.ADD, SystemAction.FOR_COMMENT, "2", rolesList);

setDistribution("role 2", Recipient.ROLE, Recipient.REMOVE, rolesList);

// ADD/REMOVE GROUP

JsonArray groupList = new JsonArray();

setDistribution("Doc AAA", Recipient.GROUP, Recipient.REMOVE, groupList);

setDistribution("Doc BBB", Recipient.GROUP, Recipient.ADD, groupList);

// ADD/REMOVE ORG

JsonArray orgsList = new JsonArray();

setDistribution("R17 Test Inc", Recipient.ORG, Recipient.ADD, SystemAction.FOR_STATUS_CHANGE, "1", orgsList);

setDistribution("org 2", Recipient.ORG, Recipient.REMOVE, orgsList);

// Set other options of create discussion screen

responseJSON.add(IGroovyConstant.TITLE, addAttributes("true", "false", "Create Comment Reply Groovy.."));

responseJSON.add(IGroovyConstant.DESCRIPTION, addAttributes("true", "false", "Description From Groovy .."));

responseJSON.add(IGroovyConstant.MARK_AS_PRIVATE, addAttributes("true", "true", "true"));

JsonObject distributionList = new JsonObject();

distributionList.add(Recipient.USER, usersList);

distributionList.add(Recipient.ROLE, rolesList);

distributionList.add(Recipient.ORG, orgsList);

distributionList.add(Recipient.GROUP, groupList);

distributionList.addProperty(IGroovyConstant.CLEAR_DISTRIBUTION_LIST, "false"); // By setting this value to true, it will clear all the recipients from the distribution list in 'To' field.

distributionList.addProperty(IGroovyConstant.ENABLE_DISTRIBUTION_LIST,"false");

distributionList.addProperty(IGroovyConstant.APPLY_TO_EDIT_DRAFT,"true");

responseJSON.add(IGroovyConstant.DISTRIBUTION_LIST, distributionList);

document.setResponseStatus(false);

document.setDynamicObject(responseJSON.toString());

return document;

}

// Do not change anything in below functions.

// FOR REMOVE

public void setDistribution(String data, String distLevel, String operation, JsonArray arrayList) {

String ojbect = "name";

if(Recipient.USER.equals(distLevel)) {

ojbect = "email";

}

JsonObject jsonObject = new JsonObject();

jsonObject.addProperty(ojbect, data);

jsonObject.addProperty("operation", operation);

arrayList.add(jsonObject);

}

// TO ADD

public void setDistribution(String data, String distLevel, String operation, int actionId, String dueDays, JsonArray arrayList) {

String ojbect = "name";

if(Recipient.USER.equals(distLevel)) {

ojbect = "email";

}

JsonObject jsonObject = new JsonObject();

jsonObject.addProperty(ojbect, data);

jsonObject.addProperty("operation", operation);

jsonObject.addProperty("actionId", String.valueOf(actionId));

jsonObject.addProperty("dueDays", dueDays);

arrayList.add(jsonObject);

}

// PREPARE JSON

public JsonElement addAttributes(String applyToEditDraft, String editable, String defaultValue) {

JsonElement jsonObject = new JsonObject();

jsonObject.addProperty(IGroovyConstant.EDITABLE, editable);

jsonObject.addProperty(IGroovyConstant.VALUE, defaultValue);

jsonObject.addProperty(IGroovyConstant.APPLY_TO_EDIT_DRAFT, applyToEditDraft);

return jsonObject;

}


Creating the Trigger

Since the task mode of this script is On-Load, the System Task must be associated with a workflow trigger. To set up the Trigger:

  1. Select the required project and navigate to the Workflows tab.

  2. Select +Trigger (top right).

  3. In the pop-up, enter:

    • The Name, Priority and Description: mandatory but the values are up to your discretion.

    • Context: must match the Context used for the System Task you previously created.

    • The Conditions section (Field/Operator/Value) can usually stay empty unless you want the trigger to run only under specific metadata conditions.

    • Event: Discussion.

    • Task-Mode: On-Load.

    • Task: [your System Task].

  4. Click Create to save the Trigger.



Did this answer your question?