2

I am creating a VF page where I have to select date and filter data and using button I need to send the another VF page as PDF with same data in email as attachment. When I selecting Date from 1st VF page and on click button passing the selected date and record Id to Another VF page (Which I need to send as pdf). I am getting the Data in 2nd page constructor but once I am sending the same page using single messaging its throwing error saying that 'List has no row fro assignment.'

Using below code I am passing Id and Dates.

Pagereference pr = Page.RenewalProductPageTemplate;
    pr.setRedirect(true);
    pr.getParameters().put('searchFilter1', rpid);
    pr.getParameters().put('searchFilter2', test1);
    pr.getParameters().put('searchFilter3', test3);
    return pr;

Controller of 2nd Page.

    rpid  = ApexPages.currentPage().getParameters().get('searchFilter1'); 
    test1 = ApexPages.currentPage().getParameters().get('searchFilter2');
    test2 = ApexPages.currentPage().getParameters().get('searchFilter3');


    rRecord = [Select id,name,Family__r.name ,Family__r.Email_ID__c from Renewal__c where Id =:rpid]; 

    rpRecord= [Select id,name,Company__r.name,Client_Name__c,Client_Name__r.name,Product__r.name,
                    Renewal__r.Family__r.name, Premium_Amount__c,Premium_Due_Date__c,Plan__c from Renewal_Product__c 
                    where  Renewal__c=:rpid];

    system.debug('***'+rRecord+'---'+rpRecord);
    sendClientRecord();

}  

In above 2nd page (Which I need to send as PDF) I am geeitng data(rpid, test1, test2). And I am geeting the debug. But once calling [sendClientRecord();] Method for sending mail the same constructor not working.

Below is sendClientRecord() Method code piece.

 public void sendClientRecord(){ ;

    PageReference pdf;
    pdf = Page.RenewalProductPageTemplate;
    pdf.getParameters().put('id',rpid);
    pdf.getParameters().put('test1var',test1);
    pdf.getParameters().put('test2var',test2);

    pdf.setRedirect(true);  

    Blob pdfBody;
    if(!test.isRunningTest()){
        pdfBody = pdf.getContentAsPDF();
    }
    else
    {
        pdfBody = blob.valueof('TEST');
    }
    list<Attachment> at=[select id,name,body from Attachment where ParentId=:rpid];
    String name;
    if(at.size()!=0)
        name=rRecord.Name+ '-' +'RenewalProduct'+at.size()+'.pdf';
    else
    {
        name=rRecord.Name + '-' +'SendRenProdEmailwithButtonpage.pdf';
    }
    Attachment attach = new Attachment();
    attach.ParentId =rpid;
    attach.name = name;
    attach.body = pdfBody ;
    System.debug('tId '+rpid);
    INSERT attach;

    Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();

    List<String> liststr = new List<String>();

    liststr.add('[email protected]');

    system.debug('***222'+liststr);

    email.setSubject('');
    email.setToAddresses(liststr);
    email.setToAddresses(liststr); //Used SOQL to retrieve addresses in the address
    email.setPlainTextBody('Please find attached PDF');
    // rpRecord. = true;

    Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
    efa.setFileName(attach.Name);
    efa.setBody(pdfBody);
    email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});

    Messaging.SendEmailResult [] r = Messaging.SendEmail(new Messaging.SingleEmailMessage[] {email}); 

    PageReference pageRef = new PageReference('/'+rpid);
    pageRef.setRedirect(true);


   // return pageRef;

} 

Here I am not getting any value. once calling page from this method. Please Help me in this!

1 Answer 1

2

This may have been included in the code you didn't post, but both rRecord and rpRecord are of type list<Renewal__c>. It would seem to me that if you use Limit 1 on both of those queries, they can just be Renwal__c records and won't need to be lists. Otherwise, your code will need to get the values of rRecord[0] and rpRecord[0] which could possibly be part of the problems you're having.

Unless you're wanting to preview the email, you'll want to use the original button to send the email with the attachments because neither your email or the attachment should have any active content on them, especially if they're both being PDF'd. The attachment in particular must be created using HTML 4.1 and CSS 2.

EDIT

I also see where it appears that you've declare a VOID method yet are trying to return a redirected PageReference. A VOID method can't return anything!

You must log in to answer this question.

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