1

I have a problem. I need to get pdf and attach it to object. I create object (and for test I execute query - it's ok), get ID and put it to page as parametr:

PageReference pdfPage = Page.ConsolidatedPDF;
pdfPage.getParameters().put('cInvoice', consId);
Blob pdfBlob = pdfPage.getContentAsPDF();

then on controller of page ConsolidatedPDF:

consolidatedInvoiceTmp = [SELECT Id, Name FROM someObject__c WHERE Id =: consolidatedId];

consolidatedInvoiceTmp just empty but object with ID exist.

Can you help me?

public RecordAndPresentController(ApexPages.StandardSetController setController) {
    selectRecords = setController;
    selectRecords.setPageSize(2000);

    PageReference currentPage = ApexPages.currentPage();

    invoices = [
        SELECT id, Name, Status__c, Amount__c, Hours__c, RecordTypeId, Employee_Name__c, Period_Ending__c, Client__c,
                Client__r.AccountNumber
        FROM Invoice__c
        WHERE Id IN :((List<Invoice__c>)selectRecords.getSelected())
    ];

    //checkErrors();
}
public Invoice__c getConsolidatedInvoice() {
    String consolidatedRTId = getRecordTypeInvoiceId(CONSOLIDATED_RT);

    if (invoices.size() > 0 && !String.isEmpty(consolidatedRTId)) {
        Invoice__c cInvoice = new Invoice__c(RecordTypeId = consolidatedRTId, Hours__c = hours);
        insert cInvoice;

        for (Invoice__c inv : invoices) {
            inv.Parent__c = cInvoice.Id;
        }
        update invoices;

        return cInvoice;
    }

    return null;
}

public PageReference recordAction() {
    Invoice__c cInvoice = getConsolidatedInvoice();
    System.debug('straaangeeee ' + [select id, name FROM Invoice__c Where Id =: cInvoice.Id]);

    if (cInvoice != null) {
        attachPdfToInvoice(new List<Id> {cInvoice.Id});
    }
    return null;
}

public void attachPdfToInvoice(List<Id> cInvIdList) {

    List<Attachment> attachmentList = new List<Attachment>();

    for (Id consId : cInvIdList) {
        PageReference pdfPage = Page.ConsolidatedPDF;
        pdfPage.getParameters().put('cInvoice', consId);
        Blob pdfBlob = pdfPage.getContentAsPDF();

        attachmentList.add(new Attachment(parentId = consId, name = 'test' + '.pdf', body = pdfBlob));
    }
    insert attachmentList;
}

And controller for ConsolidatedPDF page:

public String consolidatedId { get; set; }

public TestPDFController() {
    consolidatedId = ApexPages.currentPage().getParameters().get('cInvoice');
    getDataForPDFInvoice();
}

public void getDataForPDFInvoice() {
    List<Invoice__c> detailedInvoices;
    List<Invoice__c> consolidatedInvoiceTmp;
    consolidatedInvoiceTmp = new List<Invoice__c>();
    if (!String.isEmpty(consolidatedId)) {
        System.Debug('Id ' + consolidatedId);
        consolidatedInvoiceTmp = [SELECT Id, Name FROM Invoice__c WHERE Id =: consolidatedId];

        System.Debug('result ' + consolidatedInvoiceTmp);
    }
}
  • Check if you are querying right object. – Raul Oct 25 '16 at 8:20
  • Yes, object is right. Also if this ID put in parametrs of SOQL and start the process once again - I got objects (i.e. use Id from previously iteration). – Dmitry Dubonos Oct 25 '16 at 8:28
  • are you using a standard controller? – Raul Oct 25 '16 at 8:34
  • No, I use custom controller But I use a standard controller on page where I set param to "ConsolidatedPDF" page – Dmitry Dubonos Oct 25 '16 at 8:46
  • 1
    Can you share your controller code? It appears you're using the wrong parameter to set the consolidatedId – Fernando Gavinho Oct 25 '16 at 8:51
0

Try this

PageReference pdfPage = Page.ConsolidatedPDF;
pdfPage.getParameters().put('Id', consId);
Blob pdfBlob = pdfPage.getContentAsPDF();
  • Thank you for your help Sharat. But it not helps – Dmitry Dubonos Oct 25 '16 at 8:47
  • Please share your controller code. – C. Praveenkumar Oct 25 '16 at 8:58
  • @C.Praveenkumar please see code – Dmitry Dubonos Oct 25 '16 at 9:01

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.