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
Alfredo Puente VasconcelosAlfredo Puente Vasconcelos 

Problem With Attachment null on Email Message Trigger

Hello everyone, I'm having some issues while I try to get the attachments on an Email Message trigger. I want to block all the emails that have Excel Files attached but when I try to access the Attachments, Apex says the the list is Empty. Can someone help me? :(
 
trigger BlockExcel on EmailMessage (before insert) {
    List<EmailMessage> compatibleEmails = new List<EmailMessage>();
    for(EmailMessage activeMail : Trigger.new){
        if(activeMail.HasAttachment == true && activeMail.Subject.contains('Entrega de propuesta')){
            compatibleEmails.add(activeMail);
        }else{
            System.debug('Dont Enter');
        }
    }
    for(EmailMessage email : compatibleEmails){   
        /*********************ERROR Attachment Size = 0***********/
        System.debug('The size of my attachment list is: ' + email.Attachments.size());      
        for(Integer i = 0; i < email.Attachments.size(); i++){     
            if(email.Attachments[i].ContentType == '.xlsx' || email.Attachments[i].ContentType == 'xlsx'){
                email.addError('Excel Files are not allow');
                break;
            }
        }
    }
}

Thanks for your time
pankul guptapankul gupta
The code seems to be fine, just trythe below, . should be prefixed before xlsx and moreover can you please explain how are you trying to test the same:
if(email.Attachments[i].ContentType == '.xlsx' || email.Attachments[i].ContentType == '.xlsx')

 
Alfredo Puente VasconcelosAlfredo Puente Vasconcelos
Yes I should fix the If clause but my real problem is that my Attachments list is empty. I have a Debug in my code to see the size of the Attachment list when my trigger is activated. So when I send emails to test the trigger I take a look to my logs in my developer console and I noticed that always the Attachment list is empty.
User-added image

User-added image