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
chiranthchiranth 

Too many Soql 101 Exception in the below attached code

trigger EmailNotificationForQuote on Case (after insert, after update) {
set<id> se1=new set<id>();
List<Case> c1=new List<Case>();


for (Case c : Trigger.new)
{
se1.add(c.Id);
}
if(se1.size() > 0)
{
c1=[Select id,Case_Record_Type__c,Quote_Number__c,testtext__c,testDifferenceDate__c from Case where id IN:se1];

for(Case newCase:c1 ){
System.debug(Trigger.new.size()+'------chiru--');
System.debug(newCase+'------chiranth--');
if(newCase.Case_Record_Type__c =='Quotes' && newCase.Quote_Number__c !=null&& newCase.testtext__c=='Incoming'&& newCase.testDifferenceDate__c==2){
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'tets@gmail.com'};
mail.setToAddresses(toAddresses);
string subject='Hi';
string body='Hw ttttttttttttttttttttttttttttt u';
mail.setSubject(subject);
mail.setPlainTextBody('Hhhhhhhhhhhhhhhhh');
mail.setHtmlBody(body);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
newCase.Sub_Status__c='Follow-Up';

}


}
update c1;
}


}

 

Please help me on this.

Thanks

Hengky IlawanHengky Ilawan

Hi,

 

You call a DML update on the case record which will fire the trigger again, causing an infinite loop.

 

Regards,

Hengky

chiranthchiranth
Thanks, can u please post me the solution on this. Thanks and Regards, __________________________________________________________________________________________________________________________________________________________________________ Chiranth Aradhya ______________________________________________________________________ Disclaimer:This email and any attachments are sent in strictest confidence for the sole use of the addressee and may contain legally privileged, confidential, and proprietary data. If you are not the intended recipient, please advise the sender by replying promptly to this email and then delete and destroy this email and any attachments without any further use, copying or forwarding
MagulanDuraipandianMagulanDuraipandian

trigger EmailNotificationForQuote on Case (after insert, after update) {
string se1;
List<Case> c1=new List<Case>();


for (Case c : Trigger.new)
{
se1=c.Id;
}
if(se1.size() > 0)
{
c1=[Select id,Case_Record_Type__c,Quote_Number__c,testtext__c,testDifferenceDate__c from Case where id =:se1];

for(Case newCase:c1 ){
System.debug(Trigger.new.size()+'------chiru--');
System.debug(newCase+'------chiranth--');
if(newCase.Case_Record_Type__c =='Quotes' && newCase.Quote_Number__c !=null&& newCase.testtext__c=='Incoming'&& newCase.testDifferenceDate__c==2){
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'tets@gmail.com'};
mail.setToAddresses(toAddresses);
string subject='Hi';
string body='Hw ttttttttttttttttttttttttttttt u';
mail.setSubject(subject);
mail.setPlainTextBody('Hhhhhhhhhhhhhhhhh');
mail.setHtmlBody(body);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
newCase.Sub_Status__c='Follow-Up';

}


}
update c1;
}


}

 

Try the above code

 

Regards,

Magulan D

Salesforce.com certified Force.com Developer.

 

SFDC Blog

 

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

chiranthchiranth
Thanks Thanks and Regards, __________________________________________________________________________________________________________________________________________________________________________ Chiranth Aradhya ______________________________________________________________________ Disclaimer:This email and any attachments are sent in strictest confidence for the sole use of the addressee and may contain legally privileged, confidential, and proprietary data. If you are not the intended recipient, please advise the sender by replying promptly to this email and then delete and destroy this email and any attachments without any further use, copying or forwarding
chiranthchiranth

I was very  useful thanks

SFFSFF

Where to start?

 

1. This should be a before query, not an after query.

2. You are doing a query inside the for loop. This is not best practices and is probably why you are getting the too-many-queries error.

3. You don't need to execute a query at all - all of the data you need is already available within the system.trigger.new list.

4. If this were a before query, you wouldn't need to do an update at all.

5. You are doing DML inside the for loop. This is not best practices.

6. You are sending an email inside the for loop. This is not best practices.

 

I am not going to do your work for you but you should be able to reduce the number of lines of code by half.

chiranthchiranth

Hi after chaning the code i came up with a trigger which works fine for me

 

 

trigger EmailNotificationForQuote on Case (after insert, after update){
set<id> idSet=new set<id>();
List<Case> caseList=new List<Case>();
if(SomeClass.someValue){
SomeClass.someValue = false;
for(Case c : Trigger.new){
idSet.add(c.Id);
}
if( idSet!=null && idSet.size() > 0){
try{
caseList=[Select id,Case_Record_Type__c,Quote_Number__c,testtext__c,testDifferenceDate__c from Case where id IN:idSet];
}catch(QueryException qe){}
if( caseList !=null && caseList.size() >0 ){
for(Case newCase:caseList ){
if(newCase.Case_Record_Type__c =='Quotes' && newCase.Quote_Number__c !=null&& newCase.testtext__c=='Incoming'&& newCase.testDifferenceDate__c==3){
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'test@test.com'};
mail.setToAddresses(toAddresses);
string subject='Hi';
string body='Hw r u';
mail.setSubject(subject);
mail.setPlainTextBody('Hhhhhhhhhhhhhhhhh');
mail.setHtmlBody(body);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
newCase.Sub_Status__c='Follow-Up';

}
}
}
update caseList;
}

}
}

 

 

 

Thanks for all your help