function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AbAb 

converting report in to PDF in Apex class

Hello,

I am using joined report

Is it possibel to take a screenshot of a report (or converting report in to pdf)and get it in apex.

I will later use it to email by scheduler

thank you
Best Answer chosen by Ab
Gyanender SinghGyanender Singh
Hi Sandrine,

It is not possible to convert report into pdf, in place of this you can create vf page same as report and shedule the vf page by using the code:


public class EmailReport implements Schedulable {
    public void execute(SchedulableContext sc) {
        List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
        Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
        attach.setContentType('application/pdf');
        attach.setFileName('Report.pdf');
        pagereference Pg = Page.PDFPage;
        Blob body = pg.getcontentAsPdf();
        attach.Body = body;
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setToAddresses(new String[] { 'magulancse24@gmail.com' });
        mail.setSubject('PDF Generation');
        mail.setHtmlBody('PFA');
        mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach });    
        mails.add(mail); 
       
        if(!mails.isEmpty()) {
            Messaging.SendEmail(mails);
        }
    }

Please let me know if this is working for your problem.

Thanks 
Gyani

All Answers

Gyanender SinghGyanender Singh
Hi Sandrine,

It is not possible to convert report into pdf, in place of this you can create vf page same as report and shedule the vf page by using the code:


public class EmailReport implements Schedulable {
    public void execute(SchedulableContext sc) {
        List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
        Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
        attach.setContentType('application/pdf');
        attach.setFileName('Report.pdf');
        pagereference Pg = Page.PDFPage;
        Blob body = pg.getcontentAsPdf();
        attach.Body = body;
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setToAddresses(new String[] { 'magulancse24@gmail.com' });
        mail.setSubject('PDF Generation');
        mail.setHtmlBody('PFA');
        mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach });    
        mails.add(mail); 
       
        if(!mails.isEmpty()) {
            Messaging.SendEmail(mails);
        }
    }

Please let me know if this is working for your problem.

Thanks 
Gyani
This was selected as the best answer
Nash79Nash79

Hi Gyani,

Can you please explain the below:
1) PDFPage is the visualforcepage name?
2) Blob body = pg.getcontentAsPdf(); contentAsPdf is a standard function?

If possible, Please get me VF code as well.

Thanks in advance
Nagesh

 

Gyanender SinghGyanender Singh
Hi Nagesh,
  • PDFPage  is the vf page name i.e. this is page name that is use in making the report.
  • Blob body = pg.getcontentAsPdf() this funtion is standard funtion for getting pdf page.
Thanks,
Gyani
07503868174
 
Nash79Nash79
Thank you Gyanender for your quick reply. my use case is this. When an Account becomes inactive, I need close all open oppties and send an email with PDF to the sales and owners on Oppties. This PDF should  include all Open oppties as a list with status and few more fields. So please guide me how to approach on this. Sorry for asking too much. I am a new developer.
Regards
Nagesh M
Gyanender SinghGyanender Singh
Hi Nagesh,

For your requirement first you have to write a trigger in which take all the opportunities of that account and when the account becomes inactive then trigger fire and using that trigger close all open opportunities of that account.
Second thing you have to create a vf page and apex class in that vf page fetch all the closed opportunity of that account and using apex repeat tag create a table in the vf page.
Third thing create a send email method in your class and call the method using trigger.

Thanks & Regards
Gyani
07503868174
Nash79Nash79
Thank you Gyanender for your reply. I am really sorry that I am asking questions again and again. Based on your guidance this is what I understood (I gave sample names to address/link them together)
1) Create Trigger called "CloseOppties" on Account upon Account status changes to "Inactive".
2) Create a vf page called "ClosedOpptiesPDFReport" with Standard Controller=Opportunity and a extension controller = "closedOpptyapexclass"
3) closedOpptyapexclass should contain a method to send email. This same class should retrieve 3 email ids from each of the oppties of the Account. My org, there are 3 sales people on each Opportunity.
4) call the "closedOpptyapexclass" methods from "CloseOppties" Trigger.

