Show/Hide Toolbars

You can use the following samples of automated actions written in Java with rules of type Custom Action:

Copying Values from Parent to Multiple Child Records

Creating a History Record

Updating Added Categories

Checking and Setting Values in Custom Fields of Type List

Creating Related Task Record

For parameterized rule action samples, see Parameterized Actions. For wizard page action samples, see Wizard Page Actions.

Copying Values from Parent to Multiple Child Records

The following Java action gets the values of two custom fields of type date in the current object record (custom object Complaint) and copies the values of these fields into date fields in the child records of a particular custom object definition (custom object Served Company).

Business Rule

When the Service Date and Received Date custom fields in the parent complaint record update, the same values must copy to the corresponding Service Date and Received Date fields in the child served company records.

Action

Copy the values from the date custom fields in the parent complaint record to the corresponding date custom fields in all of its child served company records.

Sample Values

Object Definition:

COMP (custom object Complaint)

SECO (custom object Served Company, child of Complaint)

Categories:

COMP (root category of Complaint)

SECO (root category of Served Company)

Custom Fields:

In Complaint:

serviceDate (Date)

receivedDate (Date)

In Served Company:

serviceDate (Date)

receivedDate (Date)

Java Action: Copying Values from Parent to Multiple Child Records

public class CopyingValuesFromParentToMultipleChildRecords extends CustomAction<Project> {

@Override

public void action(final Project project) {

// Run as system user to bypass security

platform.getUtilityService().runAsSystemUser(new Callable() {

@Override

public void call() {

// Get service date and received date field values from parent

CalendarDate serviceDate = project.getCalendarDateFieldValue("COMP", "serviceDate");

CalendarDate receivedDate = project.getCalendarDateFieldValue("COMP", "receivedDate");

 

// Get a set of child objects for a specific child object definition

List<Project> servedCompanies = platform.getProjectService().getChildProjects(project, "SECO");

 

for (Project company : servedCompanies) {

// Set the values from the parent project fields onto each child

company.setCalendarDateFieldValue("SECO", "serviceDate", serviceDate);

company.setCalendarDateFieldValue("SECO", "receivedDate", receivedDate);

}

}

});

}

}

Creating a History Record

The following Java action creates a history record and relates it to the current object, which is a contact record. This action would be executed whenever a contact's name is updates.

Business Rule

If the name of a contact modifies, write the following information to the contact History, under the Data Change category:

Old name of the contact.

New name of the contact.

Date when the change occurred.

ID of the user who made the change.

Action

Create a new history record with the following information in its fields:

Text: Contact name changed from <old name> to <new name>. Changed by <user ID>.

Note: The date of the change was made is automatically recorded in the Created On system field of the history record.

Default Category: Data Change

Parent: Contact record where name change took place.

Entered by: User who made the change.

Sample Values

Object Definition:

contact (system object Contact)

Categories:

HIST_DACH (category of History)

Custom Fields:

None

Java Action: Creating a History Record

public class CreatingAHistoryRecord extends CustomAction<Contact> {

 

@Override

public void action(final Contact contact) {

// Run as system user

platform.getUtilityService().runAsSystemUser(new Callable() {

@Override

public void call() {

// Get old contact

final Contact oldContact = platform.getContactService().readLastSaved(contact);

 

// Create a string for history text using old and new values of the contact's name

String historyText = String.format("Contact name changed from %s to %s",

oldContact.getDisplayString(), contact.getDisplayString());

 

// Create a history record and related it to the contact

History history = platform.getHistoryService().newHistory(historyText, contact);

 

// Set the primary category of the history to Data Change

history.setPrimaryCategory("HIST_DACH");

}

});

}

}

Updating Added Categories

The following Java action removes specific categories from the current object record and adds other categories.

In this example, when the value of the Pre/Post custom field in a claim record updates, the corresponding Pre or Post category is added. Each project can have only one of the two categories at a time. This action automatically sets the categories according to the value in the Pre/Post field.

Business Rule

When the Pre/Post field value updates, the corresponding Pre or Post category is added to a Claim project. Each project can have only one of the two categories at a time.

Action

Delete the old category and add the new category.

Sample Values

Object Definition:

CLAM (custom object Claim)

Categories:

CLAM (root category of Claim)

CLAM_POST

CLAM_PREE

Custom Fields:

PrePost (List)

Custom Field Value:

PRPO_ROOT_PREE (full tree position of lookup item)

Java Action: Updating Added Categories

package com.mitratech.teamconnect.enterprise.api.model.rule;

import com.mitratech.teamconnect.enterprise.api.callable.Callable;

import com.mitratech.teamconnect.enterprise.api.model.Project;

