anusha(salesforce developer)

Test Class To Cover Email Message trigger in Salesforce

Sample Test class code:-

//    Create case with test data

         Case Cas= new Case();
         Cas.Subject= 'testsubject';
         Cas.recordtypeid=casRecordTypeId;
         Cas.AccountId=acc.id; 
         Cas.ContactId=con.id;
         insert Cas;
 
//      Below data covers if you trigger logic  for outgoing email 
//     That will be decided based on flag Incoming (default is false)

 EmailMessage outGoingMail= new EmailMessage();
         outGoingMail.fromaddress='test@test.com';
         outGoingMail.toAddress = con1.Email;
         outGoingMail.subject = 'Opt Out Test Message';
         outGoingMail.TextBody= 'This is the message body BR-Interno.';
         outGoingMail.parentid=Cas.id;
         insert outGoingMail;          
          
  //*********** incoming email message*************
//    Incoming flag tru makes this as incoming email.          
 //    you can update fromaddress and toAddress according to your logic

         EmailMessage incomingMail = new EmailMessage();
         incomingMail .fromaddress=test@email.com;
         incomingMail .toAddress = test@test.com;
         incomingMail .subject = ' Test Message';
         incomingMail .TextBody= 'This is the message body ';
         incomingMail .parentid=Cas.id;
         incomingMail .incoming=true;
         insert incomingMail ;       

1 comment:

  1. Hi There,

    Interesting piece!Great to see someone write Test Class To Cover Email Message trigger in Salesforce who is not a fanatic or a complete skeptic.

    I am trying to complete the Trailhead for Paths and Workspaces and am encountering an issue where I have to add an event via the quick action "New Event".

    This button does not exist on the page layout. I have added it to all Lead page layouts, the Lead object does not currently utilise record types and I have enabled the Lightning buttons override. Despite this, there is still no button appearing under Activity for "New Event" as the Trailhead indicates there should be.

    There are two threads on this issue that have been marked as SOLVED however the solutions posted there have no helped in resolving this issue for me.

    If anyone can help provide a solution that would be great. I would really like to complete this Trailhead!

    Appreciate your effort for making such useful blogs and helping the community.

    Many Thanks,
    Preethi.

    ReplyDelete