3

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

        /apex/SDOC__SDCreate1?id={!RecordId}&Object=Claim__c&doclist=Templateid

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');
   pg.setredirect(true);
   return pg;
  } 

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

   pg:System.PageReference[/apex/SDOC_SDCreate1?doclist=a079000000O8IbL&id=a089000000gjZUxAAM&Object=Claim__c]

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

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

2 Answers 2

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

trigger AfterUpdate on Account (after update) {

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

for(Account acc: Trigger.new){
    accntIdList.add(acc.id);
}

FutureAttachmentCreator.createAttachment(accntIdList);
}

Future Method

global class FutureAttachmentCreator{

@Future(callout=true)
public static void createAttachment(list<id> accntIdList){
   HttpRequest req = new HttpRequest();
   req.setEndpoint('https://'+URL.getSalesforceBaseUrl().getHost()+'/services/apexrest/createpdf/');
   req.setMethod('POST');
   req.setBody('{"accountIdList":'+JSON.serialize(accntIdList)+'}');
   req.setHeader('Authorization', 'Bearer '+ sessionId);
   req.setHeader('Content-Type', 'application/json');
   Http http = new Http();
   HTTPResponse res = http.send(req);
}

}

REST API

@RestResource(urlMapping='/createpdf/*')
global with sharing class AddPDFtoRecordREST{

  @HttpPost
    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
            pdfPage.getParameters().put('id',accntId);
            Attachment attachment = new Attachment();
            Blob body;
            if(!test.isRunningTest()){
                body = pdfPage.getContent();
            }else{
                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
            attachmentList.add(attachment);
         }
         //insert the list of attachment
         insert attachmentList;
    }
}
0

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

Update:

 PageReference pg = new PageReference('/apex/SDOC__SDCreate1');
 pg.getParameters().put('id',cid);

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');
7
  • Tried the same but still no luck. Is this the correct way to pass the parameters as i mentioned in code.
    – sunny
    Commented 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
    Commented Sep 26, 2014 at 14:20
  • No that didnt work either
    – sunny
    Commented Sep 26, 2014 at 15:05
  • @sunny what is your debug saying then?
    – Phil B
    Commented Sep 26, 2014 at 16:45
  • Same as previously i have updated in my question.
    – sunny
    Commented 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 .