You need to sign in to do that
Don't have an account?
LoneStar69
Trigger on EmailMessage for specific Case record types
Hi Guys,
I want to throw an error message when FROM address is not "Email@sample.com" ONLY for a particular record type.
I have written the below code, but for some reason, it evaluates for all the record types.
trigger Validate_OnEmail on EmailMessage (before insert, before update) {
List<Case> cs = [SELECT RecordTypeId FROM Case WHERE RecordTypeId = '000XXX000'];
for (Case cs1:cs)
{
for (EmailMessage newEmail:trigger.new)
{
if( newEmail.Incoming!=true && (newEmail.FromAddress!='Email@sample.com'))
newEmail.addError('From:Email Address should be "Email@sample.com"');
}
}
}
Returns no errors, but triggers for case replies of all record types.
Appreciate your help!!!
I want to throw an error message when FROM address is not "Email@sample.com" ONLY for a particular record type.
I have written the below code, but for some reason, it evaluates for all the record types.
trigger Validate_OnEmail on EmailMessage (before insert, before update) {
List<Case> cs = [SELECT RecordTypeId FROM Case WHERE RecordTypeId = '000XXX000'];
for (Case cs1:cs)
{
for (EmailMessage newEmail:trigger.new)
{
if( newEmail.Incoming!=true && (newEmail.FromAddress!='Email@sample.com'))
newEmail.addError('From:Email Address should be "Email@sample.com"');
}
}
}
Returns no errors, but triggers for case replies of all record types.
Appreciate your help!!!
The reason why your code is triggering the validation every time is because you are not really looking at the Cases related to the EmailMessages being sent. Your query is actually just pulling all cases of the specific record type. So, it will always trigger the validation.
I have modified your code and added annotations to explain each step. Please let me know if you have any questions.
Good Luck!
If this answer helps you on your way, don't forget to select it as the best answer!
All Answers
The reason why your code is triggering the validation every time is because you are not really looking at the Cases related to the EmailMessages being sent. Your query is actually just pulling all cases of the specific record type. So, it will always trigger the validation.
I have modified your code and added annotations to explain each step. Please let me know if you have any questions.
Good Luck!
If this answer helps you on your way, don't forget to select it as the best answer!
In the above code, can i update the from address of the outgoing email.
Something like,
if( newEmail.Incoming != true && cs.containsKey(newEmail.ParentId) )
newEmail.FromAddress = 'Email@sample.com';
It throws no errors but, doesn't seem to be working.
Thanks for your time!!!
I don't think its possible. Your code is correct, so if its not working, then its definitely not possible.
Check out this idea: https://success.salesforce.com/ideaView?id=08730000000kdYZAAY