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
GYAN ANDRUSGYAN ANDRUS 

Query Inside the for loop,how to rectify this issue without changing this logic,can anyone help .URGENT!!!!!!!!!!!!!!!!!!

 List <peer__Investment_Booking__c> TEMPInvestmentBookingList = new List<peer__Investment_Booking__c>();
            peer__Investment_Booking__c TEMPInvestmentBooking1;
            bookingOrderList = [SELECT Id, peer__Investor__c,Insured_Amount__c,Total_Insured_Amount__c FROM peer__Booking_Order__c WHERE peer__Investor__c =: account.Id];
              /*List<Id> bookingId = new List<Id>();
            for(peer__Booking_Order__c tempbookingOrder:bookingOrderList){
                 bookingId.add(tempbookingOrder.Id);               
            }
            investmentBookingList = [SELECT peer__Investment_Amount__c,Insured_Percentage__c,Insured_Amount__c,Premium_Component__c,peer__Loan_Application__c
                                         FROM peer__Investment_Booking__c WHERE peer__Booking_Order__c IN :bookingId];
            for(j=0;j<investmentBookingList.size();j++)
                {
                    investmentBooking = investmentBookingList.get(j);
                    TEMPInvestmentBookingList.add(investmentBooking);
                }*/
          for(i=0;i<bookingOrderList.size();i++)
            {
                bookingOrder = bookingOrderList.get(i);
                investmentBookingList = [SELECT peer__Investment_Amount__c,Insured_Percentage__c,Insured_Amount__c,Premium_Component__c,peer__Loan_Application__c
                                         FROM peer__Investment_Booking__c WHERE peer__Booking_Order__c =: bookingOrder.Id];
                for(j=0;j<investmentBookingList.size();j++)
                {
                    investmentBooking = investmentBookingList.get(j);
                    TEMPInvestmentBookingList.add(investmentBooking);
                }
            }
            for(i=0;i<TEMPInvestmentBookingList.size();i++)
            {
                investmentBooking = TEMPInvestmentBookingList.get(i);
                j=i+1;
                while(j<TEMPInvestmentBookingList.size())
                {
                    TEMPInvestmentBooking1 = TEMPInvestmentBookingList.get(j);
                    if(investmentBooking.peer__Loan_Application__c == TEMPInvestmentBooking1.peer__Loan_Application__c)
                    {
                        investmentBooking.peer__Investment_Amount__c = investmentBooking.peer__Investment_Amount__c + TEMPInvestmentBooking1.peer__Investment_Amount__c;
                        TEMPInvestmentBookingList.remove(j);
                    }
                    else{
                        j++;
                    }
                }
            }
Pankaj MehraPankaj Mehra
Hi Gyan,

I have udpated your code :
 
List <peer__Investment_Booking__c> TEMPInvestmentBookingList = new List<peer__Investment_Booking__c>();
            peer__Investment_Booking__c TEMPInvestmentBooking1;
            bookingOrderList = [SELECT Id, peer__Investor__c,Insured_Amount__c,Total_Insured_Amount__c FROM peer__Booking_Order__c WHERE peer__Investor__c =: account.Id];
              /*List<Id> bookingId = new List<Id>();
            for(peer__Booking_Order__c tempbookingOrder:bookingOrderList){
                 bookingId.add(tempbookingOrder.Id);               
            }
            investmentBookingList = [SELECT peer__Investment_Amount__c,Insured_Percentage__c,Insured_Amount__c,Premium_Component__c,peer__Loan_Application__c
                                         FROM peer__Investment_Booking__c WHERE peer__Booking_Order__c IN :bookingId];
            for(j=0;j<investmentBookingList.size();j++)
                {
                    investmentBooking = investmentBookingList.get(j);
                    TEMPInvestmentBookingList.add(investmentBooking);
                }*/

          Map<String,List<peer__Investment_Booking__c>> mapBookingOrder = new Map<String,List<peer__Investment_Booking__c>>();
          for(peer__Investment_Booking__c peer : [SELECT peer__Investment_Amount__c,Insured_Percentage__c,Insured_Amount__c,Premium_Component__c,peer__Loan_Application__c
                                         FROM peer__Investment_Booking__c WHERE peer__Booking_Order__c in: bookingOrderList]) {
            if(!mapBookingOrder.contains(peer.peer__Booking_Order__c)) {
                mapBookingOrder.put(peer.peer__Booking_Order__c,new List<peer__Investment_Booking__c>);
            }
            mapBookingOrder.get(peer.peer__Booking_Order__c).add(peer);
          }
                
          for(i=0;i<bookingOrderList.size();i++)
            {
                bookingOrder = bookingOrderList.get(i);
                investmentBookingList = mapBookingOrder.get(bookingOrder.Id);//[SELECT peer__Investment_Amount__c,Insured_Percentage__c,Insured_Amount__c,Premium_Component__c,peer__Loan_Application__c
                                        // FROM peer__Investment_Booking__c WHERE peer__Booking_Order__c =: bookingOrder.Id];
                for(j=0;j<investmentBookingList.size();j++)
                {
                    investmentBooking = investmentBookingList.get(j);
                    TEMPInvestmentBookingList.add(investmentBooking);
                }
            }
            for(i=0;i<TEMPInvestmentBookingList.size();i++)
            {
                investmentBooking = TEMPInvestmentBookingList.get(i);
                j=i+1;
                while(j<TEMPInvestmentBookingList.size())
                {
                    TEMPInvestmentBooking1 = TEMPInvestmentBookingList.get(j);
                    if(investmentBooking.peer__Loan_Application__c == TEMPInvestmentBooking1.peer__Loan_Application__c)
                    {
                        investmentBooking.peer__Investment_Amount__c = investmentBooking.peer__Investment_Amount__c + TEMPInvestmentBooking1.peer__Investment_Amount__c;
                        TEMPInvestmentBookingList.remove(j);
                    }
                    else{
                        j++;
                    }
                }
            }

 
