+ Start a Discussion
TedXYZ7TedXYZ7 

EmailMessage.HasAttachment not updated by apex Test Class

The System.AssertEqual (); is not returning a PASS in my Apex Test Class.
This is because, the field EmailMessage.HasAttachment (standard field) is not being updated to True by Salesforce (this is a protected Read-Only field of Salesforce).

Here is my apex test class:
@isTest static void insertingEmailMssgWithAttachment() {
        Case c = new Case();
        c.Subject = 'Caso de ejemplo';
        c.Status = 'New';
        c.Origin = 'Phone';
        c.Hidden_Has_Attachment__c = FALSE;
        insert c;
        
        EmailMessage e = new EmailMessage();
        e.Subject = 'Email de ejemplo';
        e.ParentId = c.Id;
        insert e;
        
        Attachment a = new Attachment();
        a.Name = 'Adjunto de ejemplo';
        a.ParentId = e.Id;
        Blob b = blob.valueOf('test');
        a.Body = b;
        insert a;
        
        EmailMessage myEmailMssg = [SELECT Id, HasAttachment FROM EmailMessage];
        System.debug('FROM Test Class HasAttachmentCaseEmail_Test - Display myEmailMssg: '+myEmailMssg);
        System.assertEquals(True, myEmailMssg.HasAttachment, 'Error mssg: The outcome should be True but its not');
        
        Case myCase = [SELECT Id, Hidden_Has_Attachment__c FROM Case];
        System.assertEquals(True, myCase.Hidden_Has_Attachment__c, 'Error mssg: The HiddenHasAttachment field should be True but its not');
    }

The following apex trigger contains an if condition:
if(em.HasAttachment == TRUE) {...code...}

and unfortunately, when running the test class, it is not running the code depending on that if condition.
 
Set<Id> emailIds        = new Set<Id>();
.....

        //==> Verify if EmailMessage has a child Attachment & if its parentId is of Type Case
        for (EmailMessage em : trigger.new){
            System.debug('FROM: EmailMessageTrigger - 2ondtrigerNew: '+trigger.new);
            if(em.ParentId.getSobjectType() == Case.SobjectType) {
                System.debug('FROM: EmailMessageTrigger - ParentId is SobjectType = True?-->Yes!');
                if(em.HasAttachment == TRUE){
                    System.debug('FROM: EmailMessageTrigger - Email HasAttachment field = TRUE? --> Yes!');
                    emailIds.add(em.Id); 
                }
            }
        }
          System.debug('FROM: EmailMessageTrigger - set of all inserted/updated emails: '+emailIds);
When doing manual tests of this functionality (adding an Attachment to the EmailMessage attached to a Case), such manual test works correctly (it passes OK).

Can you possibly help?

Thank you very much.
 
Rameshwar gaurRameshwar gaur
I run your test class and found that your EmailMessage.HasAttachment (True, myEmailMssg.HasAttachment) field passtest but System.assertEquals(True, myCase.Hidden_Has_Attachment__c)  is not because its  not  update in trigger.
Please post complete trigger code so we get on what case your trigger invoke and on which object.
or else you have to change your code like that 
Set<Id> emailIds        = new Set<Id>();
		Map<ID,Case> casemp = new Map<Id,case>([SELECT ID,Hidden_Has_Attachment__c FROM Case]);
		List<case> CaseUpdate = new List<case>();

        //==> Verify if EmailMessage has a child Attachment & if its parentId is of Type Case
        for (EmailMessage em : trigger.new){
            System.debug('FROM: EmailMessageTrigger - 2ondtrigerNew: '+trigger.new);
            if(em.ParentId.getSobjectType() == Case.SobjectType) {
                System.debug('FROM: EmailMessageTrigger - ParentId is SobjectType = True?-->Yes!');
                if(em.HasAttachment == true){
                    System.debug('FROM: EmailMessageTrigger - Email HasAttachment field = TRUE? --> Yes!');
                   casemp.get(em.parentId).Hidden_Has_Attachment__c=TRUE;
                    CaseUpdate.add(casemp.get(em.parentId));
                    emailIds.add(em.Id); 
                }
            }
        }
          System.debug('FROM: EmailMessageTrigger - set of all inserted/updated emails: '+emailIds);
		Insert CaseUpdate;
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

 
Rameshwar gaurRameshwar gaur
If you find your answer please close this thread.