Modifications to lightning component

This commit is contained in:
Christopher Gary committed 2021-04-12 05:44:06 -04:00
1 parent 2622f14385
commit 21df2d0bd2
16 files changed
+362 -6

No files matched your search

+6
View File
@@ -14,5 +14,11 @@ package.xml
# Exclude applications # Exclude applications
*.appMenu *.appMenu
# Exclude Layouts
*.layout
# Exclude FlexiPage # Exclude FlexiPage
*.flexipage *.flexipage
# Exclude FieldSets
*.fieldSet
@@ -0,0 +1,6 @@
<aura:documentation>
<aura:description>Documentation</aura:description>
<aura:example name="ExampleName" ref="exampleComponentName" label="Label">
Example Description
</aura:example>
</aura:documentation>
@@ -0,0 +1,3 @@
<aura:component implements="force:hasRecordId,force:lightningQuickActionWithoutHeader">
<c:opportunityCustomClone recordId="{!v.recordId}"></c:opportunityCustomClone>
</aura:component>
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<AuraDefinitionBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>51.0</apiVersion>
<description>A Lightning Component Bundle</description>
</AuraDefinitionBundle>
@@ -0,0 +1,2 @@
.THIS {
}
@@ -0,0 +1,3 @@
<design:component >
</design:component>
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 54.58 38.09">
<defs>
<style>.cls-1{fill:#616669;}.cls-2{fill:#6a8aa4;}</style>
</defs>
<title>CP-Social-Logo</title>
<path class="cls-1" d="M38.24,48.55v-17h10c2.64,0,4.23.12,4.77.2a3,3,0,0,1,1.39.57,2.87,2.87,0,0,1,1,1.18,8.31,8.31,0,0,1,.51,3.32,10.83,10.83,0,0,1-.27,2.71,3.21,3.21,0,0,1-.9,1.56,3.26,3.26,0,0,1-1.33.79,8.94,8.94,0,0,1-2.15.26c-.35,0-1.34,0-3,0H40.36v6.41Zm2.12-8.4h7.92c1.94,0,3.15,0,3.62-.09a2.19,2.19,0,0,0,1.2-.52,1.71,1.71,0,0,0,.49-.91,7.64,7.64,0,0,0,.15-1.77A9,9,0,0,0,53.61,35a1.44,1.44,0,0,0-.48-.86A2.34,2.34,0,0,0,52,33.67c-.46-.06-1.69-.11-3.68-.16H40.36v6.64Z" transform="translate(-2.54 -10.95)"/>
<path class="cls-2" d="M31,49.05a9,9,0,0,1-6.46-2.38A8.33,8.33,0,0,1,22,40.44a8.69,8.69,0,0,1,2.76-6.65,9.7,9.7,0,0,1,6.85-2.53A12,12,0,0,1,36,32l.64.26v4.25l-1.48-.75a7.76,7.76,0,0,0-3.53-.8,5.42,5.42,0,0,0-4,1.46,5.2,5.2,0,0,0-1.49,4A4.93,4.93,0,0,0,27.51,44a5,5,0,0,0,3.71,1.36,8,8,0,0,0,3.89-.89l1.51-.83v4.1l-.58.27A11.7,11.7,0,0,1,31,49.05Z" transform="translate(-2.54 -10.95)"/>
<path class="cls-2" d="M41,13.51a16.93,16.93,0,0,0-9.43,2.82,9.65,9.65,0,0,0-18.31,4.28,10.16,10.16,0,0,0,.18,1.84h-.12A10.62,10.62,0,0,0,2.54,32.86a10.59,10.59,0,0,0,10.14,10.4v0h.8l7.44,0a8.1,8.1,0,0,1-.36-1.75l-7.1,0h-.38l-.29,0a8.85,8.85,0,0,1-8.5-8.65,8.87,8.87,0,0,1,9.07-8.66h.09l2.15,0-.41-2.1A7.73,7.73,0,0,1,15,20.61,7.9,7.9,0,0,1,30,17.1l.88,1.79,1.66-1.11A15.15,15.15,0,0,1,41,15.26c7.9,0,14.33,5.87,14.33,13.08a11.78,11.78,0,0,1-.3,2.6c.16.09.33.17.49.28a3,3,0,0,1,.94,1.16,13.59,13.59,0,0,0,.62-4C57.11,20.15,49.91,13.51,41,13.51Z" transform="translate(-2.54 -10.95)"/>
</svg>

After

Width:  |  Height:  |  Size: 1.7 KiB

@@ -0,0 +1,5 @@
({
myAction : function(component, event, helper) {
}
})
@@ -0,0 +1,5 @@
({
helperMethod : function() {
}
})
@@ -0,0 +1,5 @@
({
// Your renderer method overrides go here
})
@@ -12,7 +12,13 @@ public with sharing class OpportunityCloneService {
* clonde process. * clonde process.
*/ */
public class CloneException extends System.Exception {} public class CloneException extends System.Exception {}
/**
* @description Map of Child relationship objects to the opportunity.
*/
public static Map<String,Schema.DescribeSObjectResult> opportunityChildrenMap; public static Map<String,Schema.DescribeSObjectResult> opportunityChildrenMap;
/**
* @description static constructor that will populate the opportunityChildrenMap
*/
static { static {
List<Schema.ChildRelationship> childRelationshipList = List<Schema.ChildRelationship> childRelationshipList =
Schema.Opportunity.getSObjectType().getDescribe().getChildRelationships(); Schema.Opportunity.getSObjectType().getDescribe().getChildRelationships();
@@ -22,10 +28,91 @@ public with sharing class OpportunityCloneService {
opportunityChildrenMap.put(childObjRes.getName(),childObjRes); 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) @AuraEnabled(cacheable=true)
public static List<String> ObtainOpporunityChildRelationships(){ public static Map<String,String> ObtainOpporunityChildRelationships(){
return new List<String>(opportunityChildrenMap.keySet()); //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) { public static String ObtainQueryStringForOppAndChildren(Id oppRecordId, List<String> relationships) {
@@ -62,4 +149,53 @@ public with sharing class OpportunityCloneService {
} }
return fieldNameList; 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;
}
} }
@@ -1,3 +1,24 @@
<template> <template>
<lightning-card title="Clone Opportunity">
<div class="slds-m-horizontal_small slds-grid slds-wrap slds-gutters">
<div class="slds-col slds-size_1-of-1">
<lightning-record-edit-form object-api-name={formObjectApiName} onsubmit={handleSubmit}>
<template for:each={formFields} for:item="formField">
<lightning-input-field key={formField.apiName} field-name={formField.apiName} value={formField.value}></lightning-input-field>
</template>
<lightning-button type="submit" label="Submit"></lightning-button>
</lightning-record-edit-form>
</div>
<div class="slds-col slds-size_1-of-1">
<h2 class="slds-text-heading_medium">Child Records to Clone</h2>
</div>
<div class="slds-col slds-size_1-of-1">
<lightning-combobox name="childRelationships" label="Child Relationships" placeholder="Select To Clone"
options={childRelationshipDisplayList} onchange={handleChildRelationshipSelection}></lightning-combobox>
</div>
<div class="slds-col slds-size_1-of-1">
<lightning-pill-container items={childRelationshipSelectionList} onitemremove={handleChildSelectionRemoval}></lightning-pill-container>
</div>
</div>
</lightning-card>
</template> </template>
@@ -1,3 +1,122 @@
import { LightningElement } from 'lwc'; import { LightningElement, api, wire, track } from 'lwc';
import { getObjectInfo } from 'lightning/uiObjectInfoApi';
import { getRecord } from 'lightning/uiRecordApi';
import OPPORTUNITY_OBJECT from '@salesforce/schema/Opportunity';
import ObtainCloneOpportunityFields from '@salesforce/apex/OpportunityCloneService.ObtainCloneOpportunityFields';
import ObtainChildRelationships from '@salesforce/apex/OpportunityCloneService.ObtainOpporunityChildRelationships';
import CloneOpportunityWithChildren from '@salesforce/apex/OpportunityCloneService.CloneOpportunityWithChildren';
export default class OpportunityCustomClone extends LightningElement {} export default class OpportunityCustomClone extends LightningElement {
@api recordId;
formObjectApiName;
displayedFields;
requiredFieldMap;
opportunityFields;
childRelationshipList;
recordInfo;
childRelationshipDisplayList = [];
@track childRelationshipSelectionList = [];
@wire(getRecord,{recordId:'$recordId',layoutTypes:['Full'],modes:['Edit']})
processRecordInfo({err, data}) {
if(err) {
window.console.error('There was a problem obtaining Opportunity Record Data');
window.console.error(err);
}
if(data) {
//window.console.log('Original Data: %s',JSON.stringify(data,null,"\t"));
this.recordInfo = data.fields;
}
}
@wire(ObtainChildRelationships)
processChildRelationships({error, data}) {
if(error) {
window.console.log('There is an error obtaining Cild Relationships');
window.console.error(error);
}
if(data) {
var childRelationshipPicklistArray = [];
Object.keys(data).forEach(childItem => {
childRelationshipPicklistArray.push({'label':data[childItem],'value':childItem});
})
this.childRelationshipDisplayList = childRelationshipPicklistArray;
}
}
@wire(ObtainCloneOpportunityFields,{fieldsetName:'ClonedFields'})
processFieldsetInfo({error, data}) {
if(error){
window.console.error('There is an error obtaining ClonedFields fieldset');
window.console.error(error);
}
if(data){
this.displayedFields = Object.keys(data);
this.requiredFieldMap = data;
}
}
@wire(getObjectInfo,{ objectApiName: OPPORTUNITY_OBJECT })
processOpportunityObjectInfo({error, data}) {
if(error){
window.console.error('There is an error obtaining fields');
}
if(data){
this.formObjectApiName = OPPORTUNITY_OBJECT.objectApiName;
this.opportunityFields = data.fields;
}
}
/**
* @description Handler that executes on selection of child Relationship
*/
handleChildRelationshipSelection(evt){
//find the item that was selected from the distplay list
var selectedItem = evt.target.value;
let fullSelectedList = this.childRelationshipSelectionList;
let displayList = this.childRelationshipDisplayList;
let chosenObj = displayList.find( element => element.value === selectedItem);
let alreadySelected = fullSelectedList.findIndex(element => element.value === selectedItem);
window.console.log('Already Selected Value: %s',alreadySelected);
if(chosenObj && alreadySelected < 0){
fullSelectedList.push(chosenObj);
}
this.childRelationshipSelectionList = fullSelectedList;
window.console.log('Full Selected List: %s',JSON.stringify(this.childRelationshipSelectionList,null,"\t"));
}
handleChildSelectionRemoval(evt){
window.console.log('Item Clicked: %s',evt.detail.index);
let fullSelectedList = this.childRelationshipSelectionList;
let removedItem = fullSelectedList.splice(evt.detail.index,1);
this.childRelationshipSelectionList = fullSelectedList;
}
/**
* @description This will override the submission
* @param {*} evt
*/
handleSubmit(evt){
evt.preventDefault();
var fields = evt.detail.fields;
window.console.log('Fields Submitted: %s',JSON.stringify(fields,null,"\t"));
}
get formFields(){
var formFieldsToReturn = [];
var fieldRequirementMap = this.requiredFieldMap;
if(this.opportunityFields && this.displayedFields && this.recordInfo) {
window.console.log('fieldInfo %s: ', JSON.stringify(this.recordInfo,null,"\t"));
this.displayedFields.forEach(fieldName => {
let fieldItem = fieldRequirementMap[fieldName];
let fieldDisplay = JSON.parse(JSON.stringify(this.opportunityFields[fieldName]));
if(!fieldItem){
fieldDisplay.value = this.recordInfo[fieldName].value;
}
formFieldsToReturn.push(fieldDisplay);
});
}
return formFieldsToReturn;
}
}
@@ -2,4 +2,12 @@
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata"> <LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>51.0</apiVersion> <apiVersion>51.0</apiVersion>
<isExposed>false</isExposed> <isExposed>false</isExposed>
<targets>
<target>lightning__RecordPage</target>
</targets>
<targetConfigs>
<targetConfig targets="lightning__RecordPage">
<property name="recordId" type="String" default="{!recordId]"></property>
</targetConfig>
</targetConfigs>
</LightningComponentBundle> </LightningComponentBundle>
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<ListView xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>All</fullName>
<columns>ACCOUNT.NAME</columns>
<columns>ACCOUNT.SITE</columns>
<columns>ACCOUNT.ADDRESS1_STATE</columns>
<columns>ACCOUNT.PHONE1</columns>
<columns>ACCOUNT.TYPE</columns>
<columns>CORE.USERS.ALIAS</columns>
<filterScope>Everything</filterScope>
<label>All</label>
</ListView>
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<QuickAction xmlns="http://soap.sforce.com/2006/04/metadata">
<description>Customized Cloning of Opportunity Record</description>
<height>400</height>
<label>SmartClone</label>
<lightningComponent>OppCustomCloneAura</lightningComponent>
<optionsCreateFeedItem>false</optionsCreateFeedItem>
<type>LightningComponent</type>
<width>-100</width>
</QuickAction>