You need to sign in to do that
Don't have an account?
Cloud-Ling
sending outbound email
Greetings,
Any idea why i can't send Email? my code below
Class Code:
public class EmailMessage { public static void SendEmail() { System.debug('Sending Email...'); Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); String[] toAdd = new String[]{'m*******','l*******com','t******.com'}; mail.setToAddresses(toAdd); mail.setReplyTo('ke****.com'); mail.setSenderDisplayName('Salesforce Notification'); mail.setSubject('Duplicating Account Name'); mail.setBccSender(false); mail.setUseSignature(false); mail.setPlainTextBody('Some user attempts to duplicate you Account Name.'); List<Messaging.SendEmailResult> results = Messaging.sendEmail(new Messaging.Email[] { mail }); System.debug('Email Sent: '+results.get(0).isSuccess() ); } }
Trigger Code:
trigger ErrorDuplicateOutput on Account (before insert, before update) { List<Account> accList =new List<Account>(); Set<String> b = new Set<String>(); accList = [Select Name FROM Account]; for (Account acc : accList) { b.add(acc.Name); } for(integer x = 0; x < Trigger.new.size(); x++) { if(b.contains(Trigger.new[x].name)) { Trigger.new[x].addError('Error:Account name already exist!'); EmailMessage.SendEmail(); } } }
Any ideas are welcome, thank you!
S-L
I tested your EmailMessage class and method an that worked from the system log console.
I think the email is not firing because you have the email logic after the error logic. The addError() method kills all processes.
I would change you code to something more like this. Because your original code will break when you have more then 1000 accounts, and it's not very efficient for bulk inserts.
Let me know if the order of the email message send solves your issue.
All Answers
I tested your EmailMessage class and method an that worked from the system log console.
I think the email is not firing because you have the email logic after the error logic. The addError() method kills all processes.
I would change you code to something more like this. Because your original code will break when you have more then 1000 accounts, and it's not very efficient for bulk inserts.
Let me know if the order of the email message send solves your issue.
I have a very much similar scenario. I tried to send the email before the addError() method. but it doesn't work for me.
my code looks like this
dcu.sendEmail(body, toAddresses, 'subject', 'test@gmail.com');
asset.addError('Error:');
Cloud-ling/Mike , can you let me know if that worked for you.
Thanks,
PK