1

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;




    }
}
2

Your BookDetails page invokes the constructor of the extension, which only creates a new instance of Book__c - book = new Book__c();. At this point book.Name will be null, it hasn't been set to anything.

In your constructor you need to get the ID parameter from the URL passed in from BookPage and query the record. So instead of:

book = new Book__c()

have

Id bookId = Apexpages.currentPage().getParameters().get('id');

if (bookId != null)
{
    book = [SELECT Id, Name, OTHERFIELDS FROM Book__c WHERE Id = :bookId];
}
else
{
    book = new Book__c();
}
  • I have followed your suggestion, but still it returns blank book number. Maybe, there's something wrong with the way I have executed it on my controller. Can you check my code? I've already edited my post and inserted the modified version of the controller there. – asdfg Mar 11 '16 at 3:54
  • You need this piece of code inside the constructor of your class, not in the sendAttach() method – Boris Bachovski Mar 11 '16 at 4:06
  • Hi, code's inside the constructor of the class already, yet book number is still blank. – asdfg Mar 11 '16 at 5:41
  • Are the other fields showing? – Boris Bachovski Mar 11 '16 at 5:48
  • yes, fields for title, author, price and publisher are displaying in the pdf – asdfg Mar 11 '16 at 5:49

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.