+ Start a Discussion
Lakshmi SLakshmi S 

System.debug Statement in Apex class ?

Hi Team,

Can we use System.debug(' caught exception'+ex.getMessage()) this statement in catch block (Production).
Please advise is this any issue in production.

Thanks,
Lakshmin.

 
Best Answer chosen by Lakshmi S
Steven NsubugaSteven Nsubuga
I would move the query outside the for loop. Everything else looks ok.
try{
            List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
            Contact con = [Select id from Contact where Email='abc@com'];
            EmailTemplate et=[Select id,name from EmailTemplate where Name='abc template'];
            
            for(Account aa : accList){
                    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                    mail.setTargetObjectId(con.id);
                    mail.setTemplateId(et.Id); 
                    mail.setWhatId(aa.Id); 
                    mail.setSaveAsActivity(false); 
                    mails.add(mail);
                }
            }
            Messaging.sendEmail(mails);
        }
        catch(Exception ex){
            System.debug('****** Exception Caught :   '+ex.getMessage());
        }

 

All Answers

Steven NsubugaSteven Nsubuga
It is not an issue at all. You can use it.
Raj VakatiRaj Vakati
yes you can use it but if you are calling the code  from 
  1. If you are calling its from the trigger or other backend jobs System.debug logs is fine and make sure dnt print any senstive info 
  2. If you are using in front end like VF page and LTNG ..Show an Error message to the UI also
Lakshmi SLakshmi S
Hi Steven Nsubuga,

Requirement : Using trigger send email before deleting the account. I am using below code for sending email, is this code correct or i need to add any error message to display for user or any code changes required. Please advise.
try{
            List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
            Contact con = [Select id from Contact where Email='abc@com'];
            Messaging.SingleEmailMessage mail;
            for(Account aa : accList){
                    mail.setTargetObjectId(con.id);
                    EmailTemplate et=[Select id,name from EmailTemplate where Name='abc template'];
                    mail.setTemplateId(et.Id); 
                    mail.setWhatId(aa.Id); 
                    mail.setSaveAsActivity(false); 
                    mails.add(mail);
                }
            }
            Messaging.sendEmail(mails);
        }
        catch(Exception ex){
            System.debug('****** Exception Caught :   '+ex.getMessage());
        }

Thanks,
Lakshmi.
 
Steven NsubugaSteven Nsubuga
I would move the query outside the for loop. Everything else looks ok.
try{
            List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
            Contact con = [Select id from Contact where Email='abc@com'];
            EmailTemplate et=[Select id,name from EmailTemplate where Name='abc template'];
            
            for(Account aa : accList){
                    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                    mail.setTargetObjectId(con.id);
                    mail.setTemplateId(et.Id); 
                    mail.setWhatId(aa.Id); 
                    mail.setSaveAsActivity(false); 
                    mails.add(mail);
                }
            }
            Messaging.sendEmail(mails);
        }
        catch(Exception ex){
            System.debug('****** Exception Caught :   '+ex.getMessage());
        }

 
This was selected as the best answer
Lakshmi SLakshmi S
Hi Steven Nsubuga,

Thanks for your reply.
I forgot about the query inside for loop,

Thanks,
Lakshmi.