Adds rule-based filtering for cloned records

Implements a mechanism to exclude child records from being cloned based on configurable formulas.

Introduces a Custom Metadata object to define exclusion rules, allowing administrators to specify formulas for different SObjects.

Updates the cloning service to evaluate these formulas and filter out records that meet the exclusion criteria.
This commit is contained in:
cgary committed 2025-04-21 16:39:07 -04:00
1 parent 81b24f298b
commit f34a61e653
67 files changed
+127 -26

No files matched your search

@@ -19,30 +19,50 @@ public with sharing class OpportunityCloneService {
* @description Map of Child relationship objects to the opportunity.
*/
public static Map<String,Schema.DescribeSObjectResult> opportunityChildrenMap;
public static Map<String,SmartCloneRecordExclusionRule__mdt> exclusionRuleMap;
public static Map<String,List<FormulaEval.FormulaInstance>> formulaInstanceBySObjectMap;
/**
* @description static constructor that will populate the opportunityChildrenMap
*/
static {
List<Schema.ChildRelationship> childRelationshipList =
Schema.Opportunity.getSObjectType().getDescribe().getChildRelationships();
opportunityChildrenMap = new Map<String, Schema.DescribeSObjectResult>();
List<String> restrictedRelationshipList = new List<String>();
String restrictedChildList = Label.SmartCloneChildList;
if(String.isNotBlank(restrictedChildList)){
restrictedRelationshipList = restrictedChildList.split(';');
}
for(Schema.ChildRelationship childRel:childRelationshipList) {
if(String.isNotBlank(childRel.getRelationshipName())){
Schema.DescribeSObjectResult childObjRes = childRel.getChildSObject().getDescribe();
if(!restrictedRelationshipList.isEmpty() && restrictedRelationshipList[0] != 'N/A'){
if(restrictedRelationshipList.contains(childObjRes.getName())){
opportunityChildrenMap.put(childRel.getRelationshipName(),childObjRes);
}
} else {
opportunityChildrenMap = new Map<String, Schema.DescribeSObjectResult>();
List<String> restrictedRelationshipList = new List<String>();
String restrictedChildList = Label.SmartCloneChildList;
if(String.isNotBlank(restrictedChildList)){
restrictedRelationshipList = restrictedChildList.split(';');
}
for(Schema.ChildRelationship childRel:childRelationshipList) {
if(String.isNotBlank(childRel.getRelationshipName())){
Schema.DescribeSObjectResult childObjRes = childRel.getChildSObject().getDescribe();
if(!restrictedRelationshipList.isEmpty() && restrictedRelationshipList[0] != 'N/A'){
if(restrictedRelationshipList.contains(childObjRes.getName())){
opportunityChildrenMap.put(childRel.getRelationshipName(),childObjRes);
}
} else {
opportunityChildrenMap.put(childRel.getRelationshipName(),childObjRes);
}
}
}
exclusionRuleMap = new Map<String,SmartCloneRecordExclusionRule__mdt>();
formulaInstanceBySObjectMap = new Map<String,List<FormulaEval.FormulaInstance>>();
Map<String,Schema.SObjectType> sObjectTypeMap = Schema.getGlobalDescribe();
for(SmartCloneRecordExclusionRule__mdt rule:SmartCloneRecordExclusionRule__mdt.getAll().values()){
if(rule.IsActive__c && String.isNotBlank(rule.ExclusionFormula__c)){
exclusionRuleMap.put(rule.DeveloperName,rule);
String sObjectName = rule.RelatedObject__c;
FormulaEval.FormulaInstance fInstance = Formula.builder().withType(sObjectTypeMap.get(sObjectName.toLowerCase()))
.withReturnType(FormulaEval.FormulaReturnType.Boolean)
.withFormula(rule.ExclusionFormula__c).build();
List<FormulaEval.FormulaInstance> fInstanceList = formulaInstanceBySObjectMap.get(sObjectName);
if(fInstanceList == null){
fInstanceList = new List<FormulaEval.FormulaInstance>();
}
fInstanceList.add(fInstance);
formulaInstanceBySObjectMap.put(sObjectName,fInstanceList);
}
}
}
/**
* @description Lightning Exposed method that will provide a map of Child relationships
@@ -185,16 +205,30 @@ public with sharing class OpportunityCloneService {
}
private static List<SObject> CloneAndReassociateRecords(List<SObject> recList, String parentFieldName, Id newParentId) {
System.debug(LoggingLevel.INFO,'\n\nOpportunityCloneService.CloneAndReassociateRecords Executing.\n\n ');
List<SObject> newList = new List<sObject>();
for(SObject rec:recList){
SObject newRec = rec.clone(false,true,false,false);
newRec.put(parentFieldName, newParentId);
/**
if(newRec.getSObjectType().getDescribe().getName().toUpperCase() == 'OPPORTUNITYLINEITEM') {
String sObjectName = newRec.getSObjectType().getDescribe().getName();
if(sObjectName.toUpperCase() == 'OPPORTUNITYLINEITEM') {
newRec.put('UnitPrice',null);
}
*/
newList.add(newRec);
//TODO: This is where we can evaluate the record to determine if it meets the formula criteria being included
// as a child record.
Boolean fResult = false;
if(formulaInstanceBySObjectMap.containsKey(sObjectName)){
List<FormulaEval.FormulaInstance> fInstanceList = formulaInstanceBySObjectMap.get(sObjectName);
for(FormulaEval.FormulaInstance fInstance:fInstanceList){
fResult = (Boolean)fInstance.evaluate(newRec);
if(fResult) {
break;
}
}
}
if(!fResult) {
newList.add(newRec);
}
}
return newList;
}
@@ -70,6 +70,7 @@ private class OpportunityCloneServiceTest {
}
}
insert oliTestLines;
testFutureMethod();
}
@isTest
@@ -106,10 +107,11 @@ private class OpportunityCloneServiceTest {
}
@isTest
static void CloneOpportunityWithChildren(){
Test.startTest();
Opportunity testOpp = [SELECT Id, Name FROM Opportunity LIMIT 1];
Map<String,String> testReplacements = new Map<String,String>();
testReplacements.put('Name',TestCloneUtility.GenerateRandomString(10));
Test.startTest();
Id testResult = OpportunityCloneService.CloneOpportunityWithChildren(testOpp.Id, testReplacements,
new List<String>{'OpportunityLineItems'});
@@ -117,4 +119,9 @@ private class OpportunityCloneServiceTest {
Test.stopTest();
}
@future
public static void testFutureMethod(){
//insert some Exlusion rules
Test.loadData(SmartCloneRecordExclusionRule__mdt.SObjectType, 'SmartClone_TestData');
}
}
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<CustomMetadata xmlns="http://soap.sforce.com/2006/04/metadata" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<label>QuantityLessThanTwo</label>
<protected>false</protected>
<values>
<field>ExclusionFormula__c</field>
<value xsi:type="xsd:string">Quantity &lt; 2</value>
</values>
<values>
<field>IsActive__c</field>
<value xsi:type="xsd:boolean">false</value>
</values>
<values>
<field>RelatedObject__c</field>
<value xsi:type="xsd:string">OpportunityLineItem</value>
</values>
</CustomMetadata>
@@ -0,0 +1,2 @@
DeveloperName,MasterLabel,IsActive__c,RelatedObject__c,ExclusionFormula__c
QuantityLessThanThree,QuantityLessThanThree,TRUE,OpportunityLineItem,Quantity < 3
1 DeveloperName MasterLabel IsActive__c RelatedObject__c ExclusionFormula__c
2 QuantityLessThanThree QuantityLessThanThree TRUE OpportunityLineItem Quantity < 3
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<StaticResource xmlns="http://soap.sforce.com/2006/04/metadata">
<cacheControl>Public</cacheControl>
<contentType>text/csv</contentType>
<description>Test Data for SmartClone Code Coverage.</description>
</StaticResource>