You need to sign in to do that
Don't have an account?
On Sandbox box Outbound email is not working.
Hi,
I have written a trigger on lead object which checks for duplicates Company name before insert and before update. If it finds duplicate then it should send mail to Owner.
It is working for single and bulk insert except sendMail function.
The trigger is
trigger checkDuplicateLeads on Lead (before insert, before update) { Map<String, Lead> leadMap = new Map<String, Lead>(); for (Lead lead : Trigger.new) { if(Trigger.isInsert || (lead.Company != Trigger.oldMap.get(lead.Id).Company) || (lead.Company != null)) { if(leadMap.containsKey(lead.Company)) { lead.Company.addError('Another new lead has the same Company Name'); } else { leadMap.put(lead.Company, lead); } } } for(Account account : [SELECT Name, Owner.Id, Owner.email FROM Account WHERE Name IN :leadMap.KeySet()]) { Lead newLead = leadMap.get(account.Name); if(account.Name == newLead.Company) { String Message = 'An Account (' + account.Name + ') with this Lead Name already exists'; sendMailToDupLeadsAccounts.sendMail(account.owner.email, Message); newLead.Company.addError(Message); } } for(Lead lead : [SELECT Company, OwnerId FROM Lead WHERE Company IN :leadMap.KeySet()]) { User Owner = [SELECT email FROM User WHERE Id = :lead.ownerid LIMIT 1]; Lead newLead = leadMap.get(lead.Company); if(newLead.Company == lead.Company) { String Message = 'A Lead (' + lead.Company + ') with this Name already exists'; sendMailToDupLeadsAccounts.sendMail(Owner.email, Message); newLead.Company.addError(Message); } } }
The sendMail function is
public class sendMailToDupLeadsAccounts { sendMailToDupLeadsAccounts() { } public static void sendMail(String Email, String Message) { Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); mail.setToAddresses(new String[] { Email }); mail.setSubject('Important Message!'); mail.setPlainTextBody(Message); List<Messaging.SendEmailResult> results = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); system.debug('Result - ' + results); } }
I am getting problem with sendMail function. As I am inserting duplicate lead from my account I should receive mai in my Inbox but it is not happening. Is there setting which needs to be done on Sandbox. I tried all sorts of settings from Email Administration and 'Personal Setup -> Email', but no success.
When I tried same trigger and class from my Dev Org it is working fine.
Has anyone came across similar problem. It will help a lot if you can guide me.
Thanks and Regards,
Rahul Borgaonkar
The sending of emails from sandboxes changed in Spring 13. If you go to : Setup -> Administration Setup -> Email Administration -> Deliverability, what is the value of the 'Access to Send Email' picklist? This will default to 'System Email Only' and needs to be set to 'All email' to allow emails to be sent.
If that is set correctly, you should check that the email address you are trying to send a message to has not been marked as bouncing.
Hi Bob,
I have checked that option. It is set to 'All email'. The mail id to which I am sending this email is my company email. When I tried to send an activity history mail from Lead screen to my company mail id it is working fine. Any further clues will help a lot.
Thanks,
Rahul Borgaonkar
Have you turned on debug logging and checked that the mail is being queued correctly? It should show in the list of governor limit usage at the end.
Hi Bob,
The email is sent without any problem. Please read last part of the blog for your attn.
Please let me know if you need more details.
Thanks,
Rahul
Have you checked the Email Log Files (under Setup -> Administration Setup -> Monitoring)? It may be a greylisting issue.
Hi Bob,
There are no logs in that section. I just a put a request to get for last 2 days.
Thanks,
Rahul
Hi Bob,
I found the problem. It is not sending mail because of addError function. Though sendMail is before addError, its not sending mail. If I remove it completely it is sending mail.
sendMailToDupLeadsAccounts.sendMail(Owner.email, Message);
newLead.Company.addError(Message);
I need to call sendMail in exception function. Any idea how to do that in trigger?
Thanks,
Rahul
You can't do that - if you add an error, that will roll the transaction back, which will remove everything you have done, including queued emails, @future calls etc.
The best I've been able to workaround this is to mark the record as in error, then make an @future call to do what I need to with that record - which may be deleting it. Its not a good user experience though.
You'll need to rethink the process I reckon.
Hi Bob,
Is this same for Task too. I was trying to insert Task but no success ? Let me know.
Thanks,
Rahul
Its the same for everything. When you add an error, that causes the transaction to be rolled back, which discards all of your work.
Thanks Bob,
Just one quick question. For installing Data Loader I need admin access, but do i need admin access to run it also ?
System Requirements
I don't have admin rights on my machine in my company.
Please reply,
Thanks,
Rahul
I don't know to be honest. I've always had administrator privileges. I would expect that once it is installed you'd be able to run it on the local machine without problem, but I haven't tried that.