+ Start a Discussion
Rst123Rst123 

ERROR : Too many SOQL queries: 101

HI,
I'm getting error"Too many SOQL queries: 101" in line User user1 = [Select id, Profile.Name from User where Id= :UserInfo.getUserId() ]; of code below :-
I have highlighted the line below.

//Method added for Sending AFD to Siebel 
  if(trigger.isBefore && trigger.isUpdate){
    Set<String> caOwnerId = new Set<String>();
    Map<Id,User> ownerRole = new Map<Id,User>();
    String s_profile = null; //Addition for Defect 13928
    for(Credit_Approval__c ca:trigger.new){
        caOwnerId.add(ca.OwnerId);
    }  
    if(caOwnerId.size()>0){
        ownerRole = new Map<Id,User>([select id,UserRole.Name from User where id = :caOwnerId]);   
    }
      
        User user1 = [Select id, Profile.Name from User where Id= :UserInfo.getUserId() ];
        s_profile = user1.Profile.Name;
     for(Credit_Approval__c ca:trigger.new){
          
          if(OwnerRole.get(ca.OwnerId)!=null && !'GE Integration User'.equalsIgnoreCase(s_profile)){
          //if(OwnerRole.get(ca.OwnerId)!=null{
                   if(OwnerRole.get(ca.OwnerId).UserRole!=null && OwnerRole.get(ca.OwnerId).UserRole.Name.contains('HFS')&& OwnerRole.get(ca.OwnerId).UserRole.Name.contains('Zone')){// HFS Project Change - Add null check
              ca.Flow_Struc__c = 'Flow';
            }
            if(OwnerRole.get(ca.OwnerId).UserRole!=null && OwnerRole.get(ca.OwnerId).UserRole.Name.contains('HFS')&& !OwnerRole.get(ca.OwnerId).UserRole.Name.contains('Zone')){// HFS Project Change - Add null check
              ca.Flow_Struc__c = 'Structured';
            }        
          }   
    } 
  }


}
Best Answer chosen by Rst123
Satyendra RawatSatyendra Rawat
Hi

Check all the trigger related to Credit_Approval__c object all event it may having any query in loop,

and also improve your code use minimum query in the same context it could helpful you,
ex: you write the two query insted of one query

for(Credit_Approval__c ca:trigger.new){
        caOwnerId.add(ca.OwnerId);
    } 

  caOwnerId.add(UserInfo.getUserId())

    if(caOwnerId.size()>0){
        ownerRole = new Map<Id,User>([select id,UserRole.Name,Profile.Name from User where id = :caOwnerId]);  
    }
     
   User user1 = ownerRole.get(UserInfo.getUserId() );

Thanks
Satya

All Answers

Satish_SFDCSatish_SFDC
The query is running in a loop. Which means the query is executed as many number of times as there are records in the trigger.new.

Ideally that should be placed out of the loop.
You may gather all the User ID's in a Set and then issue just one soql query to get all the data.


Regards,
Satish Kumar
Rst123Rst123
Can you please help me out on this to acheive this.

Thanks in Advance!!!
Satyendra RawatSatyendra Rawat
Hi

Check all the trigger related to Credit_Approval__c object all event it may having any query in loop,

and also improve your code use minimum query in the same context it could helpful you,
ex: you write the two query insted of one query

for(Credit_Approval__c ca:trigger.new){
        caOwnerId.add(ca.OwnerId);
    } 

  caOwnerId.add(UserInfo.getUserId())

    if(caOwnerId.size()>0){
        ownerRole = new Map<Id,User>([select id,UserRole.Name,Profile.Name from User where id = :caOwnerId]);  
    }
     
   User user1 = ownerRole.get(UserInfo.getUserId() );

Thanks
Satya

This was selected as the best answer
Rst123Rst123
Thanks Satish :)

I'm facing similar issue :-

Highlighted the line i'm getting Too many SOQL queries: 101

if(trigger.isAfter){        
         if(caids.size()>0){ 
            
                          Set<Id> RelCreditApprovalId= new Set<Id>();
              for(credit_approval__c ca:trigger.new){
               if(caids.contains(ca.id)){
                             
                  if(ca.Credit_Line_Id__c!=null){
                    system.debug('Credit Line Id (LOC): '+ca.Credit_Line_Id__c);
                    RelCreditApprovalId.add(ca.Credit_Line_Id__c);
               
                }
              }
             }
             
               List<Credit_Approval__c> RelCreditApprovalList= [select Id,Application_Type__c,Credit_Line_ID__c,Submittal_Status__c,Approved_Amount__c,Approved_Amount_With_Tolerance__c,LOC_Adjustment_Amount__c, Risk_Decision__c,Amount_Approved_Remaining__c,
                                    Credit_Line_ID__r.Submittal_Status__c,Credit_Line_ID__r.Approved_Amount__c,Credit_Line_ID__r.Approved_Amount_With_Tolerance__c,Credit_Line_ID__r.Risk_Decision__c,Opportunity__c
                                    from Credit_Approval__c where Id in :RelCreditApprovalId];
             

             
                    
             if(RelCreditApprovalList!=null && RelCreditApprovalList.size()>0){
              try{
                    update RelCreditApprovalList;
                 }catch(DMLException e){
                    system.debug('/nDML Exception : '+e.getmessage());
                 }
             }
        }      
     }