• Cristiam Jackson
  • NEWBIE
  • 0 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
Hello,

As part of a test method, I need to create an EmailMesage (on a Case) with some Attachments.

When I performing a manual test on the UI, the functionality works fine, i.e. I simply go to a Case record and from the "Emails" Related List, I add an EmailMessage with an Attachment. During such manual process, these are the steps:
1) Adding a new Email on the Emails Related List of the Case;
2) Populate some required fields (Subject)on the email form the "To" field;
3) click the button "Attach File" > click button "Choose File" > Click button "Attach To Email" > Click button "Done".
(the Attachment is now attached to the email, even BEFORE sending that email (I guess the email record with its attachment are in a caché until the email is actually sent;
Surpringly, the EmailMessage has its standard checkbox "HasAttachment" = TRUE, even before commintting the EmailMessage record to the database.
4)  Clic button "Send" (on Email Message).

This manuall process triggers the population of a custom checkbox field "Hidden_Has_Attachment__c" on the Case object.

The problem is when coding the Apex TEST CLASS:

On the Test Class, I create the List of Cases, then the List of EmailMessages and finaly a List of Attachments.

The Attachment is associated to its parent EmailMessage through its field ParentId.

I need to create, in the Test Class, the EmailMessages WITH Attachments. How can I do this?

The TEST CLASS creates records in the following sequence:
1) Creates Case;
2) Creates EmailMessages (without Attachment)
3) Creates Attachments

The problem is: I cannot create EmailMessages WITH Attachments and insert these EmailMessages with Attachments so as to get the trigger to trigger (which would help the apex test class to pass).
The test method in the test class needs to insert EmailMessages that will enter the following if statement:
for (EmailMessage em : trigger.new){
      if (em.HasAttachment == TRUE &&
          em.ParentId.getSobjectType() == Case.SobjectType){
               emailIds.add(em.Id);
               }

When the test is done manually on the Salesforce classic UI, the Email Messages enters the if statement but when the apex test method runs, the EmailMessage is NOT entering the if statement because em.HasAttachment is NOT true.