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
anvesh@force.comanvesh@force.com 

Getting System.limit exception 101 too many SOQL Querries ?

HI please can anyone  resolve below code. Why i am getting System.Limit Exception. I am unable to find out the reason.

LA Detail ,LA Summery 2 objects...while i am inserting/Updating Bulk records getting this error. Its working while manually inserting 1 record.

trigger LAdetail on L_A_Detail__c (before insert,before update) {

set<string> Tset = new set<string>();
set<string> Cset = new set<string>();

set<string> Lset = new set<string>();

List<L_A_Detail__c> Dlist = new List<L_A_Detail__c>();

Map<string,string> LDetailMap1 = new Map<string,string>();
Map<string,string> LSumeryMap2 = new Map<string,string>();

for(L_A_Detail__c La : Trigger.new){
Tset.add(La.SE2_ID__c);
Cset.add(La.Appointment_State__c);
Lset.add(La.License_Number__c);
LDetailMap1.put(La.SE2_ID__c+La.Appointment_State__c,La.SE2_ID__c+La.Appointment_State__c);
}

List<L_A_Summary__c> LAlist = [select id,License_Number__c,SE2_ID__c,Appointment_State__c from L_A_Summary__c  where SE2_ID__c in:Tset And Appointment_State__c in:Cset And License_Number__c in:Lset];

for(L_A_Summary__c Al : LAlist){
LSumeryMap2.put(Al.SE2_ID__c+Al.Appointment_State__c,Al.SE2_ID__c+Al.Appointment_State__c);
}

for(L_A_Detail__c D : Trigger.new){
for(L_A_Summary__c S : LAlist){
string s1 = LDetailMap1.get(D.SE2_ID__c+D.Appointment_State__c);
string s2 = LSumeryMap2.get(S.SE2_ID__c+S.Appointment_State__c);

if(s1==s2){
if(D.Line_of_Business__c == 'Health'){
S.Health__c = True;
}

if(D.Line_of_Business__c == 'Life'){
S.Life__c = True;
}

if(D.Line_of_Business__c == 'Variable'){
S.Variable__c = True;
}

if(D.Line_of_Business__c == 'Annuity'){
S.Annuity__c = True;
}

if(D.Line_of_Business__c == 'Casualty'){
S.Casualty__c = True;
}

if(D.Line_of_Business__c == 'DI'){
S.DI__c = True;
}

if(D.Line_of_Business__c == 'Fixed'){
S.Fixed__c = True;
}

if(D.Line_of_Business__c == 'Long Term Care'){
S.Long_Term_Care__c = True;
}

if(D.Line_of_Business__c == 'Property'){
S.Property__c = True;
}

if(D.Line_of_Business__c == 'Variable Life'){
S.Variable_Life__c = True;
}

}
try{
update LAlist;
}
catch(DMlException e){
System.debug(e);
}
}


}
}
anvesh@force.comanvesh@force.com
also getting DMlexception Too many DML Statement 150  exception ?
Art SmorodinArt Smorodin

Hi, 

it could be due to the fact that your Update is inside the for loop, which itself is inside the foor loop. 
Try moving the update outside one, or both of them. 
 

trigger LAdetail on L_A_Detail__c (before insert,before update) {

set<string> Tset = new set<string>();
set<string> Cset = new set<string>();

set<string> Lset = new set<string>();

List<L_A_Detail__c> Dlist = new List<L_A_Detail__c>();

Map<string,string> LDetailMap1 = new Map<string,string>();
Map<string,string> LSumeryMap2 = new Map<string,string>();

for(L_A_Detail__c La : Trigger.new){
Tset.add(La.SE2_ID__c);
Cset.add(La.Appointment_State__c);
Lset.add(La.License_Number__c);
LDetailMap1.put(La.SE2_ID__c+La.Appointment_State__c,La.SE2_ID__c+La.Appointment_State__c);
}

List<L_A_Summary__c> LAlist = [select id,License_Number__c,SE2_ID__c,Appointment_State__c from L_A_Summary__c  where SE2_ID__c in:Tset And Appointment_State__c in:Cset And License_Number__c in:Lset];

for(L_A_Summary__c Al : LAlist){
LSumeryMap2.put(Al.SE2_ID__c+Al.Appointment_State__c,Al.SE2_ID__c+Al.Appointment_State__c);
}

for(L_A_Detail__c D : Trigger.new){
for(L_A_Summary__c S : LAlist){
string s1 = LDetailMap1.get(D.SE2_ID__c+D.Appointment_State__c);
string s2 = LSumeryMap2.get(S.SE2_ID__c+S.Appointment_State__c);

if(s1==s2){
if(D.Line_of_Business__c == 'Health'){
S.Health__c = True;
}

if(D.Line_of_Business__c == 'Life'){
S.Life__c = True;
}

if(D.Line_of_Business__c == 'Variable'){
S.Variable__c = True;
}

if(D.Line_of_Business__c == 'Annuity'){
S.Annuity__c = True;
}

if(D.Line_of_Business__c == 'Casualty'){
S.Casualty__c = True;
}

if(D.Line_of_Business__c == 'DI'){
S.DI__c = True;
}

if(D.Line_of_Business__c == 'Fixed'){
S.Fixed__c = True;
}

if(D.Line_of_Business__c == 'Long Term Care'){
S.Long_Term_Care__c = True;
}

if(D.Line_of_Business__c == 'Property'){
S.Property__c = True;
}

if(D.Line_of_Business__c == 'Variable Life'){
S.Variable_Life__c = True;
}

}

}//for(L_A_Summary__c S : LAlist)
try{
update LAlist;
}
catch(DMlException e){
System.debug(e);
}

}//for(L_A_Detail__c D : Trigger.new)
}