+ Start a Discussion
Sunny SSunny S 

Trigger Email Alert

Hi members, need help !

I am trying to create a Trigger (on Lead object) for Email Alert based on the Username. In the sense-  whenever a user deletes a lead record, an email should go out the the manager.... but getting across the below error. 
Can someone please rectify my Trigger and suggest that wrong am i doing here ?

*** TRIGGER***

trigger EmailAfterDeleteLead on Lead(after delete) {
    Messaging.reserveSingleEmailCapacity(trigger.size);
        List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
            for (Lead lead : Trigger.old) { 
                Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
                    if (userInfo.LastName=='Event Catering' && userInfo.LastName=='General Catering'){
                        email.setToAddresses(new String[] {'Manager@Functions.com'});
                        }
                        Else {
                    email.setToAddresses(new String[] {'Administrator@Functions.com'});
                email.setSubject('Deleted Lead Alert');
            email.setPlainTextBody('Dear Manager,' +'\n'+ 'This message is an alert that the following lead has been deleted from Salesforce by *** ' + UserInfo.getFirstName() + ' ' + UserInfo.getLastName() + '***. You can restore the deleted lead from recycle bin within 15 days ' + '\n \n'
        + 'Lead Name: ' + lead.FirstName + ' ' + lead.LastName + '\n'
        + 'Lead Email: ' + lead.Email + '\n'
        + 'Lead Status: ' + lead.Status + '\n'       
        + 'Lead Id: ' + lead.Id + '\n\n'
        + 'Current Salesforce User Name: ' + UserInfo.getFirstName() + ' ' + UserInfo.getLastName() +  '\n\n\n\n\n'
        );
        emails.add(email);
        }
        
    }
    Messaging.sendEmail(emails);

}

============================

User-added image
Best Answer chosen by Sunny S
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hey I just gone through your code again.
You are setting body ,subject and add email to sending list only in else loop .Move  all that part to outside of else loop.
Try this code.I tested this in my org.
trigger EmailAfterDeleteLead on Lead(after delete) {
    Messaging.reserveSingleEmailCapacity(trigger.size);
        List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
            for (Lead lead : Trigger.old) { 
                Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
                //user userInfo = new user();
                    if (userInfo.getLastName() =='Event Catering' || userInfo.getLastName() =='General Catering'){
                        email.setToAddresses(new String[] {'xyz@gmail.com'});
                        }
                        Else {
                    email.setToAddresses(new String[] {'Administrator@Functions.com'});
                        }
                email.setSubject('Deleted Lead Alert');
            email.setPlainTextBody('Dear Manager,' +'\n'+ 'This message is an alert that the following lead has been deleted from Salesforce by *** ' + UserInfo.getFirstName() + ' ' + UserInfo.getLastName() + '***. You can restore the deleted lead from recycle bin within 15 days ' + '\n \n'
        + 'Lead Name: ' + lead.FirstName + ' ' + lead.LastName + '\n'
        + 'Lead Email: ' + lead.Email + '\n'
        + 'Lead Status: ' + lead.Status + '\n'       
        + 'Lead Id: ' + lead.Id + '\n\n'
        + 'Current Salesforce User Name: ' + UserInfo.getFirstName() + ' ' + UserInfo.getLastName() +  '\n\n\n\n\n'
        );
        emails.add(email);
        
        
    }
    Messaging.sendEmail(emails);

}

Hope this helps you
Let me know if this helps you. Kindly mark it as solved so that it may help others in future.

Thanks and Regards

All Answers

Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi Sunny,
Replace userinfo.lastName with user info.getLastName().
 try this code
trigger EmailAfterDeleteLead on Lead(after delete) {
    Messaging.reserveSingleEmailCapacity(trigger.size);
        List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
            for (Lead lead : Trigger.old) { 
                Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
                //user userInfo = new user();
                    if (userInfo.getLastName() =='Event Catering' && userInfo.getLastName() =='General Catering'){
                        email.setToAddresses(new String[] {'Manager@Functions.com'});
                        }
                        Else {
                    email.setToAddresses(new String[] {'Administrator@Functions.com'});
                email.setSubject('Deleted Lead Alert');
            email.setPlainTextBody('Dear Manager,' +'\n'+ 'This message is an alert that the following lead has been deleted from Salesforce by *** ' + UserInfo.getFirstName() + ' ' + UserInfo.getLastName() + '***. You can restore the deleted lead from recycle bin within 15 days ' + '\n \n'
        + 'Lead Name: ' + lead.FirstName + ' ' + lead.LastName + '\n'
        + 'Lead Email: ' + lead.Email + '\n'
        + 'Lead Status: ' + lead.Status + '\n'       
        + 'Lead Id: ' + lead.Id + '\n\n'
        + 'Current Salesforce User Name: ' + UserInfo.getFirstName() + ' ' + UserInfo.getLastName() +  '\n\n\n\n\n'
        );
        emails.add(email);
        }
        
    }
    Messaging.sendEmail(emails);

}

