Setting up a rule in clcommon
This page enables you to setup the rules which can be executed as per the business requirements. You can create new rules or view/edit existing rules.
Prerequisites
The following is the prerequisite to setting up a rule:
-
Rule set must be created
Note:If rule set is not created, you can create new rule set from the Manage Rules page by selecting Add New Rule Set.
To enable the Use Enhanced Rules Org Parameters, perform the following steps:
Log in to your Salesforce account.
Go to Setup > Setup.
In the Quick Find box, search for Custom Settings and select it.
Select Org Parameters with the namespace prefix (genesis), and select Manage.
Select Edit, and then select the Use Enhanced Rule checkbox.
Select Save.
Steps
Perform the following steps to set up a rule:
Log in to your Salesforce account
Go to Origination Configuration > Credit and Pricing Setup > Credit Setup.
Select Manage Rules> New.
-
In the page that appears, select the Rule Set Name.
Note:You can edit the rule set name, by selecting the Edit button.
Once you select the Rule Set Name, a list of configured rules gets displayed. You can edit or disable/enable those rules by selecting the Edit or Disable/Enable button.
-
Select Add New Rule and specify the details in the Rule Details, Criteria Details, Evaluation Criteria, and Actions sections as described in the following tables:
Field Name Action and Information Rule Name Specify a Rule Name. This must be a unique name. For example, you may create a rule by the name Credit Approval or FICO score. Namespace Select a Namespace from the dropdown menu for the object type for which you want to create the rule. The dropdown menu consists of all the packages that are installed in the org.
Note:If you want to select an object which is either standard salesforce object or custom object without namespace then select None option from the dropdown menu.
Object Type Select the Object Type from the dropdown menu for which you are creating the rule. This dropdown menu consists of the object types as per the selected Namespace. Rule Set Name The Rule Set Name field is auto-populated on the basis of the selected Rule Set Name. If you want to change, select it from the dropdown menu as needed. Rule Priority Specify a number in the Rule Priority field as needed. This must be unique for all the rules in the selected rule set. External Id Specify an External Id as required. This must be unique across all rules. It is advised to name the external id similar to rule name and use only underscore (_) to separate the words as special character. To add details in the Criteria Details section, select the Add New Criteria button and provide the details as described in the following table:
Note:The Add New Criteria button is displayed only when Object Type is provided in the Rule Details section.
You can add multiple criteria by selecting the Add New Criteria button again.
You can edit or remove the criteria by selecting the Edit and Remove button.
Field Name Action and Information Sequence The sequence number of the defined criteria which is associated with the rule. Related Object The object type on which the rule is to be defined. This dropdown menu consists all the related objects of the selected object type. Field Name The field or attribute of the object that is being measured or evaluated through the evaluation rule. Lookup Object Field Name
This field displays the list of fields depending on the selected reference (lookup /master-detail) field in the Field Name.
Second Level Lookup Field This field displays the list of fields depending on the selected reference (lookup /master-detail) field in the Lookup Object Field Name. Operator Select the required operator to define value or the range of values for the field.
Value Type This field displays the following options:
Static: If selected, provide an actual value in the Value field with which you want to compare the provided Field Name/Lookup Field/Second Level Lookup Field.
Field Comparison: If selected, provide a field from the same related object with which you want to compare the provided Field Name/Lookup Field/Second Level Lookup Field.
Value This value is compared or matched with the provided Field Name/Lookup Field/Second Level Lookup Field while evaluating the criteria.
If the Operator is selected as BETWEEN, then the following values must be provided:
Maximum: The maximum range value of the provided field.
Minimum: The minimum range value of the provided field.
In the Evaluation Rule field, provide an evaluation expression to define the order in which the criteria must be evaluated.
In case of multiple criteria, this can be achieved using the evaluation expression.
For example:
$1 && $2
$1 || $2 || $3
$1 && ($2 || $3)
$1 && ( $2 || $3) && ($4 || $5)
This section enables you to define the actions once the rule criteria are evaluated.
In the Actions section, provide the details as described in the following table:
To add details in the Actions section, select the Add New Action button and provide the details as described in the following table:
Note:You can add multiple actions by selecting the Add New Action button again.
You can remove the actions, by selecting the Remove button.
Field Name Action and Information Sequence
The sequence number of the defined actions which are associated with the rule. Action This field displays the following actions:
Success Action: Select this, if you want to run this action when the result of the rule evaluation is true.
Failure Action: Select this, if you want to run this action when the result of the rule evaluation is false.
Class Name Specify an apex class name which must be executed after the rule evaluation. WhenIf the result of the rule evaluation is truerule evaluation result ors to true or success provide the success action class name otherwise failure action class name. success, provide a success action class name else provide a failure action class name.
Note:The provided class must either implement clcommon.IRuleAction3 interfaceinterface or extend clcommon.AbstractRuleAction class. or extend clcommon.AbstractRuleAction class.
Input Parameter The input parameter and their values can be passed to success or failure actions to evaluate the data on run-time.
The following fields are displayed under the Input Parameter:
Parameter Name
Parameter Value
Note:You can only add the parameter, if you have speicified a valid class name.
To add the details in the Parameter name and value, select Add Parameter.
Select the parameter name from the dropdown.
Specify a value in parameter value.
Note:You can only select the parameters from the drop-down as they are provided by the class.
You can add any parameter only once.
If you want to add additional parameters, you must add those in the given class.
Select Save.
Create Action Classes
To create action classes perform the following steps:
Navigate to the Developer Console > File > New > Apex Class.
Provide the class name. For example SuccessClass and FailureClass and select Ok.
-
Enter the class code in the newly created class tab.
Refer to the following samples of the SuccessClass and FailureClass:
SuccessClass SampleCopyglobal class SuccessClass implements clcommon.IRuleAction3 {
private Set<Id> recIds;
global void setIds(Set<Id> ids){
System.debug('success set id');
this.recIds = ids;
}
global void setRecordsMap(Map<Id,Map<String,Map<String,Object>>> recordMap){}
global Object evaluate(Map<String,Object> params){
System.debug('success parameters: '+params);
System.debug('Success rule action successclass');
return true;
}
// This method set all the parameters list for the Action
global List<String> getActionInputParameters(){
List<String> parameterList = new List<String> {'Action Type'};
return parameterList;
}
//This method will pass the unit of work transaction to add the records to be updated or inserted
global void setUnitOfWorkTransaction(mfiflexUtil.UnitOfWorkTransaction uowTransaction) {}
}Failureclass SampleCopyglobal class FailureClass implements clcommon.IRuleAction3 {
private Set<Id> recIds;
global void setIds(Set<Id> ids){
this.recIds = ids;
}
global void setRecordsMap(Map<Id,Map<String,Map<String,Object>>> recordMap){}
global Object evaluate(Map<String,Object> params){
System.debug('Failuere parameters: '+params);
System.debug('Failure rule action failclass');
return true;
}
//This method set all the parameters list for the Action
global List<String> getActionInputParameters(){
List<String> parameterList = new List<String> {'Action Type'};
return parameterList;
}
//This method will pass the unit of work transaction to add the records to be updated or inserted
global void setUnitOfWorkTransaction(mfiflexUtil.UnitOfWorkTransaction uowTransaction) {
}
} Navigate to File > Save or Select Ctrl + S to save.
Execute Rules
The rules can be executed either via any process automation or by running it through Developer Console.
If the rule needs to be executed for any customer specific condition or any custom logic then an invocable apex method can be created for calling the Rules API.
If the rule needs to be run only once then it can be executed via Developer Console.
Perform the following steps if you wish to execute the rules independently:
Navigate to the Developer Console window > Debug > Open Execute Anonymous Window.
-
Paste the following code after replacing the values mark to replace in the code:
CopyList<clcommon__Rule__c> ruleListForSelection = [SELECT ID,
Name,
clcommon__Enabled__c,
clcommon__Object_Type__c,
clcommon__Parent_Rule__c,
clcommon__Rule_Json__c,
clcommon__Rule_Priority__c,
clcommon__Rule_Set__c,
clcommon__Rule_Set__r.clcommon__Mode_of_Rule_Execution_On_Priority__c,
clcommon__Rule_Set__r.clcommon__Logging_Enabled__c
FROM clcommon__Rule__c
WHERE clcommon__Rule_Set__r.Name = 'ruleset1' //replace ruleset1 by name of rule set added
AND clcommon__Object_Type__c = 'genesis__Applications__c' //genesis will be replaced with org namespace if any
AND clcommon__Enabled__c = true
WITH SECURITY_ENFORCED
];
Set <Id> idSet = new Set<Id>();
idSet.add('ApplicationId'); //replace ApplicationId with Id of application(or any other object) added which is used for testing newly created rule
clcommon.AbstractRulesAPI rulesAPI = clcommon.APIFactory.getAbstractRulesAPI();
rulesAPI.setIDs(idSet);
rulesAPI.setCategory('ruleset1'); ///replace ruleset1 by name of rule set added
rulesAPI.setRules(ruleListForSelection);
rulesAPI.skipRulesQuery(true);//skip the query on Rules as ruleListForSelection has complete rules with fields
rulesAPI.evaluate(); Select Execute.
-
To check the rule result whether it was a successful or failure, go to Log panel, open the log for the above execution and search for Success rule action or Failure rule action depending on whether the criteria is satisfied or not (Search for SuccessClass or FailureClass according to above example).
Note:If the SuccessAction or FailureAction are updating any record(s), the updated value can be checked directly in those records.