The sample code in this topic is for a qualifier that checks whether the policy holder of a Policy changed. An Involved custom field in the Policy custom object definition specifies the policy holder. If the primary key of the policy holder is the same as the primary key of the policy holder in the previous state of the record, the qualifier returns true, and the action runs. Otherwise, the qualifier returns false.
Before attempting to run this rule, you must create a custom field of type Involved in the root of the Policy custom object definition. Then, you specify this custom field as a parameter for the qualifier.
As explained in Parameters in Qualifiers and Actions, all rule parameters use the Parameters class and the declareParameter() method of a rule condition or action. Because the example rule compares the contents of an involved custom field in two states of a record, we need to be able to locate that custom field. The Parameter.addTextParameter() method in the example locates the field by taking three arguments:
•The label of the parameter. This text describes the custom field.
•The name of the parameter. This text identifies the custom field.
•The default value of the parameter.
Specifying this parameter results in a text box with the appropriate label in the Qualifiers tab of the rule. The following code sample shows how to define this text box using a parameter.
import com.mitratech.teamconnect.enterprise.api.custom.CustomCondition; public class MyParameterizedCustomCondition extends CustomCondition<Project> { @Override public void declareParameters() { parameters.addTextParameter("My Parameter Label", "myParameter", "defaultValue"); } @Override public boolean condition(final Project dispute) { // Place code for all conditions here. } |
After defining the parameter, the rule must be able to compare the policy holder in the previous version of the policy with the policy holder in the new version. The rule should return true if the primary keys of the custom fields' contents are the same. When writing the code, the rule must perform the following actions:
•Retrieve the associated project.
•Retrieve the old state of the record from the database.
•Retrieve the parameter using the name of the parameter, which is fieldKey for this sample.
Use the getTextParameterValue() method to retrieve the value: String fieldKey = parameters.getTextParameterValue("fieldKey");
The following code sample includes the parameter and conditions:
Completed Qualifier Example
import com.mitratech.teamconnect.enterprise.api.custom.CustomCondition; import com.mitratech.teamconnect.enterprise.api.model.Contact; import com.mitratech.teamconnect.enterprise.api.model.Project; public class CheckingWhetherPolicyHolderWasChanged extends CustomCondition<Project> { @Override public void declareParameters() { parameters.addTextParameter("Policy custom field name", "policyCustomFieldName", "policyHolder"); } @Override public boolean condition(final Project dispute) { // Getting the old object. Project oldDispute = platform.getProjectService().read(dispute); // Getting parameter value that indicates custom field key String policyCustomFieldName = parameters.getTextParameterValue("policyCustomFieldName"); // Getting policy holder from the old object's custom field. Contact oldPolicyHolder = oldDispute.getInvolvedFieldValue(policyCustomFieldName).getContact(); // Getting policy holder from the current object's custom field. Contact currentPolicyHolder = dispute.getInvolvedFieldValue(policyCustomFieldName).getContact(); // If the policy holder in the old object is not the policy holder in the current object, return true. return !oldPolicyHolder.getPrimaryKey().equals(currentPolicyHolder.getPrimaryKey()); } } |