You need to sign in to do that
Don't have an account?
EdCode
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:
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.
Can you possibly help?
Thank you very much.
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.
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 I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.