0

I have implemented an Page called"attachmentPDF" with standard controller 'Contact' which having some contact details and renderAs="PDF".

Now i have implemented another page to send email with attachment of above 'pdf' file.

even though i passing contact record id for the page i am getting "System.VisualforceException: SObject row was retrieved via SOQL without querying the requested field: Contact.FirstName".

Note: if i replace another page "attachmentPDF2" with account standard controller it is working fine.

Any one can help on this.

Thanks,

Chandra

Page1:

Contact Details

  <apex:outputLabel for="Name" value="Name"/>
  <apex:outputText id="Name" value="{!Contact.FirstName}"/>

Page2:

Email Message

Send an Email   

Controller:

public class ContactSendEmail{

public String selectedTemplateId { public get; public set; }
public String SelectedFromAddress {get; set;}
public String selectedObject {get; set;}
public String subject {get; set;}
public String body {get; set;}
public string Attach { get;set; }
public string AttachName { get;set; }
public string ToAddress { get; set; }
Map<id,EmailTemplate> Maptemp = new Map<Id,EmailTemplate>();
Contact Con = new Contact();

public ContactSendEmail(ApexPages.StandardController controller) {
          Con = [SELECT Id,LastName,FirstName,Email,Phone FROM Contact WHERE Id=:ApexPages.currentPage().getParameters().get('id')];
          ToAddress = string.valueof(Con.Email);
          Attach = 'attachmentPDF';
}

public List<SelectOption> getEmailFrom() 
{
    List<SelectOption> objNames = new List<SelectOption>();
    List<OrgwideEmailAddress> org = [Select id,Address from OrgwideEmailAddress];
    for(OrgwideEmailAddress name : org)
    {
        objNames.add(new SelectOption(name.Id,name.Address));
    }
    return objNames;
}

public List<SelectOption> getMyPersonalTemplateOptions() {
    List<SelectOption> options = new List<SelectOption>();
    for (EmailTemplate t : [select Id,Name,Subject,body from EmailTemplate]) 
     {
        Maptemp.put(t.Id,t);
        options.add(new SelectOption(t.Id,t.Name));
    }
    return options;

}

public PageReference send() 
{
    String[] toAddresses = ToAddress.split(':', 0);
    system.debug('toAddresses ######'+toAddresses );
    system.debug('Emailaddress######'+toAddresses);
    Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
    List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
    PageReference pdf =  Page.attachmentPDF;
    pdf.getParameters().put('id',Con.Id);
    pdf.setRedirect(true);
    Blob b = pdf.getContent();
    Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
    efa.setFileName('Attachement.pdf');
    efa.setBody(b);
   fileAttachments.add(efa);
    email.setFileAttachments(fileAttachments);
    email.setOrgWideEmailAddressId(SelectedFromAddress);
    email.setSubject( subject );
    email.setToAddresses( toAddresses );
    email.setPlainTextBody( body );
    system.debug('selectedTemplateId######'+selectedTemplateId);
    system.debug('Emailaddress######'+toAddresses);
    Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});

    system.debug('#########Result#####'+r);

    return null;
} 


public PageReference Cancel() {
    PageReference pg= new PageReference ('/home/home.jsp');
    pg.setRedirect(true);
    return pg;
}

}

1

The query where you are getting contact fields on the basis of contact id, you have not mentioned FirstName field in that query.Mention that field in the query and this error will go away :)

  • i included first name in query – ncmouli Aug 4 '16 at 10:32
  • Can you post the page and controller or the code ? – Abhijeet Aug 4 '16 at 10:33
  • Please add the Vf page too...i think you must be getting this error on the page from where u r generating the pdf. is that correct? – Abhijeet Aug 4 '16 at 10:41

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.