1

How can we send PDF or MS WORD file via salesforce after inserting in it values from my user object.

Example: Sending each user (employee) a Salary certificate containing all his information

5
  • Are you doing this in batch apex? @KawtarBRAZI Commented Apr 14, 2014 at 16:24
  • 3
    Kawtar -- you might look at Conga Composer which will construct word/pdf/excel/ppt files as a combination of templates + merge fields. Product isn't very expensive and the vendor has superb support
    – cropredy
    Commented May 14, 2014 at 16:59
  • Drawloop is another paid option that works well. Commented May 14, 2014 at 17:46
  • If you want to retrieve the value using SOQL for the object that you just inserted in the same transaction ,you wont able to because the value not yet committed.The alternative is - using the page parameter or split the transaction.save the object in another method.return to page , and then send the email by calling another method upon button click.The easier way is to put the new value in page parameter.
    – unidha
    Commented Nov 3, 2016 at 3:29
  • This post appears to be abandoned.
    – Adrian Larson
    Commented Feb 9, 2018 at 2:09

3 Answers 3

0

You can attach those files as email attachment and then send to user:

// fetch attachments OR Same way you can query Document object
   List<Attachment> attList = [SELECT id, Name, body, ContentType FROM Attachment WHERE Name = : employeeName];

   // List of attachments handler
   Messaging.EmailFileAttachment[] efaList = new Messaging.EmailFileAttachment();
   for(Attachment att : attList)
   {
     // Create the email attachment
     Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
     efa.setFileName(att.Name);
     efa.setBody(att.body);
     efa.setContentType(att.ContentType);
     efa.setInline(false);
     efaList.add(efa);
   }

    // Attach files to email instance
    email.setFileAttachments(efaList);
6
  • You've got here sending the file as an attachement! But how can I manipulate my file? any idea?
    – LoveLace
    Commented Apr 14, 2014 at 14:27
  • Have read of this link, it may shed some light: salesforce.com/us/developer/docs/pages/Content/… Commented Apr 14, 2014 at 14:38
  • @kawtarbrazi you can't manupilate document content instead you can generate new document with update value.
    – Ashwani
    Commented Apr 14, 2014 at 15:05
  • I've created a HTML document where I can map between fields I named on it and the user field value! and I generating PDF file via Apex, I have a limited caracters comment here, I'll post the code in other question
    – LoveLace
    Commented Apr 14, 2014 at 15:13
  • @kawtarbrazi you can assign value by using parameters if redirecting t other page or if using same controller you can assign variables in apex controller and generate pdf from it.
    – Ashwani
    Commented Apr 14, 2014 at 15:24
0

If you have a look here: Insert Attachment into Visualforce Email Template

You can see an example of a Visualforce email template with an attachment. I think this would personally be the best way to do it and the VF templates are pretty simple to set up :)

0

Hi you can create a custom visualforce page and render it as PDF. in this visualforce page you'll create PDF from your user object based on if provided in URL. Let me know if you need help with this part. If you are able to create a visualforce page that renders correct data. Rest is simple

<apex:page standardController="Account" renderAs="pdf" applyBodyTag="false">

Reference https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_quick_start_renderas_pdf.htm

You can store it as document before you send it to user.

PageReference Pg = Page.YouCustompage;  
pg.getParameters().put('ud',userId);
Blob pdf1 = pg.getContent();

Now you can use this Blob to create a attachment set attachment.Body = pdf1; or save it wherever you want.

Please let me know if i am unclear.

Not the answer you're looking for? Browse other questions tagged .