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
Vasu@blrVasu@blr 

System.LimitException: Too many SOQL queries: 101"

I am getting below error:

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

 

How can I optimize my code to avoid this exception?

Please, help me.

trigger My_AddCommissions_US  on Account (after insert,after update) {

    List<Commission__c> insertCommission=new List<Commission__c>();
    List<Commission__c> updateCommission=new List<Commission__c>();
    set<ID> setupdate=new Set<ID>();
    map<Id,ID> accownermap=new map<id,id>();
    List<Commission__c> lstcom=new List<Commission__c>();
    set<Id> Ownerset=new Set<Id>();   
    ID role;
    map<Id,User> usermap=new Map<Id,User>([select id ,Userroleid,ProfileId from user where IsActive=true]);        
    //Start
   
    //End
    Userrole adminrole1=[select id from Userrole where Name='System Administrator'];
    //system.debug('OOOO'+adminrole1.id);
    Userrole chinarole=[select id from Userrole where Name='China Users'];
    Userrole ITDEVrole=[select id from Userrole where Name='IT DEV'];
    Userrole ProMktgadmin=[select id from Userrole where Name='Pro Mktg Administrator'];
    Userrole ProMktguser=[select id from Userrole where Name='Pro Marketing User'];
    Userrole ProSalesSVP=[select id from Userrole where Name='Pro Sales SVP'];
    Userrole RPSM=[select id from Userrole where Name='RPSM'];
    Userrole MyProRep=[select id from Userrole where Name='My Pro Rep'];

    if(trigger.IsInsert){
        for(Account acc:trigger.new){
            role=usermap.get(acc.OwnerId).Userroleid;
            if(role!=RPSM.id && role!=MyProRep.id && role!=adminrole1.id && role!=chinarole.id && role!=ITDEVrole.id && role!=ProMktgadmin.id && role!=ProMktguser.id && role!=null){
                Commission__c com=new Commission__c();
                com.Account__c=acc.id;               
                com.Sales_Team_Member__c=acc.OwnerId;               
                com.Percent_of_Sale__c=100;
                insertCommission.add(com);
            }
        }
        insert insertCommission;
    }
    if(Trigger.Isupdate){
        for(Account acc:trigger.new){
            if(trigger.newmap.get(acc.id).Ownerid!=trigger.oldmap.get(acc.id).Ownerid){
                setupdate.add(acc.id);
                role=usermap.get(acc.OwnerId).Userroleid;
                if(role!=RPSM.id && role!=MyProRep.id && role!=adminrole1.id && role!=chinarole.id && role!=ITDEVrole.id && role!=ProMktgadmin.id && role!=ProMktguser.id && role!=null){
                    accownermap.put(acc.id,acc.OwnerId);
                    Ownerset.add(trigger.oldmap.get(acc.id).Ownerid);
                }
            }
        }
        lstcom=[select  id,Sales_Team_Member__c,Account__c from Commission__c where Account__c in :setupdate and Sales_Team_Member__c=:Ownerset];          
          
        for(Commission__c c:lstcom){
            c.Sales_Team_Member__c=accownermap.get(c.Account__c);          
            updateCommission.add(c);          
        }
        try{
            update  updateCommission;
        }
        catch(Exception e){
            system.debug('Update failed'+e.getmessage());
        }
    }
}

 

Best Answer chosen by Admin (Salesforce Developers) 
jd123jd123

Hi 

 

  try this code

 

