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
nasir jawednasir jawed 

Trigger to populate an Email message object related to cases

Hi

 

i have written a trigger.

trigger Duplicate on Case (before insert) {
    EmailMessage em = new EmailMessage();
    Case dupCase;
    boolean duplicate;
    for (Case c : System.Trigger.new) {
        if(c.Subject.contains('Re:')){
            dupCase = c;
            duplicate = true;
            
        }
    }
    
    if(dupCase!=null){
        String dupCaseSubject = dupCase.Subject.substring(3, dupCase.Subject.length());
        System.debug('RII Debug:'+ dupCaseSubject);
        Case myCase = [select id, Subject from Case where CaseNumber ='00001073'];        
        em.ParentId = myCase.id;
        System.debug('RII Debug caseid:'+ em.ParentId);
        //em.Status='New';
        em.Subject = dupCase.Subject;
        em.FromAddress = 'ramesh@rixyncs.co.in';
        em.FromName='Test';
        em.Incoming = true;
        System.debug('RII Debug sub:'+ em.Subject);
        em.TextBody = dupCase.Description;
        insert em;
        System.debug('RII Debug inserted:'+ em);
  }
    if(duplicate){
        dupCase.addError('This is a reply email');
    }
}

 

 

 

He is the scenario like when ever a case is created from 'email to case'.When we open that case there is a related list "Email" in which that email comes and sits in.

 

now if i use email to case mail id and create a case and also cc to some other person.This mail ceates a case and it is also send to the other person.Suppose if the other person know the answer for that case and he replies back to "email to case" mail .For this a new case should not create,instead it should get attached to the "Email" related list.

 

Please help