+ Start a Discussion
t.deepthi05t.deepthi05 

Failure Message: "System.LimitException: Too many SOQL queries: 101", Failure Stack Trace:

I am deploying my code to production and i am getting the below error

My trigger

trigger OpportunityTrigger on Opportunity (after delete, after insert, after undelete,
after update, before delete, before insert, before update) {

    if(Trigger.isAfter && Trigger.isInsert){
        {
        EncryptURl.insertOpp(Trigger.new,Trigger.newMap.keySet());
        }
}

and my trigger class is

public static void insertOpp(Opportunity[] o,Set<id> newMapKeySet)
{

    list<Opportunity> oppsToUpdate =[select id,Encrypted_URL__c from Opportunity where id  IN :newMapKeySet];
    for(Opportunity op:oppsToUpdate){
        if(op.id != null){
            string oppid=op.id;
            Blob dataLead = Blob.valueOf(oppid);
            String b64Data = EncodingUtil.base64Encode(dataLead);
            string oppURL =Label.PWV_FavoriteURL+'oId='+b64Data;
            op.Encrypted_URL__c =oppURL;
        }  
   }
    try{
    update oppsToUpdate;
    }catch(exception e){
            system.debug('DMl exception'+e);
     }

}

can i know how to over the soql exception
pradeep naredlapradeep naredla
HI deepthi,

"Too many SOQL queries: 101".

This error System.LimitException: Too many SOQL queries: 101is due to the fact, you are hitting on governor limit. The governor limit that says we can run total 100 SOQL queries in a context and you are hitting the limit.

All the triggers fired will be counted in a single context or call. We need to ensure that total number of SOQL fired should be less than 100.
In order to by pass this, you need to change your code in such a way that SOQL fired must be less than 100 or if you need to change the context then you can use @future annotation which will run the code asynchronously.

You need to make sure that the SOQL query you are using should not be inside the for loop.

There are certain best practices which you would have to follow to avoid this error (to avoid hitting governor limit).

1. http://wiki.developerforce.com/page/Best_Practice:_Avoid_SOQL_Queries_Inside_FOR_Loops

2. http://salesforcedeveloperblog.blogspot.com/2011/05/best-practices-of-triggers.html

3. http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_bestpract.htm

4. http://wiki.developerforce.com/page/Apex_Code_Best_Practices

If you follow the above practices, the error will stop. Moreover there is no way wherein we in Salesforce can increase the
governor limit or can stop it so best practices need to be followed.
t.deepthi05t.deepthi05
I wrote code checking the best practice only but still i am getting the error

https://developer.salesforce.com/page/Best_Practice:_Avoid_SOQL_Queries_Inside_FOR_Loops
Pavan Kumar KajaPavan Kumar Kaja
Hi,

Try removing after insert, after update  and after delete events so that u dont require to soql query to get opp records.

trigger OpportunityTrigger on Opportunity (after undelete, before delete, before insert, before update) {
    if(Trigger.isBefore){
		if(Trigger.isInsert || Trigger.isUpdate){
			EncryptURl.insertOpp(Trigger.new);
		}
	}
}


	// Class

public static void insertOpp(List<Opportunity> lstOpp){
  
	for(Opportunity op:lstOpp){
		if(op.id != null){
            string oppid=op.id;
            Blob dataLead = Blob.valueOf(oppid);
            String b64Data = EncodingUtil.base64Encode(dataLead);
            string oppURL =Label.PWV_FavoriteURL+'oId='+b64Data;
            op.Encrypted_URL__c =oppURL;
        }  
   }
}

Let me know its woring or not.

t.deepthi05t.deepthi05
Before insert it is not working because i am getting the record as null

Pavan Kumar KajaPavan Kumar Kaja

k Then Try using after events, can u tel me what ur trying to do with this.

trigger OpportunityTrigger on Opportunity (after undelete, before delete, after insert, after update) {
    if(Trigger.isAfter){
		if((Trigger.isInsert || Trigger.isUpdate) && EncryptURl.isRecursive == false ){
			EncryptURl.insertOpp(Trigger.new);
		}
	}
}


	

public static void insertOpp(List<Opportunity> lstOpp){
	List<Opportunity> lstOpptoUpdate = new List<Opportunity>();
	Public static Boolean isRecursive = false;
	
	for(Opportunity op:lstOpp){
		if(op.id != null){
            string oppid=op.id;
            Blob dataLead = Blob.valueOf(oppid);
            String b64Data = EncodingUtil.base64Encode(dataLead);
            string oppURL =Label.PWV_FavoriteURL+'oId='+b64Data;
            op.Encrypted_URL__c =oppURL;
			lstOpptoUpdate.add(op);
        }  
   }
	if(!lstOpptoUpdate.isEmpty()){
		isRecursive = true;
		update lstOpptoUpdate ;
   }
}


Pavan Kumar KajaPavan Kumar Kaja
Ping me if u r still having issue.


pavanthetech@gmail.com
t.deepthi05t.deepthi05
i am encryppting the id and putting the value into Encrypted_URL__c, I need to be done after trigger because i need id of opportunity to encrypt it
Pavan Kumar KajaPavan Kumar Kaja
ok try the above after trigger events code and let me know its working or not.
t.deepthi05t.deepthi05
We cannot edit the same object after insert i I will be getting the follwoing "error read only" that is the reason i am query and updating the value

I implemented the recurssive logic but still facing the same error

Chidambar ReddyChidambar Reddy
Hi Deepthi, You mentioned that you are getting error while deploying to production

Is your test class passed in the sandbox, if it is passed, check the deployment errors, there might be some other apex component is giving error.

Once you run All tests in production ( develop > apex test execution) , then disable parallel apex test runs (settings)

Then make your user as debug user and try to validate the changeset again and check the debug log for complete process ( make sure you disabled parallel apex test execution)



Since you want to update a field using Id, you can remove after delete, after update and before insert trigger events


You can stop recurcive triggers since you are using before update trigger event




t.deepthi05t.deepthi05
I issuse is due some trigger which is firing and getting recrussive 

I added the logic of  isRecursive = true; and i was able to deploy fine