• jackzhufengsfdc
  • NEWBIE
  • 30 Points
  • Member since 2015
  • Product Manager
  • Education First

  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
Hi guys ! i am going through every opportunity to copy records from another object  Here is my code 
List<OpportunityLineItem> result = new List<OpportunityLineItem>();
            List<PricebookEntry> pbe  = [SELECT Id, Product2Id, IsActive FROM PricebookEntry WHERE Product2Id IN :ProductIds];
               
                for (Opportunity op : opps){
                List<OpportunityLineItem> oli = [SELECT OpportunityId FROM OpportunityLineItem WHERE OpportunityId =: op.Id];
                   if(oli.size() == 0){
                     for(Sales_Order__c s :so){
                      	for(PricebookEntry pbee : pbe){
                     		for(SalesLine__c SL : productIdToSLineMap.get(pbee.Product2Id)){
                         		if(op.Id == s.Quote__c && SL.Sales_Order__c == s.Id){
                                    result.add (new OpportunityLineItem(
                                    OpportunityId = SL.Opportunity_Id__c,
                                    PricebookEntryId = pbee.Id,
                                    Quantity = SL.Quantity__c > 0 ? SL.Quantity__c : 1,
                                    UnitPrice =(SL.Quantity__c > 0 ? (SL.Amount__c > 0 ? SL.Amount__c/SL.Quantity__c : 0) : (SL.Amount__c > 0 ? SL.Amount__c : 0)))) ;
                          }
                        }
                      }
                   }
                   }else{
                       system.debug('OpportunityLineItem Exists');
                   }
             }
         insert result;

This gives too many soql error because i am checking if there are opportunitylineitems in opportunity in a loop is there a way to do this without querying in a loop  any help would be appreciated
Hi guys ! i am going through every opportunity to copy records from another object  Here is my code 
List<OpportunityLineItem> result = new List<OpportunityLineItem>();
            List<PricebookEntry> pbe  = [SELECT Id, Product2Id, IsActive FROM PricebookEntry WHERE Product2Id IN :ProductIds];
               
                for (Opportunity op : opps){
                List<OpportunityLineItem> oli = [SELECT OpportunityId FROM OpportunityLineItem WHERE OpportunityId =: op.Id];
                   if(oli.size() == 0){
                     for(Sales_Order__c s :so){
                      	for(PricebookEntry pbee : pbe){
                     		for(SalesLine__c SL : productIdToSLineMap.get(pbee.Product2Id)){
                         		if(op.Id == s.Quote__c && SL.Sales_Order__c == s.Id){
                                    result.add (new OpportunityLineItem(
                                    OpportunityId = SL.Opportunity_Id__c,
                                    PricebookEntryId = pbee.Id,
                                    Quantity = SL.Quantity__c > 0 ? SL.Quantity__c : 1,
                                    UnitPrice =(SL.Quantity__c > 0 ? (SL.Amount__c > 0 ? SL.Amount__c/SL.Quantity__c : 0) : (SL.Amount__c > 0 ? SL.Amount__c : 0)))) ;
                          }
                        }
                      }
                   }
                   }else{
                       system.debug('OpportunityLineItem Exists');
                   }
             }
         insert result;

This gives too many soql error because i am checking if there are opportunitylineitems in opportunity in a loop is there a way to do this without querying in a loop  any help would be appreciated