+ Start a Discussion

How to change EmailMessage ParentId (Case) via Trigger?

Hey Everyone,

I've grabbed some trigger code from another forum to solve my main issue of new emails coming into a already closed case. What I would like to also accomplish in this trigger is have the incoming EmailMessage be related to the cloned case, and it's original parent case. Is it possible to reassign the EmailMessages ParentID after insert?

Here is my current code for the trigger:
trigger CloneClosedCase on EmailMessage (after insert) {
Set<ID> caseSet = new Set<ID>();
List<Case>cloneList = new List<Case>();
Map<Id, String> emailMap = new Map<Id, String>{};
for (EmailMessage so : Trigger.new) {
    if(so.ParentId.getSObjectType() == Case.sObjectType && so.Incoming){
        emailMap.put(so.ParentId, so.TextBody);
Map<Id,case> caseMAP = new Map<Id,case>([SELECT id, [OTHER FIELDS]   FROM Case WHERE id in:caseSet]);  
for(Case c:caseMAP.values()){
    if(c.Status=='Closed') {
        Case cloneCase = c.clone(false,true);
        cloneCase.Subject = 'C: '+c.Subject;
try { 
    insert cloneList;      
} catch(DMLException e) {   
     System.debug('The following exception has occurred: ' + e.getMessage()); 
I cant seem to figure out if 1) it is possible to change the parent Id of an EmailMessage or 2) if i am going about it the right way.


Thanks in advance!
Best Answer chosen by Molson94
Unfortunately, you cannot update the ParentId field on an EmailMessage.  You may be able to insert a new EmailMessage and use the ReplyToEmailMessageId field to link the two together.  I'm not certian if this will work to be honest.