Additional feature to pre-determine children list to clone

This commit is contained in:
Christopher Gary committed 2021-04-14 07:34:56 -04:00
1 parent 21df2d0bd2
commit a33604b498
10 files changed
+267 -37

No files matched your search

@@ -5,6 +5,7 @@
* @version 1.0
* @group Opportunity Cloning
* @history 2021-04-02 - CJG - Created
* 2021-04-12 - CJG - Added setting to restrict child records shown to a particular List
*/
public with sharing class OpportunityCloneService {
/**
@@ -23,9 +24,22 @@ public with sharing class OpportunityCloneService {
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) {
Schema.DescribeSObjectResult childObjRes = childRel.getChildSObject().getDescribe();
opportunityChildrenMap.put(childObjRes.getName(),childObjRes);
if(String.isNotBlank(childRel.getRelationshipName())){
Schema.DescribeSObjectResult childObjRes = childRel.getChildSObject().getDescribe();
if(!restrictedRelationshipList.isEmpty()){
if(restrictedRelationshipList.contains(childObjRes.getName())){
opportunityChildrenMap.put(childRel.getRelationshipName(),childObjRes);
}
} else {
opportunityChildrenMap.put(childRel.getRelationshipName(),childObjRes);
}
}
}
}
/**
@@ -73,6 +87,7 @@ public with sharing class OpportunityCloneService {
List<String> childRelationshipsToClone) {
//dynamic query in order to find the original opportunity.
String originalOppQueryString = ObtainQueryStringForOppAndChildren(oppId, childRelationshipsToClone);
System.debug(LoggingLevel.INFO,'\n\n*****\nQuery: ' + originalOppQueryString + '\n*****\n\n');
List<Opportunity> oppList = Database.query(originalOppQueryString);
//if the original opp cannot be found, throw and exception
if(oppList.isEmpty()){
@@ -125,7 +140,11 @@ public with sharing class OpportunityCloneService {
childQueryString += relationship + ')';
subqueryList.add(childQueryString);
}
queryString += ', ' + String.join(subqueryList,',') + 'FROM Opportunity WHERE Id =\'' + oppRecordId + '\'';
if(!subqueryList.isEmpty()){
queryString += ', ' + String.join(subqueryList,',') + 'FROM Opportunity WHERE Id =\'' + oppRecordId + '\'';
} else {
queryString += ' FROM Opportunity WHERE Id =\'' + oppRecordId + '\'';
}
return queryString;
}
@@ -147,6 +166,10 @@ public with sharing class OpportunityCloneService {
fieldNameList.add(fieldTokenDescribe.getName());
}
}
//need to account for weird objects where not all of the fields come back
if(objectName.toUpperCase() == 'OPPORTUNITYLINEITEM'){
fieldNameList.add('PricebookEntryId');
}
return fieldNameList;
}
@@ -155,6 +178,9 @@ public with sharing class OpportunityCloneService {
for(SObject rec:recList){
SObject newRec = rec.clone(false,true,false,false);
newRec.put(parentFieldName, newParentId);
if(newRec.getSObjectType().getDescribe().getName().toUpperCase() == 'OPPORTUNITYLINEITEM') {
newRec.put('UnitPrice',null);
}
newList.add(newRec);
}
return newList;
@@ -163,6 +189,9 @@ public with sharing class OpportunityCloneService {
private static Object CreateObjectValueFromString(String fieldName, String fieldValue) {
Map<String,Schema.SObjectField> fieldSObjectMapping = Schema.Opportunity.getSObjectType().getDescribe().fields.getMap();
Schema.DisplayType fieldType = fieldSObjectMapping.get(fieldName).getDescribe().getType();
if(String.isBlank(fieldValue)){
return null;
}
Object returnObj;
switch on fieldType {
when ADDRESS, COMBOBOX, EMAIL, ENCRYPTEDSTRING, MULTIPICKLIST, PHONE, PICKLIST, STRING, TEXTAREA,URL {
@@ -175,7 +204,7 @@ public with sharing class OpportunityCloneService {
returnObj = Double.valueOf(fieldValue);
}
when DATE {
returnObj = Date.parse(fieldValue);
returnObj = Date.valueOf(fieldValue);
}
when DATETIME {
returnObj = Datetime.parse(fieldValue);