function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Melissa GMelissa G 

Trigger by specific Record type

Good afternoon!

I was wondering if someone could further assist with the following question from August: https://developer.salesforce.com/forums/ForumsMain?id=906F0000000g0j0IAA.

The original question was:

Hi! I am trying to edit some code to copy an email message to case comments based on specific record types. I have two or three record types to add. I have little coding experience so I understand the logic of what I need to do, just not the actual code. I found code for this trigger & class online and it works great as it stands (for all record types). I found additional code for selecting the record type, which it appears to do, but I have no idea how to add additional record types to the query or what to do after the record type is selected. Can someone help me fill in the blanks? The trigger is CopyEmail and the class is CopyEmailCode. Thanks!
 
trigger CopyEmail on EmailMessage (after insert) {
Id recordTypeId = [Select Id From RecordType Where DeveloperName = 'Salesforce_Support'].Id;

/*
How do I add a second and third record type of ‘Salesforce_Projects’ & ‘Salesforce_Training’?
Then, what else goes here to make it only select the requested record types?
*/

    CopyEmailCode.copyEmail(Trigger.new);
}

I received the following response that caused an error I do not understand:
 
trigger CopyEmail on EmailMessage (after insert) {

List<EmailMessage>emailmlist=new List<EmailMessage>();

map<ID,RecordType>mapval=new map<ID,RecordType>([SELECT DeveloperName,Id,IsActive,SobjectType FROM RecordType WHERE DeveloperName IN ('Salesforce_Support','Salesforce_Training','Salesforce_Projects') AND SobjectType = 'EmailMessage' AND IsActive=true]);
system.debug('-----'+mapval);


for(EmailMessage e:Trigger.new){

  if(e.recordtypeid!null){

     if((mapval.get(e.recordtypeid).DeveloperName=='Salesforce_Support' ) || (mapval.get(e.recordtypeid).DeveloperName=='Salesforce_Training' ) || (mapval.get(e.recordtypeid).DeveloperName=='Salesforce_Projects' )){
       
emailmlist.add(e);
      
}

}

}

if(emailmlist.size()>0){
    CopyEmailCode.copyEmail(emailmlist);

}
}

The error was: Invalid field recordtypeid for SObject EmailMessage

The only thing I am trying to accomplish is to trigger a pre-written Apex class based on a specific record type. Basically, we have 4 different departments using cases and I want to be able to copy emails to the case comments section to allow a user to follow a chain of communication (instead of trying to go through emails). We exclusively use email-to-case so we never have customer case comments. This makes following what is going on difficult. I found and installed the code here https://success.salesforce.com/answers?id=90630000000glk9AAA. This works great but I need it to only work on specific record types so I can turn it off for the department that doesn't want to use it. I also noticed that the emails sent by that department are too long and error with this code. 

I am just looking for a solution to that problem. Any help anyone can provide would be much appreciated!!

Thanks
SalesFORCE_enFORCErSalesFORCE_enFORCEr
These are Case record types, if I am not wrong but you are passing EMail Message as SobjectType in the query. Email Messaage does not have record types so you need to query on Case object using the ParentId field on EMail Message and then filter out the case records based on record types.
SalesFORCE_enFORCErSalesFORCE_enFORCEr
Try this
trigger CopyEmail on EmailMessage (after insert) {

List<EmailMessage>emailmlist=new List<EmailMessage>();

map<ID,RecordType>mapval=new map<ID,RecordType>([SELECT DeveloperName,Id,IsActive,SobjectType FROM RecordType
	WHERE DeveloperName IN ('Salesforce_Support','Salesforce_Training','Salesforce_Projects') AND SobjectType = 'Case' AND IsActive=true]);
system.debug('-----'+mapval);


for(EmailMessage e:Trigger.new){
	Case c = [Select RecordTypeId from Case where Id=:e.ParentId];

  if(c.recordtypeid!=null){

     if(mapval.containsKey(c.RecordTypeId)){
       
emailmlist.add(e);
      
}

}

}

if(emailmlist.size()>0){
    CopyEmailCode.copyEmail(emailmlist);

}
}