+ Start a Discussion
yarramyarram 

Urgent:: System.LimitException: Too many SOQL queries: 101

Hi All,

i am getting too many soql limt exceed error when my below class executed. Please help me on this. This is very urgent.

public class ProjectUtil{  
   
    public static void beUpdate(List<OpportunityLineItem > listOLIList){
        Project_Assay__c PrjAssay;
for (OpportunityLineItem OppLI: listOLIList) {
            
  PrjAssay=[select id,name,Assay_Number__c,Sales_Price__c, Study_Number__c,OppAssay__c,
                                    from Project_Assay__c where OppAssay__c=:OppLI.OppAssay__c];                   
                           
                            PrjAssay.Assay_Number__c=OppLI.ProductCode;
                            PrjAssay.Quantity__c=OppLI.Quantity;
                            PrjAssay.Study_Number__c=OppLI.Study_Number__c;
                            PrjAssay.OppAssay__c=OppLI.OppAssay__c;
                            PrjAssay.Sales_Price__c=OppLI.UnitPrice;
                            
        }
         update PrjAssay;
    
    }
    
}

Thanks,
Yarram
Sagar PareekSagar Pareek
You have placed query inside for loop that is why you are getting this exception.
yarramyarram
HI Sagar, yes, SOQL is inside the for loop, i have tried to put out side but i am not success. how can i implement this query out side of the FOR loop. please help me on this How can i over come this.

Thanks,
yarram.
Waqar Hussain SFWaqar Hussain SF
Hello Yarram..
Try this code it must solve your problem
public class ProjectUtil{  
   
    public static void beUpdate(List<OpportunityLineItem > listOLIList){
        List<Project_Assay__c> PrjAssay = new List<Project_Assay__c>();;
		set<data type OppAssay__c> OppSet= new set<data type OppAssay__c>();
		for (OpportunityLineItem OpL: listOLIList) {
			OppSet.add(OpL.OppAssay__c);
		}

		List<Project_Assay__c> prjList = [select id,name,Assay_Number__c,Sales_Price__c, Study_Number__c,OppAssay__c,
                                    from Project_Assay__c where OppAssay__c IN :OppSet];
		
		for (OpportunityLineItem OppLI: prjList) {
            
                           PrjAssay.add(new Project_Assay__c(
							    Assay_Number__c=OppLI.ProductCode;
								Quantity__c=OppLI.Quantity;
								Study_Number__c=OppLI.Study_Number__c;
								OppAssay__c=OppLI.OppAssay__c;
								Sales_Price__c=OppLI.UnitPrice;
						   ));
                           
        }
		If (PrjAssay.size() >0){
			update PrjAssay;
		}
    

	}
}

pls let me know if it works.
yarramyarram
Hi Vickey,

Thanks for your reply, 

is Second FOR loop correct? you are assigning the ProjectAssay list to Opp.Lineitem object.
please give me the some clarity about this.

Thanks,
yarram.
Waqar Hussain SFWaqar Hussain SF
Oh yeah yarram you are right.. Try the this  code
public class ProjectUtil{  
   
    public static void beUpdate(List<OpportunityLineItem > listOLIList){
	
        List<Project_Assay__c> PrjAssay = new List<Project_Assay__c>();;
		set<data type OppAssay__c> OppSet= new set<data type OppAssay__c>();
		for (OpportunityLineItem OpL: listOLIList) {
			OppSet.add(OpL.OppAssay__c);
		}

		PrjAssay = [select id,name,Assay_Number__c,Sales_Price__c, Study_Number__c,OppAssay__c,
                                    from Project_Assay__c where OppAssay__c IN :OppSet];
		
		for (OpportunityLineItem OppLI: listOLIList) {
								if(OppLI.OppAssay__c ! null){
									PrjAssay.Assay_Number__c=OppLI.ProductCode;
									PrjAssay.Quantity__c=OppLI.Quantity;
									PrjAssay.Study_Number__c=OppLI.Study_Number__c;
									PrjAssay.OppAssay__c=OppLI.OppAssay__c;
									PrjAssay.Sales_Price__c=OppLI.UnitPrice;
								update PrjAssay;
						   }
                           
        }
				
	}
}

 
Chidambar ReddyChidambar Reddy

public class ProjectUtil{  
   
    public static void beUpdate(List<OpportunityLineItem > listOLIList){
        List<Project_Assay__c> PrjAssay = new List<Project_Assay__c>();;
		set<data type OppAssay__c> OppSet= new set<data type OppAssay__c>();
		for (OpportunityLineItem OpL: listOLIList) {
			OppSet.add(OpL.OppAssay__c);
		}

		List<Project_Assay__c> prjList = [select id,name,Assay_Number__c,Sales_Price__c, Study_Number__c,OppAssay__c, from Project_Assay__c where OppAssay__c IN :OppSet];
		Map<Id,Project_Assay__c> OppAssayToProjectAssayMap = new Map<Id,Project_Assay__c>();
                for(Project_Assay__c pa: prjList)
                           OppAssayToProjectAssayMap.put(pa.OppAssay__c, pa);

        
Map<Id,Project_Assay__c> projectAssayUpdateMap = new Map<Id,Project_Assay__c>();
 

		for (OpportunityLineItem OppLI:  listOLIList)) {
            if(OppAssayToProjectAssayMap.containsKey(OpL.OppAssay__c)){
                            Project_Assay__c prjAssay = OppAssayToProjectAssayMap.get(OpL.OppAssay__c);

                            PrjAssay.Assay_Number__c=OppLI.ProductCode;
                            PrjAssay.Quantity__c=OppLI.Quantity;
                            PrjAssay.Study_Number__c=OppLI.Study_Number__c;
                            PrjAssay.OppAssay__c=OppLI.OppAssay__c;
                            PrjAssay.Sales_Price__c=OppLI.UnitPrice;

                            projectAssayUpdateMap.put(prjAssay.Id, PrjAssay);

            }
                          
                           
        }
		If (OppAssayToProjectAssayMap.size() >0){
			update projectAssayUpdateMap.Values();
		}
    

	}
}
Try this