+ Start a Discussion
Bob James 8Bob James 8 

Apex Trigger, Inbound Email, File Type

Wondering if anyone has tackled this.
An Apex Trigger that looks at an inbound email (before insert, before update), and if there is an Attachment, and the Attachment is of certain File Types (.zip, .mp4, etc.) it does not create/update the case AND an email is sent to the sender notifying them that File Type is not accepted and to plesse resend their email with accepted File Type (.jpg, .png, .pdf).
Abdul KhatriAbdul Khatri
Hi Bob,

Yes you can use Messaging.InboundEmailHandler https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_email_inbound_using.htm to track incoming email to Salesforce and do whatever you want to do there.

Do you have anything already build that you can share and can be helped or do you need from scratch?
Bob James 8Bob James 8
I'm starting from scratch, and am very new to Apex coding - just started Monday (4 days ago).
I will look at the InboundEmailHandler to see if I can figuer it out from there.

Thank you Abdul.
I'll let you know how it goes.
Abdul KhatriAbdul Khatri
Hi Bob,

I hope this code will help you move forward. 

Couple of things due you have the format of the Email you are expecting specially when you want to update the case. The email you receive must have some form of Case Id for update. 

I have left couple of things blank that you can fill based how you create or update them. 
 
global class ProcessLeadByEmail implements Messaging.InboundEmailHandler {

    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope){
           
        Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
    
        if(email.binaryAttachments != null && email.binaryAttachments.size() > 0)
        {
            for(Messaging.InboundEmail.BinaryAttachment att : email.binaryAttachments){
            
                system.debug('>>>>>>>>>>>>>>>>> ' + att.mimeTypeSubType);
                if(isAcceptableFileType(att.mimeTypeSubType))
                    system.debug('Create or Update a Case >>>>>>>>>>>>>>>>');
                else{
                    system.debug('Dont create a case and email>>>>>>>>>>>>>>>>>>>>');
                    sendMail(email.fromAddress);
                }
            }
        }  
        
        return result;      
    
    }
    
    
    private void createCase()
    {
        
    }
    
    private void updateCase(Id idCase){
        
    }
    
    private Boolean isAcceptableFileType(String params)
    {
		List<String> acceptableFileExtList = new List<String>{'png','jpeg'};
        
        for(String ext : acceptableFileExtList)
        {
            if(params.contains(ext)) return true;
        }
		return false;
            
    }
    
    private void sendMail(String address) {

        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {address};
        mail.setToAddresses(toAddresses);
        mail.setSubject('Wrong Filetype');
        mail.setPlainTextBody('File Type is not accepted and to plesse resend their email with accepted File Type (.jpg, .png, .pdf)');

        Messaging.SendEmailResult[] results = Messaging.sendEmail(
                                 new Messaging.SingleEmailMessage[] { mail });
        
    }
}

 
Bob James 8Bob James 8
Hi Abdul,

Thank you for your help on this.
In most instances the customer should be responding to us, and in the Subject of their email there should be the Case reference, which looks like this:
ref:_00D3ipdYT._5003iHa68M:ref
ref:_xxxxxxxxx._xxxxxxxxxx:ref
(It should not be the only thing in the Subject, but it should be there.)

The case reference should also appear in the Body.
(We do know that some customers, either on purpose or otherwise, remove these Case references.)

If the Case reference is not there, there is no format, it simply an email from a customer, which would create a new case.
Abdul KhatriAbdul Khatri
Hi Bob,

Thanks for the clarity, so where (which field) that ref:_00D3ipdYT._5003iHa68M:ref exists on the Case Level to match.

Regards,
Bob James 8Bob James 8
It is not in any field, only in the Subject of the email from the customer.
Abdul KhatriAbdul Khatri
Hi Bob,

My Question was as you mentioned that 

"in the Subject of their email there should be the Case reference, which looks like this:
ref:_00D3ipdYT._5003iHa68M:ref
ref:_xxxxxxxxx._xxxxxxxxxx:ref"

How this is referencing the Case?
How would I going to pull the Case based on this reference?
Bob James 8Bob James 8

The actual Case Number is not in the email, this "ref:........:ref" must be there, or a new case will be created.
Salesforce does not care if the actual case number is anywhere in the email, only that this reference is in either the Subject or the Body.

If the "ref:........:ref" is in the Subject or Body, an existing case is updated.
If the "ref:........:ref" is not in the Subject or Body, a new case is created - even if the customer already has a case, if they remove the "ref:........:ref" in their return email, a new case is created.

Abdul KhatriAbdul Khatri
Hi Bob,

Sorry for lots of question as I am trying to get my base to update my code accordingly.
  • So how to pull the customer ( I believe from email you received the message from) ?
  • Is this From the Account or Contact on the Case? (See the screen shot below).
  • If the customer have multiple cases, I believe we need to update the latest one, right?

User-added image
Bob James 8Bob James 8
I think you're over-thinking it.
If the "ref:...:ref" string is in the inbound email from the customer, the correct case will be updated.
If the "ref:....:ref" is missing, then it will create a new case, and this is OK.
My objective is to not allow certain file types.
Abdul KhatriAbdul Khatri
Hi Bob,

Here is the code, atleast this code will as per you objective, allow only types you looking for. Let me know
 
global class ProcessLeadByEmail implements Messaging.InboundEmailHandler {

    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope){
           
        Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
    	Set<String> customerEmailList = new Set<String>();
        
        if(email.binaryAttachments != null && email.binaryAttachments.size() > 0) 
        {
           if(isAcceptableFileType(email.binaryAttachments[0].mimeTypeSubType))
           {
                Case caseRec;
                if(email.subject.contains('ref')){
                    caseRec = [SELECT Id FROM Case WHERE ContactEmail = :email.fromAddress ORDER BY CreatedDate DESC LIMIT  1];
                }
               
                if(caseRec != null)
                    Database.update(caseRec);
                else
                {
                     //createCase()
                }
            }else{
                    system.debug('Dont create a case and email>>>>>>>>>>>>>>>>>>>>');
                    sendMail(email.fromAddress);
            }
        }
        
        return result;      
    
    }
    
    
    private void createCase()
    {
        
    }
    
    private Boolean isAcceptableFileType(String params)
    {
		List<String> acceptableFileExtList = new List<String>{'png','jpeg'};
        
        for(String ext : acceptableFileExtList)
        {
            if(params.contains(ext)) return true;
        }
		return false;
            
    }
    
    private void sendMail(String address) {

        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {address};
        mail.setToAddresses(toAddresses);
        mail.setSubject('Wrong Filetype');
        mail.setPlainTextBody('File Type is not accepted and to plesse resend their email with accepted File Type (.jpg, .png, .pdf)');

        Messaging.SendEmailResult[] results = Messaging.sendEmail(
                                 new Messaging.SingleEmailMessage[] { mail });
        
    }
}

 
Bob James 8Bob James 8
Thank you Abdul. I'll give it a try.
Abdul KhatriAbdul Khatri
Hi Bob,

Did you give a try?
Bob James 8Bob James 8
Hello Abdul, I have not, been busy with many other things. I do have a workaround that deletes unwanted files on a weekly basis, so it’s not an immediate problem while that’s in place. Thank you, [cid:image001.png@01D7F35C.AD733B70] Bob James | Systems Administrator American Furniture Warehouse 4700 S Power Rd, Gilbert, AZ 85296 O: 480 500-4161 x8461 | E: bjames@afwonline.com