function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
fuko9xfuko9x 

How to update hasAttachment = true in testclass trigger beforeInsert EmailMessage

I have trigger EmailMessage:
public void onBeforeInsert(List<EmailMessage> newListEmailMessage) {
        for (EmailMessage mess : newListEmailMessage) {
            if (!mess.HasAttachment) {
                newListEmailMessage.get(0).addError('NO ATTACHMENT');
                return;
            }
        }
    }

Code run normal but i can't create testclass.
EmailMessage.HasAttachment : Field is not writeable.
jigarshahjigarshah
The EmailMessage.HasAttachment is readonly Boolean field that will automatically be set to true, when an EmailMessage record his created which has an associated Attachment.

Another thing as per the Email Message documentation (https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_emailmessage.htm), update() call is supported only the Status picklist value on the EmailMessage record is set to Draft. Hence, you may want to create the EmailMessage with Status as Draft and then update the HasAttachment field, though I am not sure if this will work.