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:
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;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user