You need to sign in to do that
Don't have an account?
Sunny 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);
}
============================
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);
}
============================
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.
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
Replace userinfo.lastName with user info.getLastName().
try this code
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 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!
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
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.
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
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 !