import { LightningElement, api, wire, track } from 'lwc'; import { getObjectInfo } from 'lightning/uiObjectInfoApi'; import { getRecord } from 'lightning/uiRecordApi'; import { NavigationMixin} from 'lightning/navigation'; 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 = []; @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; 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)=>{ window.console.log('Successful callout. %s',JSON.stringify(results,null,"\t")); this.sendToOpp(results); }) .catch((err)=>{ window.console.log('There was an error attempting to clone records.'); }); } 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) { 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; } }