+ Start a Discussion
LoneStar69LoneStar69 

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!!!
Best Answer chosen by LoneStar69
SlashApex (Luis Luciani)SlashApex (Luis Luciani)
Hi Mairuz!

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!
 
trigger Validate_OnEmail on EmailMessage (before insert, before update) 
{
	//Get case Ids from the email messages being processed
	Set<Id> caseIds = new Set<Id>();
	for (EmailMessage newEmail : trigger.new)
	{
		caseIds.add(newEmail.ParentID)
	}
    
    //Query all cases related to the email messages being processed that contain the specific record type
    Map<id,Case> cs = new Map<id,Case>([SELECT RecordTypeId FROM Case WHERE RecordTypeId = '000XXX000' AND id IN :caseIds]);
    

    for (EmailMessage newEmail:trigger.new)
	{
		//For each email message being processed, error if they are not Incoming email and From Address is not the sample address, and the related case
		//is on the map of cases with the specific record type (related case is of the specific record type)
    	if( newEmail.Incoming != true && ( newEmail.FromAddress != 'Email@sample.com' ) && cs.containsKey(newEmail.ParentId) )
 			newEmail.addError('From:Email Address should be "Email@sample.com"');
	}
}

 

All Answers

SlashApex (Luis Luciani)SlashApex (Luis Luciani)
Hi Mairuz!

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!
 
trigger Validate_OnEmail on EmailMessage (before insert, before update) 
{
	//Get case Ids from the email messages being processed
	Set<Id> caseIds = new Set<Id>();
	for (EmailMessage newEmail : trigger.new)
	{
		caseIds.add(newEmail.ParentID)
	}
    
    //Query all cases related to the email messages being processed that contain the specific record type
    Map<id,Case> cs = new Map<id,Case>([SELECT RecordTypeId FROM Case WHERE RecordTypeId = '000XXX000' AND id IN :caseIds]);
    

    for (EmailMessage newEmail:trigger.new)
	{
		//For each email message being processed, error if they are not Incoming email and From Address is not the sample address, and the related case
		//is on the map of cases with the specific record type (related case is of the specific record type)
    	if( newEmail.Incoming != true && ( newEmail.FromAddress != 'Email@sample.com' ) && cs.containsKey(newEmail.ParentId) )
 			newEmail.addError('From:Email Address should be "Email@sample.com"');
	}
}

 
This was selected as the best answer
LoneStar69LoneStar69
Awesome Luis!!! Thank you for adding annotations. Now will be heading towards the test class.
 
LoneStar69LoneStar69
Luis,

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!!!
SlashApex (Luis Luciani)SlashApex (Luis Luciani)
Hello,

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
LoneStar69LoneStar69
I tried to implement in a diff way, but no luck, opened up another question earlier - https://developer.salesforce.com/forums/ForumsMain?id=906F0000000BFoAIAW