Hope this helps you
Let me know if this helps you. Kindly mark it as solved so that it may help others in future.

Thanks and Regards
Sunny SSunny S
Hi Devi, thank you for your help. I am now been able to save the code , BUT it is not working as per expectations.

All the emails are only going the the ELSE email id  (marked under Red box)... Though the email alert should goto the IF (first email id) if being deleted by first 2 users... 
Do you see any issues here ? Kindly advise.

Regards!

User-added image
 
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Please give OR in the if condition because a user cannot have 2 lastnames.So it will will check for lasttname if any of the lastname given in the if condition matches it will be true.
trigger EmailAfterDeleteLead on Lead(after delete) {
    Messaging.reserveSingleEmailCapacity(trigger.size);
        List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
            for (Lead lead : Trigger.old) { 
                Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
                //user userInfo = new user();
                    if (userInfo.getLastName() =='Event Catering' || userInfo.getLastName() =='General Catering'){
                        email.setToAddresses(new String[] {'Manager@Functions.com'});
                        }
                        Else {
                    email.setToAddresses(new String[] {'Administrator@Functions.com'});
                email.setSubject('Deleted Lead Alert');
            email.setPlainTextBody('Dear Manager,' +'\n'+ 'This message is an alert that the following lead has been deleted from Salesforce by *** ' + UserInfo.getFirstName() + ' ' + UserInfo.getLastName() + '***. You can restore the deleted lead from recycle bin within 15 days ' + '\n \n'
        + 'Lead Name: ' + lead.FirstName + ' ' + lead.LastName + '\n'
        + 'Lead Email: ' + lead.Email + '\n'
        + 'Lead Status: ' + lead.Status + '\n'       
        + 'Lead Id: ' + lead.Id + '\n\n'
        + 'Current Salesforce User Name: ' + UserInfo.getFirstName() + ' ' + UserInfo.getLastName() +  '\n\n\n\n\n'
        );
        emails.add(email);
        }
        
    }
    Messaging.sendEmail(emails);

}


Hope this helps you
Let me know if this helps you. Kindly mark it as solved so that it may help others in future.

Thanks and Regards
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hey I just gone through your code again.
You are setting body ,subject and add email to sending list only in else loop .Move  all that part to outside of else loop.
Try this code.I tested this in my org.
trigger EmailAfterDeleteLead on Lead(after delete) {
    Messaging.reserveSingleEmailCapacity(trigger.size);
        List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
            for (Lead lead : Trigger.old) { 
                Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
                //user userInfo = new user();
                    if (userInfo.getLastName() =='Event Catering' || userInfo.getLastName() =='General Catering'){
                        email.setToAddresses(new String[] {'xyz@gmail.com'});
                        }
                        Else {
                    email.setToAddresses(new String[] {'Administrator@Functions.com'});
                        }
                email.setSubject('Deleted Lead Alert');
            email.setPlainTextBody('Dear Manager,' +'\n'+ 'This message is an alert that the following lead has been deleted from Salesforce by *** ' + UserInfo.getFirstName() + ' ' + UserInfo.getLastName() + '***. You can restore the deleted lead from recycle bin within 15 days ' + '\n \n'
        + 'Lead Name: ' + lead.FirstName + ' ' + lead.LastName + '\n'
        + 'Lead Email: ' + lead.Email + '\n'
        + 'Lead Status: ' + lead.Status + '\n'       
        + 'Lead Id: ' + lead.Id + '\n\n'
        + 'Current Salesforce User Name: ' + UserInfo.getFirstName() + ' ' + UserInfo.getLastName() +  '\n\n\n\n\n'
        );
        emails.add(email);
        
        
    }
    Messaging.sendEmail(emails);

}

Hope this helps you
Let me know if this helps you. Kindly mark it as solved so that it may help others in future.

Thanks and Regards
This was selected as the best answer
Sunny SSunny S
Hi Devi, thanks heaps for your reply once again. The code (edited by you) is working perfectly fine now. 

I am still a novice with Triggers and Apex, so learning hard with every mistake of mine. But just wondering how can a missing () and wrong clousure of loop can cause a delay.
Really appreciate your help... Kindest regards !