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
NasirNasir 

Integrating Email with Visualforce with attachments and PDF

Hi 

 

I have created a button on Account which is a Custom object'SendInvitation'.When i click that button a page appear where the user have to fill the 'To' and 'Subject' and 'body' of the Invitation.I have used Standardcontroller and two visualforce page.One contain the 'mail' section and other contain the 'PDF' page.In my PDF page i am using {!Account__c.Name} and {!Account__c.Visit_Date__c} which is not getting populated, because of the following error.VisualforceException: SObject row was retrieved via SOQL without querying the requested field: Account__c.Name

 

I am able to send email with the Attachments.But when i click the View button on the mail to see the PDF.The PDF page dosen't open.

 

In system log i am getting this Error:System.VisualforceException: SObject row was retrieved via SOQL without querying the requested field: Account__c.Name  and this error is comin in the line which is Bold( b= pdf.getContent();) in the controller.

 

All i want now that Account name and visit date should display

 

 

 

Below is the code

public  class SendInvitationByMail {
    public String subject { get; set; }
    public String body { get; set; }
    public Account__c acc{ get; set; }
    public Id idacc;
   
    
    public SendInvitationByMail(ApexPages.StandardController controller) {
    idacc =controller.getRecord().id;
    acc = [Select Name,Email__c,Subject__c,EmailBody__c,Visit_Date__c from Account__c where id=:idacc];
    System.debug('record:'+acc);

    }
 

    public PageReference send() {
    Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
    // Reference the attachment page and pass in the account ID
    PageReference pdf = Page.attachmentPDF;
    pdf.getParameters().put('id',(String)acc.id);
  
    pdf.setRedirect(true);
    // Take the PDF content
     Blob b ;
     String n;
      try
      {
        
        b= pdf.getContent();
       
        system.debug('Blob'+b);
        }
        catch(VisualforceException e)
        {
         b=Blob.valueOf('SObject row was retrieved via SOQL without querying the requested field: Account__c.Name');
        
        }
    // Create the email attachment
    Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
    efa.setFileName('attachment.pdf');
    efa.setBody(b);

    if(acc.Email__c == null){
     acc.Email__c.addError('Please fill the To with an email address');

    }
     String addresses;
    if(acc.Email__c!=null){
    addresses=acc.Email__c;
    }
    String[] toAddresses = addresses.split(':', 0);
  
    email.setSubject( subject );    
    email.setToAddresses(toAddresses);
    email.setPlainTextBody( body );
    
    email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});

    // Sends the email
    Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});

    

        return null;
    }


}

 

 

 

 

 

 

 

 

Actually In my PDF visualforce page i am using {!Account__c.Name} and {!Account__c.Visit_Date__c} .

<apex:page standardController="Account__c" extensions="SendInvitationByMail" sidebar="false">

<apex:pageBlock tabStyle="Account" title="Send Invitation">

<apex:panelGrid columns="2">
<apex:form >
<apex:outputLabel value="To :"></apex:outputLabel> &nbsp;
<apex:inputField value="{!acc.Email__c}"/><br/><br />
<apex:outputLabel value="Subject :"></apex:outputLabel>&nbsp;
<apex:inputText value="{!subject}" id="Subject" maxlength="80"/>
<br /><br />
<apex:outputLabel value="Body" for="Body"/>:<br />
<apex:inputTextarea value="{!body}" id="Body" rows="10" cols="80"/>
<br /><br /><br />
<apex:commandButton value="Send Invitation" action="{!send}"/>
          

</apex:form>
</apex:panelGrid>

</apex:pageBlock>

</apex:page>
<apex:page standardController="Account__c"  extensions="SendInvitationByMail" renderAs="PDF" id="SendInvitationByMail">

Dear {!Account__c.Name},<br/>


You are cordially invited to the Visitors Day Meeting of the BNI Gems Chapter at The Royal Orchid
Hotel Spring Hall (Adjoining KGA Golf Course), HAL Airport Road on Friday {!Account__c.Visit_Date__c} . The
meeting will start promptly at 7:45AM and finish at 9.45AM. We will be sharing BNI's Secrets to
Success through "Word-of-Mouth" Marketing.<br/><br/>

