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
Rahul BorgaonkarRahul Borgaonkar 

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

bob_buzzardbob_buzzard

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.

Rahul BorgaonkarRahul Borgaonkar

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

bob_buzzardbob_buzzard

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.

Rahul BorgaonkarRahul Borgaonkar

Hi Bob,

 

The email is sent without any problem. Please read last part of the blog for your attn.

 

16:47:23.260 (260315000)|SYSTEM_CONSTRUCTOR_ENTRY|[16]|<init>()
16:47:23.260 (260335000)|SYSTEM_CONSTRUCTOR_EXIT|[16]|<init>()
16:47:23.260 (260373000)|SYSTEM_METHOD_ENTRY|[16]|LIST<Messaging.SingleEmailMessage>.add(Object)
16:47:23.260 (260413000)|SYSTEM_METHOD_EXIT|[16]|LIST<Messaging.SingleEmailMessage>.add(Object)
16:47:23.260 (260480000)|SYSTEM_METHOD_ENTRY|[16]|Messaging.sendEmail(LIST<Messaging.Email>)
16:47:23.266 (266728000)|EMAIL_QUEUE|[16]|subject: Important Message!, bccSender: false, saveAsActivity: true, useSignature: true, toAddresses: [rahul.borgaonkar@synety.com], plainTextBody: An Account (AAA PLC) with this Lead Name already exists, 
16:47:23.266 (266881000)|SYSTEM_METHOD_EXIT|[16]|Messaging.sendEmail(LIST<Messaging.Email>)
16:47:23.266 (266920000)|SYSTEM_METHOD_ENTRY|[18]|Limit.getEmailInvocations()
16:47:23.266 (266946000)|SYSTEM_METHOD_EXIT|[18]|Limit.getEmailInvocations()
16:47:23.266 (266969000)|SYSTEM_METHOD_ENTRY|[18]|String.valueOf(Object)
16:47:23.266 (266993000)|SYSTEM_METHOD_EXIT|[18]|String.valueOf(Object)
16:47:23.267 (267020000)|SYSTEM_METHOD_ENTRY|[18]|System.debug(ANY)
16:47:23.267 (267037000)|USER_DEBUG|[18]|DEBUG|Limit After - 2
16:47:23.267 (267050000)|SYSTEM_METHOD_EXIT|[18]|System.debug(ANY)
16:47:23.267 (267071000)|SYSTEM_METHOD_ENTRY|[19]|String.valueOf(Object)
16:47:23.267 (267152000)|SYSTEM_METHOD_EXIT|[19]|String.valueOf(Object)
16:47:23.267 (267173000)|SYSTEM_METHOD_ENTRY|[19]|System.debug(ANY)
16:47:23.267 (267189000)|USER_DEBUG|[19]|DEBUG|Result - (Messaging.SendEmailResult[getErrors=();isSuccess=true;])
16:47:23.267 (267203000)|SYSTEM_METHOD_EXIT|[19]|System.debug(ANY)
16:47:23.267 (267234000)|METHOD_EXIT|[37]|01pZ0000000A0PD|sendMailToDupLeadsAccounts.sendMail(String, String)
16:47:23.267 (267286000)|SYSTEM_METHOD_ENTRY|[38]|SObject.addError(String, String)
16:47:23.267 (267417000)|SYSTEM_METHOD_EXIT|[38]|SObject.addError(String, String)
16:47:23.267 (267446000)|SYSTEM_METHOD_ENTRY|[25]|Database.QueryLocatorIterator.hasNext()
16:47:23.267 (267484000)|SYSTEM_METHOD_EXIT|[25]|Database.QueryLocatorIterator.hasNext()
16:47:23.267 (267526000)|SYSTEM_METHOD_ENTRY|[42]|System.debug(ANY)
16:47:23.267 (267560000)|USER_DEBUG|[42]|DEBUG|Loop 3
16:47:23.267 (267580000)|SYSTEM_METHOD_EXIT|[42]|System.debug(ANY)
16:47:23.267 (267661000)|SYSTEM_METHOD_ENTRY|[44]|MAP<String,Lead>.keySet()
16:47:23.267 (267749000)|SYSTEM_METHOD_EXIT|[44]|MAP<String,Lead>.keySet()
16:47:23.268 (268177000)|SOQL_EXECUTE_BEGIN|[44]|Aggregations:0|select Company, OwnerId from Lead 
16:47:23.274 (274037000)|SOQL_EXECUTE_END|[44]|Rows:0
16:47:23.274 (274139000)|SYSTEM_METHOD_ENTRY|[44]|Database.QueryLocator.iterator()
16:47:23.274 (274335000)|SYSTEM_METHOD_EXIT|[44]|Database.QueryLocator.iterator()
16:47:23.274 (274366000)|SYSTEM_METHOD_ENTRY|[44]|Database.QueryLocatorIterator.hasNext()
16:47:23.274 (274406000)|SYSTEM_METHOD_EXIT|[44]|Database.QueryLocatorIterator.hasNext()
16:47:23.963 (274462000)|CUMULATIVE_LIMIT_USAGE
16:47:23.963|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 2 out of 100
  Number of query rows: 1 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Number of code statements: 30 out of 200000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 2 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10

 Please let me know if you need more details.

 

Thanks,

 

Rahul

 

 

bob_buzzardbob_buzzard

Have you checked the Email Log Files (under Setup -> Administration Setup -> Monitoring)?  It may be a greylisting issue.

Rahul BorgaonkarRahul Borgaonkar

Hi Bob,

 

There are no logs in that section. I just a put a request to get for last 2 days.

 

Thanks,

 

Rahul

Rahul BorgaonkarRahul Borgaonkar

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

bob_buzzardbob_buzzard

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.

Rahul BorgaonkarRahul Borgaonkar

Hi Bob,

 

Is this same for Task too. I was trying to insert Task but no success ? Let me know.

 

Thanks,

 

Rahul

 

bob_buzzardbob_buzzard

Its the same for everything.  When you add an error, that causes the transaction to be rolled back, which discards all of your work.

Rahul BorgaonkarRahul Borgaonkar

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

To use Data Loader, you need:
  • Microsoft® Windows® 7 or Windows XP
  • 120 MB free disk space
  • 256 MB available memory
  • Java JRE 1.6 or later (Windows 7 or Windows XP)
  • Sun JVM 1.6 or later (Windows 7 or Windows XP)
  • Administrator privileges on the machine

I don't have admin rights on my machine in my company.

Please reply,

 

Thanks,

 

Rahul

bob_buzzardbob_buzzard

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.

Rahul BorgaonkarRahul Borgaonkar
No Problem, Thanks a lot