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
ethanoneethanone 

InboundEmailHandler to Insert Attachment to custom object

I want to send an email and create a new record for a custom object, lda_Request__c. If there is an attachment on the email, i want it to be associated with the new record. The code below is 100% covered and tests appear to create the ContentVersion object, but when I send a real email with a real attachment, the new record is created, but there is no sign of the new ContentVersion record nor the ContentDocumentLink record and I get no errors. Can anyone please help?
 
global class ResearchRequestByEmail implements Messaging.InboundEmailHandler {
 
    global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, Messaging.InboundEnvelope env){
        
        // Create an InboundEmailResult object for returning the result of the Apex Email Service
        Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
        
        String PTSubject = '';
        PTSubject = email.subject;
        string myHTMLBody = '';
        myHTMLBody = email.htmlBody;
        
        // Get User Id
        User Apprvr = [SELECT Id, Name FROM User WHERE Email = :email.fromAddress LIMIT 1];
        
        // Create new Request
        lda_Request__c request = new lda_Request__c();
        request.Approver__c = Apprvr.Id;
        request.Request_Subject__c = PTSubject;
        request.Request_Details__c = myHTMLBody;
        request.Via_Email__c = True;
        request.Status__c = 'New';
        request.Property_Description_Method__c = 'Via Email';
        insert request;

        // Attachments using Content Version
        if (email.binaryAttachments != null){
            for (Messaging.InboundEmail.binaryAttachment binAttach :email.binaryAttachments){
                ContentVersion cv = new ContentVersion();
                cv.VersionData = binAttach.body;
                cv.Title = binAttach.fileName;
                cv.PathOnClient = binAttach.fileName;
                //cv.FirstPublishLocationId = request.Id;
                insert cv;
                
                cv = [select ContentDocumentId from ContentVersion where id = :cv.id limit 1];

                ContentDocumentLink cdl = new ContentDocumentLink();
                cdl.LinkedEntityId = request.Id;
                cdl.ContentDocumentId = cv.ContentDocumentId;
                cdl.ShareType = 'V';
                insert cdl;
            }
        }
        result.success = true;
        
        // Return the result for the Apex Email Service
        return result;
    }
}

 
Naval Sharma4Naval Sharma4

Hi Ethonone,

You need to set FirstPublicationId to a public folder. If you don't specify it then it goes to personal folder of the user.

Let me know if you need more help.

Thanks,
Naval

ethanoneethanone
Do you mean ContentVersion.FirstPublishLocationId (I don't see ContentVersion.FirstPublicationId)? Is there a default public folder? And I didn't see any examples setting it. Can you post one?
David Bingham 12David Bingham 12
@Ethanone, did you ever solve this? I'm trying to do the exact same thing. Could use some help.
ethanoneethanone
I didn't solve it, but I still need to. I was hoping a system update would make the solution more obvious, but frankly, I've not even reviewed this project since my last post.
David Bingham 12David Bingham 12
I actually solved it for myself. Check out my reply to my own thread I created here: If you swap my AP_Invoice__C stuff for your Request__c stuff it should work.. ? Also, my code assumes that there is an InboundEmailService set up. Let me know if you hit any walls and I can screenshot stuff for ya. https://success.salesforce.com/answers?id=9063A0000019UqOQAU