public class UpdatingAddedCategories extends CustomAction<Project> {

@Override

public void action(final Project record) {

// The record must be declared as final if it is going to be referenced in a Callable

// Get an instance of the utility service in order to run code as system user

platform.getUtilityService().runAsSystemUser(new Callable() {

public void call() {

// Getting the value of the Pre/Post custom field

String newPrePostKey = record.getProjectFieldValue("CLAM", "PrePost").getUniqueKey();

if (newPrePostKey.equals("PRPO_ROOT_PREE")) {

record.removeCategory("CLAM_POST");

record.addCategory("CLAM_PREE");

} else {

record.removeCategory("CLAM_PREE");

record.addCategory("CLAM_POST");

}

}

});

}

}

Checking and Setting Values in Custom Fields of Type List

The following Java action automatically sets the value of one custom field according to the value selected in another custom field.

Business Rule

If the value in the Ice Cream custom field is Chocolate Cake, Chocolate Chip Cookie Dough, Moose Tracks, or Chocolate Peanut Butter, select Good in the Tastiness custom field. Otherwise, select Okay.

Action

Set the Tastiness custom field value to Good or Okay.

Sample Values

Object Definition:

project (custom object)

Custom Fields:

icecream (List)

Tastiness (String)

Custom Field List Values:

In custom lookup table icecream:

ICRM_ROOT_CHOC_CAKE

ICRM_ROOT_CCHP_CDGH

ICRM_ROOT_MOOS_TRKS

ICRM_ROOT_CHOC_PBTR

In custom lookup table Tastiness:

Good

Okay

Java Action: Checking and Setting Values in Custom Fields of Type List

public class IceCreamCondition extends CustomCondition<Project> {

 

@Override

public boolean condition(Project project) {

// Getting the value of the ice cream custom lookup field

String flavor = project.getLookupFieldValue("icecream").getKey();

/* Check whether the value of the field is one of the four ice cream flavors that contains chocolate */

if (flavor.equals("CHOC_CAKE") || flavor.equals("CCHP_CDGH") || flavor.equals("MOOS_TRKS") || flavor.equals("CHOC_PBTR")) {

// If value is one of the four values, set "Tastiness" custom field to Good.

project.setTextFieldValue("Tastiness", "Good");

} else {

// If value is NOT one of the four values, set "Tastiness" custom field to Okay.

project.setTextFieldValue("Tastiness", "Okay");

}

 

/* This code can also be done without using keys, such as getting the value of a text field which is not going to have a key associated with it. */

 

// Getting the value of the ice cream custom lookup field

String flavorText = project.getTextFieldValue("icecream");

/* Check whether the value of the field is one of the four ice cream flavors that contains chocolate */

return (flavorText.equals("CHOCOLATE CAKE") || flavorText.equals("CHOCOLATE CHIP COOKIE DOUGH") || flavorText.equals("MOOSE TRACKS") || flavorText.equals("CHOCOLATE PEANUT BUTTER"));

}

}

Creating Related Task Record

The following Java action creates a task record, relates it to the current object, and sets the default category. The due date of the task is set to 15 days before the date in a custom field in the current object.

Business Rule

When the Trial Date field in a matter record updates with a value, create a task for the main assignee to complete the executive summary, with the due date 15 days before the trial date.

Action

Create a task with the following information in its fields:

Parent Project:

The Matter project

Assignee:

Main project assignee

Due On:

15 days before the trial date

Description:

Complete executive summary

Default Category:

Trial Preparation

Sample Values

Object Definition:

MATT (custom object Matter)

Related Object Created:

Task (system object)

Categories:

MATT (root category of Matter)

TASK_TRPR (Task category)

Custom Fields:

trialDate (Date field in Matter, created under the root category)

Java Action: Creating Related Task Record

public class CreatingRelatedTaskRecord extends CustomAction<Project> {

@Override

public void action(final Project project) {

// Run as system user to bypass security

platform.getUtilityService().runAsSystemUser(new Callable() {

@Override

public void call() {

// Creating task and assigning it to main assignee of current object

Task task = platform.getTaskService().newTask("Complete executive summary", project.getMainAssignee().getUser());

CalendarDate trialDate = project.getCalendarDateFieldValue("MATT", "trialDate");

 

// Set the due on date to fifteen days before the trial date

Calendar dueOn = Calendar.getInstance();

dueOn.setTime(trialDate);

dueOn.add(Calendar.DAY_OF_YEAR, -15);

task.setDueDate(new CalendarDate(dueOn));

 

// Set primary category to trial preparation

task.setPrimaryCategory("TASK_TRPR");

}

});

}

}