Modifications to lightning component
This commit is contained in:
1 parent
2622f14385
commit
21df2d0bd2
16 files changed
+363
-7
No files matched your search
@@ -12,7 +12,13 @@ public with sharing class OpportunityCloneService {
|
||||
* clonde process.
|
||||
*/
|
||||
public class CloneException extends System.Exception {}
|
||||
/**
|
||||
* @description Map of Child relationship objects to the opportunity.
|
||||
*/
|
||||
public static Map<String,Schema.DescribeSObjectResult> opportunityChildrenMap;
|
||||
/**
|
||||
* @description static constructor that will populate the opportunityChildrenMap
|
||||
*/
|
||||
static {
|
||||
List<Schema.ChildRelationship> childRelationshipList =
|
||||
Schema.Opportunity.getSObjectType().getDescribe().getChildRelationships();
|
||||
@@ -22,12 +28,93 @@ public with sharing class OpportunityCloneService {
|
||||
opportunityChildrenMap.put(childObjRes.getName(),childObjRes);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Lightning Exposed method that will provide a map of Child relationships
|
||||
* to an Opportunity that can be cloned.
|
||||
* @return Map<String,String> Map where the key represents the relationship name and the value is the plural label
|
||||
* of the child object.
|
||||
*/
|
||||
@AuraEnabled(cacheable=true)
|
||||
public static List<String> ObtainOpporunityChildRelationships(){
|
||||
return new List<String>(opportunityChildrenMap.keySet());
|
||||
public static Map<String,String> ObtainOpporunityChildRelationships(){
|
||||
//return new List<String>(opportunityChildrenMap.keySet());
|
||||
Map<String,String> relationshipMap = new Map<String,String>();
|
||||
for(String key:opportunityChildrenMap.keySet()){
|
||||
Schema.DescribeSObjectResult res = opportunityChildrenMap.get(key);
|
||||
if(res.isAccessible() && res.isCreateable()){
|
||||
String pluralLabel = res.getLabelPlural();
|
||||
relationshipMap.put(key,pluralLabel);
|
||||
}
|
||||
}
|
||||
return relationshipMap;
|
||||
}
|
||||
/**
|
||||
* @description Lightning Exposed method that will provide a Map of fields in a fieldset on the
|
||||
* opportunity.
|
||||
* @param fieldsetName The name of the fieldset to use
|
||||
* @return Map<String,Boolean> Map where the key is the apiFieldName and the value is whether the field is marked as
|
||||
* 'required' in the fieldset.
|
||||
*/
|
||||
@AuraEnabled(cacheable=true)
|
||||
public static Map<String, Boolean> ObtainCloneOpportunityFields(String fieldsetName){
|
||||
Map<String,Boolean> fieldsToReturn = new Map<String, Boolean>();
|
||||
Map<String,Schema.FieldSet> fieldSetMap = Schema.Opportunity.getSObjectType().getDescribe().fieldsets.getMap();
|
||||
Schema.FieldSet fieldSetItem = fieldSetMap.get(fieldsetName);
|
||||
if(fieldSetItem == null){
|
||||
throw new OpportunityCloneService.CloneException('No field set named ' + fieldsetName + 'found on the Opportunity.');
|
||||
}
|
||||
for(Schema.FieldSetMember fieldMember:fieldSetMap.get(fieldsetName).getFields()) {
|
||||
fieldsToReturn.put(fieldMember.getFieldPath(),fieldMember.getRequired());
|
||||
}
|
||||
return fieldsToReturn;
|
||||
}
|
||||
|
||||
@AuraEnabled
|
||||
public static Id CloneOpportunityWithChildren(Id oppId, Map<String,String> newValues,
|
||||
List<String> childRelationshipsToClone) {
|
||||
//dynamic query in order to find the original opportunity.
|
||||
String originalOppQueryString = ObtainQueryStringForOppAndChildren(oppId, childRelationshipsToClone);
|
||||
List<Opportunity> oppList = Database.query(originalOppQueryString);
|
||||
//if the original opp cannot be found, throw and exception
|
||||
if(oppList.isEmpty()){
|
||||
throw new AuraHandledException('There was a problem obtaining original opportunity.');
|
||||
}
|
||||
Opportunity originalOpp = oppList[0];
|
||||
//clone the original opportunity
|
||||
Opportunity clonedOpp = originalOpp.clone(false,true,false,false);
|
||||
//replace the values on the Opportunity with the new ones
|
||||
for(String fieldName:newValues.keySet()){
|
||||
clonedOpp.put(fieldName, CreateObjectValueFromString(fieldName,newValues.get(fieldName)));
|
||||
}
|
||||
//insert the new Opportunity
|
||||
try{
|
||||
insert clonedOpp;
|
||||
} catch(System.Exception ex){
|
||||
System.debug(LoggingLevel.INFO,'\n\n*****\nProblem creating new Opportunity: ' + ex.getMessage() + '\n*****\n\n');
|
||||
throw new AuraHandledException('Unable to create new opportunity.');
|
||||
}
|
||||
//now we try to loop through the children and clone the records
|
||||
List<Schema.ChildRelationship> childRelationships =
|
||||
Schema.Opportunity.getSObjectType().getDescribe().getChildRelationships();
|
||||
List<SObject> childRecsToInsert = new List<SObject>();
|
||||
for(Schema.ChildRelationship childRel:childRelationships){
|
||||
//if the child relationship name was passed in, get the records
|
||||
if(childRelationshipsToClone.contains(childRel.getRelationshipName())){
|
||||
List<SObject> childRecs = originalOpp.getSObjects(childRel.getRelationshipName());
|
||||
childRecsToInsert.addAll(CloneAndReassociateRecords(childRecs,
|
||||
childRel.getField().getDescribe().getName(),clonedOpp.Id));
|
||||
}
|
||||
}
|
||||
if(!childRecsToInsert.isEmpty()){
|
||||
try{
|
||||
insert childRecsToInsert;
|
||||
} catch(System.Exception ex) {
|
||||
System.debug(LoggingLevel.INFO,'\n\n*****\nError cloning children: ' + ex.getMessage() + '\n*****\n\n');
|
||||
throw new AuraHandledException('Unable to clone children.');
|
||||
}
|
||||
}
|
||||
return clonedOpp.Id;
|
||||
}
|
||||
|
||||
public static String ObtainQueryStringForOppAndChildren(Id oppRecordId, List<String> relationships) {
|
||||
String queryString = 'SELECT ';
|
||||
queryString += String.join(ObtainFieldsForObject('Opportunity'),',');
|
||||
@@ -62,4 +149,53 @@ public with sharing class OpportunityCloneService {
|
||||
}
|
||||
return fieldNameList;
|
||||
}
|
||||
|
||||
private static List<SObject> CloneAndReassociateRecords(List<SObject> recList, String parentFieldName, Id newParentId) {
|
||||
List<SObject> newList = new List<sObject>();
|
||||
for(SObject rec:recList){
|
||||
SObject newRec = rec.clone(false,true,false,false);
|
||||
newRec.put(parentFieldName, newParentId);
|
||||
newList.add(newRec);
|
||||
}
|
||||
return newList;
|
||||
}
|
||||
|
||||
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();
|
||||
Object returnObj;
|
||||
switch on fieldType {
|
||||
when ADDRESS, COMBOBOX, EMAIL, ENCRYPTEDSTRING, MULTIPICKLIST, PHONE, PICKLIST, STRING, TEXTAREA,URL {
|
||||
returnObj = fieldValue;
|
||||
}
|
||||
when BOOLEAN {
|
||||
returnObj = Boolean.valueOf(fieldValue);
|
||||
}
|
||||
when CURRENCY, DOUBLE, PERCENT {
|
||||
returnObj = Double.valueOf(fieldValue);
|
||||
}
|
||||
when DATE {
|
||||
returnObj = Date.parse(fieldValue);
|
||||
}
|
||||
when DATETIME {
|
||||
returnObj = Datetime.parse(fieldValue);
|
||||
}
|
||||
when ID {
|
||||
returnObj = Id.valueOf(fieldValue);
|
||||
}
|
||||
when INTEGER {
|
||||
returnObj = Integer.valueOf(fieldValue);
|
||||
}
|
||||
when LONG {
|
||||
returnObj = Long.valueOf(fieldValue);
|
||||
}
|
||||
when TIME {
|
||||
returnObj = fieldValue;
|
||||
}
|
||||
when else {
|
||||
returnObj = fieldValue;
|
||||
}
|
||||
}
|
||||
return returnObj;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user