GYAN ANDRUSGYAN ANDRUS



Hi pankaj.

Thanks for the Code,but got error while saving the class.please help
 UEXPECTED TOKEN IN BELOW CODE

            if(!mapBookingOrder.contains(peer.peer__Booking_Order__c)) 
            
            {
                mapBookingOrder.put(peer.peer__Booking_Order__c,new List<peer__Investment_Booking__c>);
            }
            mapBookingOrder.get(peer.peer__Booking_Order__c).add(peer);
          }

                

APEX CLASS:






List <peer__Investment_Booking__c> TEMPInvestmentBookingList = new List<peer__Investment_Booking__c>();
            peer__Investment_Booking__c TEMPInvestmentBooking1;
            bookingOrderList = [SELECT Id, peer__Investor__c,Insured_Amount__c,Total_Insured_Amount__c FROM peer__Booking_Order__c WHERE peer__Investor__c =: account.Id];
              /*List<Id> bookingId = new List<Id>();
            for(peer__Booking_Order__c tempbookingOrder:bookingOrderList){
                 bookingId.add(tempbookingOrder.Id);               
            }
            investmentBookingList = [SELECT peer__Investment_Amount__c,Insured_Percentage__c,Insured_Amount__c,Premium_Component__c,peer__Loan_Application__c
                                         FROM peer__Investment_Booking__c WHERE peer__Booking_Order__c IN :bookingId];
            for(j=0;j<investmentBookingList.size();j++)
                {
                    investmentBooking = investmentBookingList.get(j);
                    TEMPInvestmentBookingList.add(investmentBooking);
                }*/

          Map<String,List<peer__Investment_Booking__c>> mapBookingOrder = new Map<String,List<peer__Investment_Booking__c>>();
          for(peer__Investment_Booking__c peer : [SELECT peer__Investment_Amount__c,Insured_Percentage__c,Insured_Amount__c,Premium_Component__c,peer__Loan_Application__c
                                         FROM peer__Investment_Booking__c WHERE peer__Booking_Order__c in: bookingOrderList]) {
            if(!mapBookingOrder.contains(peer.peer__Booking_Order__c)) {
                mapBookingOrder.put(peer.peer__Booking_Order__c,new List<peer__Investment_Booking__c>);
            }
            mapBookingOrder.get(peer.peer__Booking_Order__c).add(peer);
          }
                
          for(i=0;i<bookingOrderList.size();i++)
            {
                bookingOrder = bookingOrderList.get(i);
                investmentBookingList = mapBookingOrder.get(bookingOrder.Id);//[SELECT peer__Investment_Amount__c,Insured_Percentage__c,Insured_Amount__c,Premium_Component__c,peer__Loan_Application__c
                                        // FROM peer__Investment_Booking__c WHERE peer__Booking_Order__c =: bookingOrder.Id];
                for(j=0;j<investmentBookingList.size();j++)
                {
                    investmentBooking = investmentBookingList.get(j);
                    TEMPInvestmentBookingList.add(investmentBooking);
                }
            }
            for(i=0;i<TEMPInvestmentBookingList.size();i++)
            {
                investmentBooking = TEMPInvestmentBookingList.get(i);
                j=i+1;
                while(j<TEMPInvestmentBookingList.size())
                {
                    TEMPInvestmentBooking1 = TEMPInvestmentBookingList.get(j);
                    if(investmentBooking.peer__Loan_Application__c == TEMPInvestmentBooking1.peer__Loan_Application__c)
                    {
                        investmentBooking.peer__Investment_Amount__c = investmentBooking.peer__Investment_Amount__c + TEMPInvestmentBooking1.peer__Investment_Amount__c;
                        TEMPInvestmentBookingList.remove(j);
                    }
                    else{
                        j++;
                    }
                }
                }
                
                
                  for(i=0; i<TEMPInvestmentBookingList.size(); i++)
            {
                investmentBooking = TEMPInvestmentBookingList.get(i);
                loanApplication = [SELECT Id, Borrower_Name__c,Loan_App_ID__c, Facility_Type__c, peer__Application_Date__c, peer__Requested_Loan_Amount__c,
                                   peer__Term__c, Genesis_Term__c, Monthly_Interest_Rate__c, Annualized_IRR__c,peer__Remaining_Requested_Amount__c,Percent_Funded__c, peer__Percent_Funded__c, 
                                   peer__Expiry_Date__c,Insurance_Percentage__c,peer__Stage__c, Status__c,Premieum_Component__c,Loan_Type__c, Sector__c,peer__Loan__r.loan__Loan_Status__c,
                                   peer__Loan__r.Updated_Loan_Status__c
                                   FROM peer__Loan_Application__c
                                   WHERE Id =: investmentBooking.peer__Loan_Application__c];
                
                investorParticipatedFacilities = new WrapperInvestorParticipatedFacilities();
                investorParticipatedFacilities.ApplicationID = loanApplication.Loan_App_ID__c;
                investorParticipatedFacilities.LoanType = loanApplication.Loan_Type__c;
                investorParticipatedFacilities.BorrowerName = loanApplication.Borrower_Name__c;
                investorParticipatedFacilities.CLContractLoanStatus= loanApplication.peer__Loan__r.loan__Loan_Status__c;   
                investorParticipatedFacilities.UpdatedLoanStatus = loanApplication.peer__Loan__r.Updated_Loan_Status__c;             
                //Format DatePosted
                Date tempDate = null;
                DateTime dt = null;
                tempDate = loanApplication.peer__Application_Date__c;
                if(tempDate != null)
                {
                    dt = DateTime.newInstance(tempDate.year(), tempDate.month(),tempDate.day());
                    String datePosted = dt.format('MMMM dd, yyyy');
                    investorParticipatedFacilities.DatePosted = datePosted;
                }
                investorParticipatedFacilities.LoanAmountRequested = loanApplication.peer__Requested_Loan_Amount__c;
                investorParticipatedFacilities.Tenure = loanApplication.Genesis_Term__c;
                investorParticipatedFacilities.InterestRatePM = loanApplication.Monthly_Interest_Rate__c;
                investorParticipatedFacilities.AnnualizedIRR = loanApplication.Annualized_IRR__c;
                
                if(loanApplication.peer__Stage__c != 'FUNDED')
                    investorParticipatedFacilities.Progress = loanApplication.peer__Percent_Funded__c;
                 else if (loanApplication.peer__Stage__c == 'FUNDED'|| loanApplication.peer__Stage__c == 'LIVE')
                     // || loanApplication.peer__Stage__c == 'LIVE'
                     investorParticipatedFacilities.Progress  = loanApplication.Percent_Funded__c;
                
                deadLineForFunding = (loanApplication.peer__Expiry_Date__c).format('MMMM dd, yyyy');
                investorParticipatedFacilities.DeadlineForFunding = deadLineForFunding;
                investorParticipatedFacilities.Status = loanApplication.Status__c;
                investorParticipatedFacilities.FacilityType = loanApplication.Facility_Type__c;
                investorParticipatedFacilities.LoanAmountInvested = investmentBooking.peer__Investment_Amount__c;
                investorParticipatedFacilities.Sector = loanApplication.Sector__c;
                investorParticipatedFacilities.RemainingRequestedAmount = loanApplication.peer__Remaining_Requested_Amount__c;
                investorParticipatedFacilities.PremiumComponent = loanApplication.Premieum_Component__c;
                investorParticipatedFacilities.InsuredPercentage =  loanApplication.Insurance_Percentage__c;
                investorParticipatedFacilitiesData.add(investorParticipatedFacilities);
            }
            //SENDING RESPONSE
            res.investorParticipatedFacilitiesData = investorParticipatedFacilitiesData;
                }
                }
Pankaj MehraPankaj Mehra
Hi Gyan,

I missed to add () after list initialisation 


            if(!mapBookingOrder.contains(peer.peer__Booking_Order__c)) 
            
            {
                mapBookingOrder.put(peer.peer__Booking_Order__c,new List<peer__Investment_Booking__c>()); // here is the bug
            }
            mapBookingOrder.get(peer.peer__Booking_Order__c).add(peer);
          }