BNI is a business and professional networking organisation whose primary purpose is to exchange
qualified business referrals. It is the most successful organisation of its type in the world today, with
over 5,800 chapters in operation world-wide (including the UK, USA, Malaysia, Singapore, Germany,
South Africa and Australia). Last year, members of BNI passed more than 6.5 million referrals which
generated more than Rs. 12,600 Crores worth of business for its members.<br/><br/>

What makes BNI unique is that it allows only one person per profession to join a chapter. Thus, once
you've joined, there is no conflict in passing leads, and none of your competitors can participate! BNI
is currently looking for IT PLM Consulting company to refer your business to. I would like to invite you

to the Visitors Day Meeting so you can learn more about the organisation, and find out if you would be
interested in generating a steady source of referrals for your business.<br/><br/>

Seating is limited, so please RSVP by calling me on +919342837640. There will be a nominal Rs.350/-
Meeting Fee, which includes breakfast. If you have any questions, please contact me. Remember to bring
plenty of business cards (at least 100 numbers) to pass around, as you will meet a lot of local business
people.<br/>

Venue<br/>
Seasons 1<br/>
The Royal Orchid Hotel<br/>
No 1, Golf Avenue, Adjoining KGA Golf Course<br/>
Old Airport Road, Bangalore 560008<br/>

Program<br/>
7:15AM-7:45AM – Open Networking<br/>
7:45AM – Meeting Starts<br/>
10:00AM – Onwards – Breakfast<br/>

Register here:<br/>
https://spreadsheets.google.com/a/rixyncs.co.in/spreadsheet/viewform?formkey=dHcwZmw5MnJtUWxHVGo0b0ZuajViZWc6MA#gid=0



Yours Sincerely,<br/>

P Ramesh Chander<br/>
Rixyncs India Inc<br/>
Core Group Member, BNI Gems Chapter<br/>

P.S: Please feel free to bring any business associate(s) with you who may also be interested in finding out how to generate more business through word of mouth referrals - this is a unique and really exciting opportunity for any individual or
 organisation to increase their business!<br/>

</apex:page>

 Please help.I will Appriciate your concer.

 

Thanks

 

Nasir

 

 

 

 

Chamil MadusankaChamil Madusanka

Hi,

 

replace

 

b= pdf.getContent();

with

 

b = pdf.getContentAsPDF();

 

If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

 

Chamil's Blog

NasirNasir

Hi Chamil,

 

I had tried this.But when i exclude {!Account__c.Name} from the visualforce page.It works.But i need the Name of the Account.So when i include this is visualforce page it gives me error :



SObject row was retrieved via SOQL without querying the requested field: Account__c.Name 

 

So can you please tell me how should i get rid of this error.In "send" method i have to include it,because it is not retriving account name.

 

Please help

 

Chamil MadusankaChamil Madusanka

Hi,

 

I'm confuse with this Account__c object. Is that your custom object? Or are you suppose to work with Account standard object?

If it is standard object remove the __c.

Chamil MadusankaChamil Madusanka

 

Hi,

 

If it is your custom object ;

 

email.setSubject( subject ); 
  mail.setWhatId(acc.Id);
    email.setToAddresses(toAddresses);
    email.setPlainTextBody( body );

 

 try those highlighted changes

 

If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.



NasirNasir

Hi Chamil,

 

Yes the Account object is a Custom object.

Chamil MadusankaChamil Madusanka

Hi,

 

If it is your custom object ;

 

email.setSubject( subject ); 
  mail.setWhatId(acc.Id);
    email.setToAddresses(toAddresses);
    email.setPlainTextBody( body );

 

 try those highlighted changes

 

If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.



NasirNasir

Hi Chamil,

 

I tried using 'email.setWhatId(acc.Id);   ' but was of no avail.This is not working.

 

When i am removing "Dear {!Acount__c.Name}" from the VF page then it is working perfectly fine.

 

Actually when i am Adding {!Acount__c.Name} and saving the VF page, and then trying to send the inviation.I am getting this Error.

 

SObject row was retrieved via SOQL without querying the requested field: Account__c.Name .

 

I think i have to use a SOQL statement in "Send Method'.What you Say?

 

Any help.I will appreciate your concern.