Hi I am getting wrong extension or type for word document from ContentVersion in mail attachment for pdf its coming correct but for its not .docx for word I debug its coming WORD_X which is wrong and I am unable to open my attachment below is my class

public class FrContractLegalApprovalMail {

    public static void sendContractEmail(List<id> contractIds){
      Id userId;
      string emailTo;
      Blob b;
      Messaging.SingleEmailMessage semail= new Messaging.SingleEmailMessage();
      EmailTemplate emailTemp = [SELECT Id FROM EmailTemplate WHERE DeveloperName ='Fr_Contract_Approval_Mail' limit 1];
      Contract__c Contr = [select id,name,Contract_Attached__c,Contact__c,L_Person_Email__c,Customer_Account__c,Status__c,OwnerId,owner.email from Contract__c where id In :contractIds];

       if(Contr.Legal_Person_Email__c!=null && Contr.OwnerId!=null){
       List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();   
        for(Id cId :contractIds){    
           for(ContentDocumentLink contentLink : [SELECT Id, ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId = :cId limit 1]){
               for (ContentDocument contentDocument : [SELECT Id, LatestPublishedVersionId FROM ContentDocument WHERE Id = :contentLink.ContentDocumentId limit 1]){
                   for(ContentVersion document : [SELECT Id, Title, VersionData,FileType FROM ContentVersion WHERE Id = :contentDocument.LatestPublishedVersionId limit 1]){
                      system.debug('** Inside Contract Attachment: ' +document);

                      Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();
                      attachment.setFileName(document.Title + '.' + document.FileType);

           list<String> emailIds= new list<String>();
           system.debug('**Contract emailIds: ' +emailIds);
           semail.saveAsActivity = false;
           //semail.setFileAttachments(new Messaging.EmailFileAttachment[]{fileAttachments});
       Messaging.sendEmail(new Messaging.SingleEmailMessage[]{semail});

In this line you use the FileType to use as the files extension, while you want the FileExtension field.

attachment.setFileName(document.Title + '.' + document.FileType);

If your file is over 3 mb then the standard setting is to send is as an HMLT download link. If you don't want this then this help article will explain how to change that:

In Salesforce Classic

Click on Setup.
In the Quick Find box, enter email attachment.
Click on Email Attachments under Email Administration. Make a selection on the right side under 'Default Behavior for Email Attachments.'
Click on Save.

In Lightning Experience

Click on the gear icon and click Setup.
In the Quick Find box, enter email attachments.
Click on Email Attachments under Email.
Make a selection on the right side under 'Default Behavior for Email Attachments.' ​
Click on Save.

  • Hi Kasper thanks for your time and answer I tried FileExtension as well its still sending docx file as .html file May 13 at 6:57
  • Hi @priyadhawan, that's odd. I don't see anything in your code that should cause that. If you query the ContentDocuments, does it return the correct FileType? Does the file name contain a . in itself perhaps? Does the file open correctly if you change it to an docx extension?
    – Kasper
    May 13 at 21:43
  • Hi @Kasper I found that salesforce sends over 3 mb file as HTML link i didn't notice below 3 mb its working fine so Let me know if you know any solution to it otherwise I am good and Thanks for your help much appreciate your time May 15 at 10:18
  • @priyadhawan Oh I forgot about that. Luckily you can change that. I've edited my answer to include that.
    – Kasper
    May 15 at 15:56

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.