trigger My_AddCommissions_US on Account (after insert,after update)
{
List<Commission__c> insertCommission=new List<Commission__c>();
List<Commission__c> updateCommission=new List<Commission__c>();
set<ID> setupdate=new Set<ID>();
map<Id,ID> accownermap=new map<id,id>();
List<Commission__c> lstcom=new List<Commission__c>();
set<Id> Ownerset=new Set<Id>();
ID role;
map<Id,User> usermap=new Map<Id,User>([select id ,Userroleid,ProfileId from user where IsActive=true]);
//Start

//End
String adminrole1;
String chinarole;
String ITDEVrole;
String ProMktgadmin;
String ProMktguser;
String ProSalesSVP;
String RPSM;
String MyProRep;

List<Userrole> listOfRoles=[select id,Name from Userrole];
for(Userrole ur:listOfRoles)
{
if(ur.Name='System Administrator')
adminrole1=ur.id;
else if(ur.Name=China Users')
chinarole=ur.id;
else if(ur.Name='IT DEV')
ITDEVrole=ur.id;
else if(ur.Name='Pro Mktg Administrator')
ProMktgadmin=ur.id;
else if(ur.Name='Pro Marketing User')
ProMktguser=ur.id;
else if(ur.Name='Pro Sales SVP')
ProSalesSVP=ur.id;
else if(ur.Name='RPSM')
RPSM=ur.id;
else if(ur.Name='My Pro Rep')
MyProRep=ur.id;
}

if(trigger.IsInsert){
for(Account acc:trigger.new){
role=usermap.get(acc.OwnerId).Userroleid;
if(role!=RPSM&& role!=MyProRep && role!=adminrole1&& role!=chinarole&& role!=ITDEVrole && role!=ProMktgadmin && role!=ProMktguser && role!=null){
Commission__c com=new Commission__c();
com.Account__c=acc.id;
com.Sales_Team_Member__c=acc.OwnerId;
com.Percent_of_Sale__c=100;
insertCommission.add(com);
}
}
insert insertCommission;
}
if(Trigger.Isupdate){
for(Account acc:trigger.new){
if(trigger.newmap.get(acc.id).Ownerid!=trigger.oldmap.get(acc.id).Ownerid){
setupdate.add(acc.id);
role=usermap.get(acc.OwnerId).Userroleid;
if(role!=RPSM && role!=MyProRep && role!=adminrole1 && role!=chinarole && role!=ITDEVrole && role!=ProMktgadmin && role!=ProMktguser && role!=null){
accownermap.put(acc.id,acc.OwnerId);
Ownerset.add(trigger.oldmap.get(acc.id).Ownerid);
}
}
}
lstcom=[select id,Sales_Team_Member__c,Account__c from Commission__c where Account__c in :setupdate and Sales_Team_Member__c=:Ownerset];

for(Commission__c c:lstcom){
c.Sales_Team_Member__c=accownermap.get(c.Account__c);
updateCommission.add(c);
}
try{
update updateCommission;
}
catch(Exception e){
system.debug('Update failed'+e.getmessage());
}
}
}

 

 if it is not working please let me know.

All Answers

jd123jd123

Hi 

 

  try this code

 

trigger My_AddCommissions_US on Account (after insert,after update)
{
List<Commission__c> insertCommission=new List<Commission__c>();
List<Commission__c> updateCommission=new List<Commission__c>();
set<ID> setupdate=new Set<ID>();
map<Id,ID> accownermap=new map<id,id>();
List<Commission__c> lstcom=new List<Commission__c>();
set<Id> Ownerset=new Set<Id>();
ID role;
map<Id,User> usermap=new Map<Id,User>([select id ,Userroleid,ProfileId from user where IsActive=true]);
//Start

//End
String adminrole1;
String chinarole;
String ITDEVrole;
String ProMktgadmin;
String ProMktguser;
String ProSalesSVP;
String RPSM;
String MyProRep;

List<Userrole> listOfRoles=[select id,Name from Userrole];
for(Userrole ur:listOfRoles)
{
if(ur.Name='System Administrator')
adminrole1=ur.id;
else if(ur.Name=China Users')
chinarole=ur.id;
else if(ur.Name='IT DEV')
ITDEVrole=ur.id;
else if(ur.Name='Pro Mktg Administrator')
ProMktgadmin=ur.id;
else if(ur.Name='Pro Marketing User')
ProMktguser=ur.id;
else if(ur.Name='Pro Sales SVP')
ProSalesSVP=ur.id;
else if(ur.Name='RPSM')
RPSM=ur.id;
else if(ur.Name='My Pro Rep')
MyProRep=ur.id;
}

if(trigger.IsInsert){
for(Account acc:trigger.new){
role=usermap.get(acc.OwnerId).Userroleid;
if(role!=RPSM&& role!=MyProRep && role!=adminrole1&& role!=chinarole&& role!=ITDEVrole && role!=ProMktgadmin && role!=ProMktguser && role!=null){
Commission__c com=new Commission__c();
com.Account__c=acc.id;
com.Sales_Team_Member__c=acc.OwnerId;
com.Percent_of_Sale__c=100;
insertCommission.add(com);
}
}
insert insertCommission;
}
if(Trigger.Isupdate){
for(Account acc:trigger.new){
if(trigger.newmap.get(acc.id).Ownerid!=trigger.oldmap.get(acc.id).Ownerid){
setupdate.add(acc.id);
role=usermap.get(acc.OwnerId).Userroleid;
if(role!=RPSM && role!=MyProRep && role!=adminrole1 && role!=chinarole && role!=ITDEVrole && role!=ProMktgadmin && role!=ProMktguser && role!=null){
accownermap.put(acc.id,acc.OwnerId);
Ownerset.add(trigger.oldmap.get(acc.id).Ownerid);
}
}
}
lstcom=[select id,Sales_Team_Member__c,Account__c from Commission__c where Account__c in :setupdate and Sales_Team_Member__c=:Ownerset];

for(Commission__c c:lstcom){
c.Sales_Team_Member__c=accownermap.get(c.Account__c);
updateCommission.add(c);
}
try{
update updateCommission;
}
catch(Exception e){
system.debug('Update failed'+e.getmessage());
}
}
}

 

 if it is not working please let me know.

This was selected as the best answer
MagulanDuraipandianMagulanDuraipandian

'usermap'  is retrieving more than 100 records....

 

go for wrapper class...

 

http://infallibletechie.blogspot.in/2012/10/collection-size-maximum-size-of-1000.html

 

Regards,

Magulan D

Salesforce.com certified Force.com Developer.

SFDC Blog

If this post is your solution, kindly mark this as the solution.

Vasu@blrVasu@blr

Thank you very much...