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
Laura Boyd 18Laura Boyd 18 

Add today's date to Visualforce page

Hi

I have a controller (shown below) which generates a PDF and sends it by email.

I am setting the file name in the line of:
attach.setFileName('POFile.pdf');

All I want to do is add todays date to the the filename which I have tried as:
attach.setFileName('POFile{!Today()}.pdf');

All is does is bolt the text of "{!Today()} to end of the filename rather than inserting the date.

Any ideas where I am going wrong?

 
public with sharing class SendPDFbyEmail {

public SendPDFbyEmail (ApexPages.StandardController controller) {
}

//Set variable values
public String EmailAddress {get;set;}
public String eSubject{get;set;}
public String eBody{get;set;}



//Start mail process and set criteria for PDF
public PageReference SendEmail() {

Messaging.SingleEmailMessage email=new Messaging.SingleEmailMessage();
PageReference pdfGeneration=Page.Purchase_Order_PDF;
pdfGeneration.SetRedirect(true);
Blob b=pdfGeneration.getContent();

//Create attachment and set criteria
Messaging.EmailFileAttachment attach=new Messaging.EmailFileAttachment();
attach.setFileName('PoFile"{!Today()}".pdf');
attach.setBody(b);

    email.SetSubject(eSubject);
    email.SetPlainTextBody(eBody);
    email.setToAddresses(new String[] { EmailAddress });
    email.setFileAttachments(new Messaging.EmailFileAttachment[] {attach});

    
//Now Sending the Email
Messaging.SendEmailResult[] r=Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
    
    return null;
    
    }
    }


 
Best Answer chosen by Laura Boyd 18
Amit Chaudhary 8Amit Chaudhary 8

Try like below

attach.setFileName('PoFile'+System.today()+'.pdf');

Please try to update your code like below
public with sharing class SendPDFbyEmail {

public SendPDFbyEmail (ApexPages.StandardController controller) {
}

//Set variable values
public String EmailAddress {get;set;}
public String eSubject{get;set;}
public String eBody{get;set;}



//Start mail process and set criteria for PDF
public PageReference SendEmail() {

Messaging.SingleEmailMessage email=new Messaging.SingleEmailMessage();
PageReference pdfGeneration=Page.Purchase_Order_PDF;
pdfGeneration.SetRedirect(true);
Blob b=pdfGeneration.getContent();

//Create attachment and set criteria
Messaging.EmailFileAttachment attach=new Messaging.EmailFileAttachment();
attach.setFileName('PoFile'+System.today()+'.pdf');
attach.setBody(b);

    email.SetSubject(eSubject);
    email.SetPlainTextBody(eBody);
    email.setToAddresses(new String[] { EmailAddress });
    email.setFileAttachments(new Messaging.EmailFileAttachment[] {attach});

    
//Now Sending the Email
Messaging.SendEmailResult[] r=Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
    
    return null;
    
    }
    }

Let us know if this will help you

 

All Answers

Amit Chaudhary 8Amit Chaudhary 8

Try like below

attach.setFileName('PoFile'+System.today()+'.pdf');

Please try to update your code like below
public with sharing class SendPDFbyEmail {

public SendPDFbyEmail (ApexPages.StandardController controller) {
}

//Set variable values
public String EmailAddress {get;set;}
public String eSubject{get;set;}
public String eBody{get;set;}



//Start mail process and set criteria for PDF
public PageReference SendEmail() {

Messaging.SingleEmailMessage email=new Messaging.SingleEmailMessage();
PageReference pdfGeneration=Page.Purchase_Order_PDF;
pdfGeneration.SetRedirect(true);
Blob b=pdfGeneration.getContent();

//Create attachment and set criteria
Messaging.EmailFileAttachment attach=new Messaging.EmailFileAttachment();
attach.setFileName('PoFile'+System.today()+'.pdf');
attach.setBody(b);

    email.SetSubject(eSubject);
    email.SetPlainTextBody(eBody);
    email.setToAddresses(new String[] { EmailAddress });
    email.setFileAttachments(new Messaging.EmailFileAttachment[] {attach});

    
//Now Sending the Email
Messaging.SendEmailResult[] r=Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
    
    return null;
    
    }
    }

Let us know if this will help you

 
This was selected as the best answer
Laura Boyd 18Laura Boyd 18
That's great Amit, thanks for your help.