function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sophia GSophia G 

Help with changing APEX code for Lightning Component on Opportunity

Hi,
I am fairly new to coding and need some help manipulating my code so that the Synced Quote on the "Lead" Opportunity record (which is the record type that has the lightning component button), is cloned to the selected "Child" records (from the list I created) instead of duplicating the Quotes which are already on the selected "Child" Opportunity records.

Thank you!

APEX:
public class UpdateExistingPaxController {
    
  @AuraEnabled
    public static List <Opportunity> fetchOpportunity() {
        return [SELECT Id, Name, Trip_File_LEAD__c, SyncedQuoteId 
                FROM Opportunity Limit 30];
        
    }
    
    @AuraEnabled
    public static void updateRecord(List <String> lstRecordId) {
        List<Opportunity> lstUpdate = new List<Opportunity>();
        for(Opportunity Opp : [SELECT Id, Name, SyncedQuoteId, Trip_File_LEAD__c FROM Opportunity WHERE Id IN : lstRecordId]){
        lstUpdate.add(Opp);
        }
        
        if(lstUpdate.size() > 0){
            update lstUpdate;
        }

 //Clone Syncing Quote
            if(lstUpdate.size() >0)
            {
                                    
                List<Quote> lstQuoteResult = new List<Quote>();
                
                List<Quote> lstQuote = [SELECT Id, OwnerId, IsDeleted, Name, CurrencyIsoCode, CreatedDate, CreatedById, LastModifiedDate, 
                                        LastModifiedById, SystemModstamp, LastViewedDate, LastReferencedDate, OpportunityId, Pricebook2Id, 
                                        ContactId, QuoteNumber, IsSyncing, ShippingHandling, Tax, Status, ExpirationDate, Description, 
                                        Subtotal, TotalPrice, LineItemCount, BillingStreet, BillingCity, BillingState, BillingPostalCode, 
                                        BillingCountry, BillingLatitude, BillingLongitude, BillingGeocodeAccuracy, BillingAddress, 
                                        ShippingStreet, ShippingCity, ShippingState, ShippingPostalCode, ShippingCountry, ShippingLatitude, 
                                        ShippingLongitude, ShippingGeocodeAccuracy, ShippingAddress, QuoteToStreet, QuoteToCity, QuoteToState, 
                                        QuoteToPostalCode, QuoteToCountry, QuoteToLatitude, QuoteToLongitude, QuoteToGeocodeAccuracy, 
                                        QuoteToAddress, AdditionalStreet, AdditionalCity, AdditionalState, AdditionalPostalCode, 
                                        AdditionalCountry, AdditionalLatitude, AdditionalLongitude, AdditionalGeocodeAccuracy, 
                                        AdditionalAddress, BillingName, ShippingName, QuoteToName, AdditionalName, Email, Phone, 
                                        Fax, ContractId, AccountId, Discount, GrandTotal, CanCreateQuoteLineItems, Airline__c, 
                                        Milestone__c, Milest__c, Milestone_Deposit_Date__c, Final_Deposit_Balance_Due__c, 
                                        FInal_Deposit_Balance_Amount__c, Start_Date__c, Product_Interested_In__c, Check_In_Date__c, 
                                        Check_Out_Date__c, Old_Quote__c, Price_Book_Name__c, Sent_to_Customer_Date__c, Price_Per_Person__c, 
                                        Airfare_Required__c, Airfare_Amount__c, Insurance_Required__c, Insurance_Amount__c, Misc_Add_On__c, 
                                        Misc_Add_On_Price__c, Overall_Grand_Total__c, Discount_Applied__c, Per_Person__c, Quotation_Total__c, 
                                        Amount__c, Departing__c, Arrival__c, Departure_Date__c, Arrival_Date__c, Flight__c, Flight_Details__c, 
                                        Insurance_From__c, Insurance_To__c, Number_of_Nights__c, Misc_Add_On_Required__c, Passenger_Names__c, 
                                        Checkout_checkin__c, Final_Payment_Due_Date__c
                                        FROM Quote
                                        where OpportunityId = :lstRecordId];
                
                if(lstQuote.size() > 0){
                    for(integer x = 0; x < lstUpdate.size(); x++)
                    { 
                        for (Quote objQuote : lstQuote){
                            Quote objNewQuote = objQuote.clone(false,false,false,false);
                            objNewQuote.id = null;
                            //objNewQuote.Status ='Draft';
                            objNewQuote.OpportunityId = lstUpdate[x].Id;
                            objNewQuote.Old_Quote__c = objQuote.Id;
                            lstQuoteResult.add(objNewQuote);
                        }
                    }
                    insert lstQuoteResult;
                    
 //Clone Quote Item
                    List<QuoteLineItem> items = new List<QuoteLineItem>();
                    for (Quote objQuote : lstQuoteResult){
                        for (QuoteLineItem pi : [Select p.QuoteId, p.Product2Id, p.Subtotal, p.ListPrice, p.Quantity,
                                                 p.UnitPrice,p.PricebookEntryId,p.OpportunityLineItemId 
                                                 From QuoteLineItem p where QuoteId = :objQuote.Old_Quote__c]) {
                                                     QuoteLineItem newPI = pi.clone(false);
                                                     newPI.QuoteId = objQuote.id;
                                                     items.add(newPI);
                                                 }
                    }
                    insert items;
        }
        
    }
}
    }
