I am trying to display the record name, which is specifically, the standard field Name (It has a label Book Number) in my PDF. Every time a new record is inserted, it must automatically send an email with a PDF attachment and must be automatically saved to the notes and attachments related list. I have achieved these two requirements, and all the other fields are displayed. It's just that the record name is not fetched, thus not including it in the PDF.
Here is the controller:
public class attachmentExample{
public Id bookId{get;set;}
public Id parentId{get;set;}
public Boolean Page1{get;set;}
public Boolean Page2{get;set;}
public Book__c book{get;set;}
public attachmentExample(ApexPages.StandardController controller)
{
Page1 = true;
Page2 = false;
book = new Book__c();
}
public pagereference sendAttach()
{
Book__c book1 = new Book__c();
book1.Id = book.Id;
book1.Title__c = book.Title__c;
book1.Author__c = book.Author__c;
book1.Price__c = book.Price__c;
book1.Publisher__c = book.Publisher__c;
insert book1;
parentId = book1.Id;
//System.assertequals(book1 + 'test','');
Page1 = false;
Page2 = true;
Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
PageReference pref = page.BookDetails;
pref.getParameters().put('id',(String)book.Id);
pref.setRedirect(true);
Blob b = pref.getContent();
attach.setFileName('BookDetails.pdf');
attach.setBody(b);
semail.setSubject('Book details');
String[] sendTo = new String[]{'samplemail@test.com'};
semail.setToAddresses(sendTo);
semail.setPlainTextBody('Please find the attached book details');
semail.setFileAttachments(new Messaging.EmailFileAttachment[]{attach});
Messaging.sendEmail(new Messaging.SingleEmailMessage[]{semail});
Attachment appPdf = new Attachment();
appPdf.Body = Page.BookDetails.getContentAsPdf();
appPdf.ContentType = '.pdf';
appPdf.Name = 'Book Details';
appPdf.ParentId = parentId;
insert appPdf;
parentId = null;
return null;
}
}
And here are my two visualforce pages:
BookPage
<apex:page standardController="Book__c" extensions="attachmentExample">
<apex:form >
<apex:pageBlock rendered="{!Page1}" >
<div>
<apex:commandButton value="Save" action="{!sendAttach}"/>
<apex:commandButton value="Cancel"/>
</div>
<apex:pageBlockSection >
<apex:inputField value="{!book.Name}"/><br/>
<apex:inputField value="{!book.Title__c}"/><br/>
<apex:inputField value="{!book.Author__c}"/><br/>
<apex:inputField value="{!book.Price__c}"/><br/>
<apex:inputField value="{!book.Publisher__c}"/>
</apex:pageBlockSection>
</apex:pageBlock>
<apex:pageBlock rendered="{!Page2}">
<apex:outputLabel >New Book has been inserted. Please check your email to confirm details.</apex:outputLabel>
</apex:pageBlock>
</apex:form>
</apex:page>
BookDetails
<apex:page standardController="Book__c" extensions="attachmentExample" renderAs="pdf" docType="html-5.0">
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection columns="1">
<apex:outputText Value="{!book.Name}"/>
<apex:outputText Value="{!book.Title__c}"/>
<apex:outputText Value="{!book.Author__c}"/>
<apex:outputText Value="{!book.Price__c}"/>
<apex:outputText Value="{!book.Publisher__c}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Thanks in advance.
Edited controller:
public class attachmentExample{
public Id bookId{get;set;}
public Id parentId{get;set;}
public Boolean Page1{get;set;}
public Boolean Page2{get;set;}
public Book__c book{get;set;}
public attachmentExample(ApexPages.StandardController controller)
{
Page1 = true;
Page2 = false;
book = new Book__c();
}
public pagereference sendAttach()
{
//Book__c book1 = new Book__c();
Id bookId = ApexPages.currentPage().getParameters().get('id');
if(bookId != null){
book = [SELECT Id, Name, Title__c, Author__c, Price__c, Publisher__c FROM Book__c WHERE Id =: bookId];
}
else{
book = new Book__c();
book.Id = bookId;
book.Title__c = book.Title__c;
book.Author__c = book.Author__c;
book.Price__c = book.Price__c;
book.Publisher__c = book.Publisher__c;
insert book;
parentId = book.Id;
//System.assertequals(book1 + 'test','');
Page1 = false;
Page2 = true;
}
Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
PageReference pref = page.BookDetails;
pref.getParameters().put('id',(String)book.Id);
pref.setRedirect(true);
Blob b = pref.getContent();
attach.setFileName('BookDetails.pdf');
attach.setBody(b);
semail.setSubject('Book details');
String[] sendTo = new String[]{'samplemail@test.com'};
semail.setToAddresses(sendTo);
semail.setPlainTextBody('Please find the attached book details');
semail.setFileAttachments(new Messaging.EmailFileAttachment[]{attach});
Messaging.sendEmail(new Messaging.SingleEmailMessage[]{semail});
Attachment appPdf = new Attachment();
appPdf.Body = Page.BookDetails.getContentAsPdf();
appPdf.ContentType = '.pdf';
appPdf.Name = 'Book Details';
appPdf.ParentId = parentId;
insert appPdf;
parentId = null;
return null;
}
}