I have a requirement to generate invoices(PDF Files) in my application. So i installed app exchange app "S-Docs" to get it done. But the problem is every time i need to click on the button to generate the PDF file. So i thought of automating it with out click of button. The code present in button was as below


Now i have both recordid and templateid. So whenever i insert a record in Invoice object i am using a trigger to call Page Reference method of a class. Now i am struck at how to pass the above code in Page Reference method. Can anyone help me on this.

The code i tried so far is

   public PageReference generate(Id cid){
   PageReference pg = new PageReference('/apex/SDOC__SDCreate1?id='+cid);
   pg.getParameters().put('Object', 'Claim__c');
   pg.getParameters().put('doclist', 'a079000000O8IbL');
   return pg;

When i treid to print pg variable using debug statements what i look is below


How can i correct the order of passing as i need it?

  • What have you tried so far with PageReference?
    – BarCotter
    Sep 26, 2014 at 13:17
  • @BarCotter : please check it now
    – sunny
    Sep 26, 2014 at 13:45
  • 1
    Using PageReferences this way won't work within a trigger. Sep 26, 2014 at 14:22

2 Answers 2


You can't redirect to a page from a trigger, if that is what you want. But you can insert the pdf generated from a VF page into an attachment.

This too isn't all that straight forward.

Your trigger would need to call a future method, which in turn makes a callout to a web service that will generate and insert the attachment. ( If you haven't done this before it might seem like a lot of work to accomplish a little thing, but unfortunately that is to overcome the limitation of Salesforce)

Below is some parts of code which I tried in my org some months back. You can make changes to it according to your requirement.


trigger AfterUpdate on Account (after update) {

List<id> accntIdList = new List<Id>();

for(Account acc: Trigger.new){


Future Method

global class FutureAttachmentCreator{

public static void createAttachment(list<id> accntIdList){
   HttpRequest req = new HttpRequest();
   req.setHeader('Authorization', 'Bearer '+ sessionId);
   req.setHeader('Content-Type', 'application/json');
   Http http = new Http();
   HTTPResponse res = http.send(req);



global with sharing class AddPDFtoRecordREST{

    global static void doPost(list<String> accountIdList) {
       list<attachment> attachmentList = new list<attachment>();
        for(String accntId: accountIdList){
            //VF page that renders as PDF
            pageReference pdfPage = Page.accntRefPDFPage;
            // passing the parameter to the page
            Attachment attachment = new Attachment();
            Blob body;
                body = pdfPage.getContent();
                body=blob.valueOf('test content');
            attachment.Body = body;
            attachment.Name = 'Ref Pdf '+System.now()+'.pdf';
            attachment.IsPrivate = false;
            attachment.ParentId = pdfPage;//This is the record to which the pdf will be attached
         //insert the list of attachment
         insert attachmentList;

It looks like you're missing the = after id. It should be: ?id='+cid);


 PageReference pg = new PageReference('/apex/SDOC__SDCreate1');

Update 2 to enforce order:

The order really shouldn't matter but if you really need it in that order try this:

PageReference pg = new PageReference('/apex/SDOC__SDCreate1?id='+cid+'&Object=Claim__c&doclist=a079000000O8IbL');
  • Tried the same but still no luck. Is this the correct way to pass the parameters as i mentioned in code.
    – sunny
    Sep 26, 2014 at 14:10
  • @sunny I added a 2nd suggestion. Try adding the id to the URL just like the other URL parameters.
    – Phil B
    Sep 26, 2014 at 14:20
  • No that didnt work either
    – sunny
    Sep 26, 2014 at 15:05
  • @sunny what is your debug saying then?
    – Phil B
    Sep 26, 2014 at 16:45
  • Same as previously i have updated in my question.
    – sunny
    Sep 26, 2014 at 16:49

You must log in to answer this question.

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