You need to sign in to do that
Don't have an account?

AddError and sending email doesn't work at same transaction
Hi all,
I'd like to prevent certain cases from coming in, and also notify the user about that.
Before insert trigger with AddError to prevent the case creation, also prevents the Messaging.SingleEmailMessage from being sent for some reason. Tried to run the message sending in a @future method, did not work either. Anybody has any idea or advice?
Thank you,
Eran
I'd like to prevent certain cases from coming in, and also notify the user about that.
Before insert trigger with AddError to prevent the case creation, also prevents the Messaging.SingleEmailMessage from being sent for some reason. Tried to run the message sending in a @future method, did not work either. Anybody has any idea or advice?
Thank you,
Eran
public static void blockEmailToCaseFromExternalDomains(List<Case> triggerNew)
{
List<Case> lst_CasesForDelete = new List<Case>();
for (Case c: triggerNew)
{
if (c.Origin == 'email2case')
{
if (c.SuppliedEmail.split('@')[1] != 'perfectomobile.com' && c.SuppliedEmail.split('@')[1] != 'verizon.com')
{
Utilities.sendTemplatedEmail(new List<String>{c.SuppliedEmail}, null, new List<String>{'eranv@perfectomobile.com', 'danielt@perfectomobile.com', 'matthewr@perfectomobile.com'}, 'Email2Case_Retired_Auto_Response', '0032000000Vzw8X', null, 'no-reply@perfectomobile.com',false);
c.addError('Cannot create external case with Email2Case');
}
}
}
}
public static void sendTemplatedEmail(String[] toRecipients, String[] ccRecipients, String[] bccRecipients, String templateApiName, ID targetObjId, Id whatId, String fromaddress, Boolean saveAsActivity)
{
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
Id templateId = [select id, name from EmailTemplate where developername = : templateApiName].id;
email.setToAddresses(toRecipients);
email.setCcAddresses(ccRecipients);
email.setBccAddresses(bccRecipients);
email.setTargetObjectId(targetObjId);
email.setWhatId(whatId);
email.setSenderDisplayName(fromaddress);
email.setTemplateId(templateId);
email.setSaveAsActivity(saveAsActivity);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
return;
}
Thanks