0

I have the following email service that works to create the custom object record just fine. However the file is not being created form the attachment on the incoming email. I see in the logs in the developer console that the service knows about the file as I see the file name and type and etc in the logs, but I only see one DML statement at the end with no errors in the logs so I know it is not creating the file based on that as well as the file not related to the record being created or in files as all.

Can someone please look over my code below and give me any help in what to change or refactor to make this work? The email service is set to accepts all attachments.

Global class createPOTicketEmail implements Messaging.InboundEmailHandler {

    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope){
        
        Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
        
        Contact conFinal = [SELECT Id, Email FROM Contact WHERE Email =: envelope.fromAddress LIMIT 1];
        
        IF(conFinal != null){
        Ticket__c ticket = new Ticket__c();
        ticket.Name = 'PO Email Request';
        ticket.Contact__c = conFinal.Id;
        ticket.Description__c = email.plainTextBody;
        ticket.Status__c = 'New';
        ticket.Board__c = 'Inside Sales';
        ticket.Priority__c = '1 - Critical';
        ticket.Type__c = 'Process Order / Purchase Order';
        insert ticket;
        }else{
        Ticket__c ticket = new Ticket__c();
        ticket.Name = 'PO Email Request';
        ticket.Description__c = envelope.fromAddress + ' - ' + email.plainTextBody;
        ticket.Status__c = 'New';
        ticket.Board__c = 'Inside Sales';
        ticket.Priority__c = '1 - Critical';
        ticket.Type__c = 'Process Order / Purchase Order';
        insert ticket; 
        
        List<ContentVersion> cvList = new List<ContentVersion>();
        List<ContentDocumentLink> cdList = new List<ContentDocumentLink>();
            for(Messaging.InboundEmail.BinaryAttachment binAttach : email.binaryAttachments){
                ContentVersion conVersion = new ContentVersion();
                conVersion.Title = binAttach.fileName;
                conVersion.VersionData = binAttach.Body;
                conVersion.PathOnClient = '/' + binAttach.fileName;
                cvList.add(conVersion);
            }
            insert cvList;
            cvList = [SELECT Id, ContentDocumentId FROM ContentVersion WHERE Id IN :cvList];
            for(ContentVersion cv : cvList){
                ContentDocumentLink cl = new ContentDocumentLink();
                cl.ContentDocumentId = cv.ContentDocumentId;
                cl.LinkedEntityId = ticket.Id;
                cl.ShareType = 'V';
                cl.Visibility = 'AllUsers';
                cdList.add(cl);
            }
            insert cdLIst;
        }

        return result;
    }
    
}
1
  • not all attachments come in as binary attachments; you can also receive textAttachments and you're not handling those
    – cropredy
    Feb 12 '21 at 19:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.