Files
smartclone/force-app/main/default/lwc/opportunityCustomClone/opportunityCustomClone.js
T

163 lines
6.3 KiB
JavaScript

import { LightningElement, api, wire, track } from 'lwc';
import { getObjectInfo } from 'lightning/uiObjectInfoApi';
import { getRecord } from 'lightning/uiRecordApi';
import { NavigationMixin} from 'lightning/navigation';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
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 NavigationMixin(LightningElement) {
@api recordId;
formObjectApiName;
displayedFields;
requiredFieldMap;
opportunityFields;
childRelationshipList;
recordInfo;
childRelationshipDisplayList = [];
toastAttributes = {
"success": {
"title":"Success!",
"message":"Your action was successful",
"variant":"success",
"mode":"dismissable"
},
"error": {
"title":"Error",
"message":"There was an error attempting to execute your request. {0}",
"messageData":[],
"variant":"error",
"mode":"pester"
}
};
@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) {
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;
}
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;
var childRelList = [];
this.childRelationshipSelectionList.forEach( selection => {
childRelList.push(selection.value);
});
window.console.log('Child Relationships Selected: %s',JSON.stringify(childRelList,null,"\t"));
CloneOpportunityWithChildren({'oppId':this.recordId,'newValues':fields,'childRelationshipsToClone':childRelList})
.then((results)=>{
this.sendToOpp(results);
})
.catch((err)=>{
let toastInstanceMessage = this.toastAttributes.error;
toastInstanceMessage.messageData.push(err.body.message);
const event = new ShowToastEvent(toastInstanceMessage);
this.dispatchEvent(event);
//window.console.log('There was an error attempting to clone records.');
//window.console.dir(err.body.message);
});
}
sendToOpp(oppId){
window.console.log('Send to Opp executed.');
this[NavigationMixin.Navigate]({
type:'standard__recordPage',
attributes:{
recordId:oppId,
objectApiName:OPPORTUNITY_OBJECT.objectApiName,
actionName:'view'
}
});
}
get formFields(){
var formFieldsToReturn = [];
var fieldRequirementMap = this.requiredFieldMap;
if(this.opportunityFields && this.displayedFields && this.recordInfo) {
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;
}
}