Lightning Component:
<aura:component controller="UpdateExistingPaxController"
                implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
    
    <aura:attribute name="OpportunityList" type="List" />
       <aura:handler name="init" value="{!this}" action="{!c.loadOpportunities}"/>
    <aura:handler event="force:refreshView" action="{!c.loadOpportunities}" />
    
    <div class="slds-grid slds-grid--align-end"> 
        <button class="slds-button slds-button--brand" onclick="{!c.updateFields}">Update</button>
    </div>
    
    
    <table class="slds-table slds-table--bordered slds-table--cell-buffer">
        <thead>
            <tr class="slds-text-title--caps">
                <th style="width:3.25rem;" class="slds-text-align--right">
                    <div class="slds-form-element">
                        <div class="slds-form-element__control">
                            <label class="slds-checkbox">
                                <!--header checkbox for select all-->
                                <ui:inputCheckbox aura:id="box3" change="{!c.selectAll}"/>
                                <span class="slds-checkbox--faux"></span>
                                <span class="slds-form-element__label text"></span>
                            </label>
                        </div>
                    </div>
                </th>
                <th>
                    <span class="slds-truncate">Name</span>      
                </th>
                <th>
                    <span class="slds-truncate">Trip File Record</span>
                </th>
                <th>
                    <span class="slds-truncate">Synced Quote</span>
                </th>
                </tr>
        </thead>
        
        <tbody>
            <aura:iteration items="{!v.OpportunityList}" var="Opp">
                <tr>
                    <td scope="row" class="slds-text-align--right" style="width:3.25rem;">
                        <div class="slds-form-element">
                            <div class="slds-form-element__control">
                                <label class="slds-checkbox">
                                    <ui:inputCheckbox text="{!Opp.Id}" aura:id="boxPack" value=""/>
                                    <span class="slds-checkbox--faux"></span>
                                    <span class="slds-form-element__label text"></span>
                                </label>
                            </div>
                        </div>
                    </td>
                    <td scope="row">
                        <div class="slds-truncate" title="{!Opp.Name}"><a>{!Opp.Name}</a></div>
                    </td>
                    <td scope="row">
                        <div class="slds-truncate" title="{!Opp.Trip_File_LEAD__c}">{!Opp.Trip_File_LEAD__c}</div>
                    </td>
                    <td scope="row">
                        <div class="slds-truncate" title="{!Opp.SyncedQuoteId}">{!Opp.SyncedQuoteId}</div>
                    </td>
                      </tr>
            </aura:iteration>
        </tbody>
    </table>
</aura:component>
JS Controller:
({
    loadOpportunities: function(component, event, helper) {
        var action = component.get('c.fetchOpportunity');
              action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                component.set('v.OpportunityList', response.getReturnValue());
                component.find("box3").set("v.value", false);
            }
        });
        $A.enqueueAction(action);
    },
    
    selectAll: function(component, event, helper) {
        var selectedHeaderCheck = event.getSource().get("v.value");
        var getAllId = component.find("boxPack"); 
        if(! Array.isArray(getAllId)){
            if(selectedHeaderCheck == true){ 
                component.find("boxPack").set("v.value", true);    
            }else{
                component.find("boxPack").set("v.value", false);
            }
        }else{
          
            if (selectedHeaderCheck == true) {
                for (var i = 0; i < getAllId.length; i++) {
                    component.find("boxPack")[i].set("v.value", true);
                }
            } else {
                for (var i = 0; i < getAllId.length; i++) {
                    component.find("boxPack")[i].set("v.value", false);
                }
            } 
        }  
    },
    
    updateFields: function(component, event, helper) {
        var updateId = [];
        var getAllId = component.find("boxPack");
        
        if(! Array.isArray(getAllId)){
            if (getAllId.get("v.value") == true) {
                updateId.push(getAllId.get("v.text"));
            }
        }else{
            
            for (var i = 0; i < getAllId.length; i++) {
                if (getAllId[i].get("v.value") == true) {
                    updateId.push(getAllId[i].get("v.text"));
                }
            }
        } 
        
        var action = component.get('c.updateRecord');
        action.setParams({
            "lstRecordId": updateId
        });
        action.setCallback(this, function(response) {
            
            var state = response.getState();
            if (state === "SUCCESS") {
                console.log(state);
                $A.get('e.force:refreshView').fire();
                $A.get("e.force:closeQuickAction").fire()
            }
        });
        $A.enqueueAction(action);
    },
    
})
Aura Application:
<aura:application extends="force:slds">
    <c:UpdateExistingPAX/>
</aura:application>