My Questions:
1) The list of Oppties that have to included in the pdf report should come from extension controller = "closedOpptyapexclass" right?
2) extension controller = "closedOpptyapexclass" should get Account Id parameter from "CloseOppties" trigger right?
3) Generally when we launch a vf using buttons or so, we pass current record id as the input parameter. likewise how do we generate pdf for a certain Account to include only the Opptie of the Account in the trigger.

Please take your time. No rush at all. Once again, very very thank you Gyanender.
 
Gyanender SinghGyanender Singh
Hi Nagesh,

Please let me know your whole requirement and send all code you write.

Thanks 
Gyani
Nash79Nash79
Hi Gyanender, Actually I need to send the list of Oppties to VF. but in the below example, I am sending Account Id.
My Scenario: When Account is suspended, Put all the Oppties in a list, generate PDF and send it as an attachment via email to Few users on the Oppities.

1) Trigger: I need to Identify the Oppties whenever the Parent Account is suspended in the below code, I didn't put close statement to expire all Oppties. I have to gather the list keep it for Email and update the status to closed. But pdf should contain the oppties list with previous status. That's why i have to prepare the list and pass it to EmailClass.
trigger Account_Suspension on Account (after update)
 { 
       Account acc1 = Trigger.new[0];                        if(Trigger.oldMap.get(acc1.id).Account_Suspension__c ==null)// || Trigger.oldMap.get(acc1.id).Account_Suspension__c =='') && (Trigger.newMap.get(acc1.id).Account_Suspension__c =='Suspension Level 1' || Trigger.newMap.get(acc1.id).Account_Suspension__c =='Suspension Level 2'))           {               List<Oppties> expOpptiess = [SELECT Id, contact__r.Name, Additional_Contact_Name__r.Name,                                                       Primary_Sales_Contact__c, Status__c FROM Oppties WHERE  Status__c ='Approved' AND                                                       AccountId__c =: acc1.Id];                                                       
                         DummyTest.sendEmailToUsers(acc1.id);    // I actually want to send list of Oppties. at this       
           }          
  }

2) Email Class:
public class DummyTest {
@future
public static void sendEmailToUsers( Id acc){
    PageReference pdf = Page.Oppties_ExpReport;
    pdf.getParameters().put('id',(String)acc);
   pdf.setRedirect(true);

Blob b = pdf.getContent();
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
efa.setFileName('OpptiesExpired.pdf');
    efa.setBody(b);

 Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
    List<String> toAddresses = new List<String> ();
    toAddresses.add('nagesh@gmail.com');
 email.setSubject( 'This is really cool.' );
email.setToAddresses( toAddresses );
email.setPlainTextBody( 'i am going to send a good attachment.' );
 email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
         Messaging.SendEmailResult [] r =
          Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
    }
}

3) VF Page to list all the exp Oppties:
<apex:page standardController="Account"  extensions="AccountExpireExtn" renderAs="pdf">
     <apex:form >
              <h1>Congratulations</h1>
              This is your new Page: Oppties_ExpReport
     <body>
   <apex:dataTable value="{!listOppties}" var="dl">
         <apex:column value="{!dl.Name}"/>
          <apex:column value="{!dl.Status__c}"/>
         </apex:dataTable>
     <div style="padding-top:30px;line-height:24px;">
              <h8 style="font-size:17px;">Thanks and Regards</h8><br/>
              <span style="font-size:17px;font-weight:bold;font-family:Britannic Bold,serif;color:#92D050">Sales Team</Span>
          </div>  
  </body>         
     </apex:form>         
</apex:page>

4) vf controller class:
public class AccountExpireExtn {
    public List<Oppties> listOppties {get;set;}
    private final Account acct;
    
    public AccountExpireExtn(ApexPages.StandardController stdController) {
          this.acct = (Account)stdController.getRecord();
          listOppties = new List<Oppties>();
          listOppties =  [Select Id, Name, Oppty#, Status__c from  Oppties
                     where Active_Account__c=:acct.Id AND Status__c ='Approved'];  //
  } }

For now, as per suggestion from one of my frnd, I created vf page on Account instead of Oppty and prepared the above controller class. But I have to send the list which I identified in Trigger class.

Thanks for your help
Nagesh
KushiKushi
Hi Gyani,

I m actually working on converting reports to pdf but I also want to display that pdf in a landscape version..Is that possible?

Thanks,
Kushi