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 with specific record type (copy email to case comments)

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);
}
Thanks so much in advance!
 
RAM AnisettiRAM Anisetti
try this one
 
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);

}
}

 
Melissa GMelissa G
Thank you very much for the response! I am getting an error, however, when I save:
 
Invalid field recordtypeid for SObject EmailMessage
Would you mind assisting further with this? I definitely appreciate any further assistance you can provide.

Thanks again,
Melissa