+ Start a Discussion
nasir jawednasir jawed 

Trigger to populate an Email message object related to cases



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) {
            dupCase = c;
            duplicate = true;
        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.Subject = dupCase.Subject;
        em.FromAddress = 'ramesh@rixyncs.co.in';
        em.Incoming = true;
        System.debug('RII Debug sub:'+ em.Subject);
        em.TextBody = dupCase.Description;
        insert em;
        System.debug('RII Debug inserted:'+ em);
        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