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 a line on a list class in apex code

Hi,
I am pretty new to coding and just need help on adding/changing the following line in my apex code so it queries the RecordTypeId as well as the OpportunityId from the record that my lightning component button has been actioned from. This RecordType which has the lightning component button is called 'Master PAX' and the RecordTypeId is 0121U000000VqrB. At the moment I have just manually added in the OpportunityId of my Master PAX and all the code is working great, I just need it to populate the OpportunityId and RecordTypeId automatically.
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 = :'0065500000HMAr5AAH'];


I have been stuck on this for a while and help would be much appreciated.
ravi soniravi soni

Hi Sophia,
you should replace the Custom ID with opportunityId variable. If you don't understand then  show me your whole apex class or Lightning component.

I will try to fix  your issue.
Thank you

 

Sophia GSophia G
Hi Veer,

Thank you for your replying. I am still having trouble with this - could you please give me a hand. Below is my Component, Controller, and Apex. Many thanks!

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>

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");
        var recordId = component.get('v.recordId')
        
        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);
    },
    
})
Apex:
public with sharing class UpdateExistingPaxController {

  @AuraEnabled
    public static List <Opportunity> fetchOpportunity() {

        return [SELECT Id, Name, Trip_File_LEAD__c, SyncedQuoteId, RecordTypeId
                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, RecordTypeId, Trip_File_LEAD__c FROM Opportunity WHERE Id IN : lstRecordId]){
        lstUpdate.add(Opp);
        }
        
        if(lstUpdate.size() > 0){
            update lstUpdate;
        }
        

            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 = : '0065500000HYmWlAAL'];
                

                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.OpportunityId = lstUpdate[x].Id; 
                            objNewQuote.Old_Quote__c = objQuote.Id;
                            lstQuoteResult.add(objNewQuote);
                        }
                    }
                    insert lstQuoteResult;
                    
 
                    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;
        }
        
    }
}
    }

Application:
<aura:application extends="force:slds">
    <c:UpdateExistingPAX/>
</aura:application>