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
Alex Waddell 18Alex Waddell 18 

SObject row was retrieved via SOQL without querying the requested field: Preventative_Screening__c.Account__r

I am getting the following error when I remove an <apex:outputText value/> from a visual force page

SObject row was retrieved via SOQL without querying the requested field: Preventative_Screening__c.Account__r
An unexpected error has occurred. Your development organization has been notified.

Here is the code before removing the field output
Page:
<apex:page standardController="Preventative_Screening__c" extensions="ContractDocumentExtDIABTest" docType="html-5.0" showHeader="false" sidebar="false" standardStylesheets="false" id="thePage">
    <apex:stylesheet value="{!URLFOR($Resource.slds120,'assets/styles/salesforce-lightning-design-system.css')}"/> 
    <apex:form id="theForm">
    <div>
    </div>
        <div align="Left" id="PatentInfo">
        <apex:outputText value="{!Preventative_Screening__c.Account__r.name}"/><br/>
      
<div style="width:100%;"> 
                <div style="float:left;">
                    <div style="display:{!IF(caseManagerSign != Null,'block','None')} ">
                        <apex:image url="{!URLFOR($Action.Attachment.Download, caseManagerSign.Id)}" rendered="{!caseManagerSign != Null}" width="30%" height="30%"/><br/>
                        <apex:outputLabel value="Signed By: "/>
                        <apex:outputText value="{!Preventative_Screening__c.Last_Modified_for_App__c}"  /><br/>
                        </div>                                                   
                </div>      
            </div>
</div>

</apex:form>

</apex:page>
Extension
/*Description : This class is a controller extension for 'Contract_Template' Page. This class contians the functionality to send email to the Customer and render the PDF for display*/
public Class ContractDocumentExtDIABTest{
    public Preventative_Screening__c contractDocument{get;set;}
    Public Attachment patientSign{get;set;}
    Public Attachment caseManagerSign{get;set;}
    public String chooserender{get;set;}
    Public List<contact> currContactLst;
    public ContractDocumentExtDIABTest(ApexPages.StandardController stdController){
        String parentid;         
        contractDocument= (Preventative_Screening__c) stdController.getRecord();            
        if(contractDocument != Null){
            currContactLst = [SELECT ID FROM Contact WHERE Accountid  = : contractDocument.Account__r.id];
        }        
        List<Attachment> attachmentLst = [SELECT ID,CreatedDate  FROM Attachment WHERE parentId =: contractDocument.id order by CreatedDate asc  ];        
        if(attachmentLst.size() == 1){
            caseManagerSign = attachmentLst[0];  
            } 
        if(attachmentLst.size() == 2){
           caseManagerSign = attachmentLst[0];
           patientSign = attachmentLst[1];                    
        }       
        if(ApexPages.currentPage().getParameters().get('renderAs') != null){
            chooserender = 'print';
        }else{
            chooserender = Null;    
        } 
    }
    /*public void sendEmail(){        
        contractDocument = [SELECT ID,Account__r.email__c,Account__r.id FROM Preventative_Screening__c WHERE ID =:contractDocument.id ];        
        if(contractDocument.Account__r.email__c == Null || currContactLst.size() == 0 ){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'Please enter the Email address in Account'));                               
        }else{
            System.PageReference currentPage = ApexPages.currentPage();
            Blob pdfData = currentPage.getContentAsPDF();            
            Messaging.EmailFileAttachment emailMessage = new Messaging.EmailFileAttachment();
            emailMessage.setContentType('application/pdf');
            emailMessage.setFileName('Contact.pdf');
            emailMessage.setInline(false);
            if (Test.IsRunningTest()){
                emailMessage.Body = Blob.valueOf('UNIT.TEST');                 
            }else{
              emailMessage.Body = pdfData;     
            }        
            try{
                List<Messaging.SingleEmailMessage> emailMessageLst= new List<Messaging.SingleEmailMessage>();
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                String[] toAddresses = new String[] {contractDocument.Account.email__c};
                mail.setToAddresses(toAddresses);
                EmailTemplate currEmailTemplate = [SELECT ID from EmailTemplate WHERE Name = : Label.Email_Template_Name_for_Contract_Document];
                mail.setTemplateId(currEmailTemplate.id);
                mail.setWhatId(contractDocument.Account__r.id);
                mail.setTargetObjectId(currContactLst[0].id);
                mail.setTreatTargetObjectAsRecipient(true);
                mail.setSaveAsActivity(true);                    
                mail.setFileAttachments(new Messaging.EmailFileAttachment[] { emailMessage});                    
                emailMessageLst.add(mail);             
                Messaging.sendEmail(emailMessageLst);   
            }Catch(Exception ex){
                System.debug('Exception Message'+ex.getmessage());
            } 
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,'Email sent successfully'));  
        }    
    }*/
    
}
Photo of VF Page working
User-added image
VF Page after removing apexOutput 
<apex:page standardController="Preventative_Screening__c" extensions="ContractDocumentExtDIABTest" docType="html-5.0" showHeader="false" sidebar="false" standardStylesheets="false" id="thePage">
    <apex:stylesheet value="{!URLFOR($Resource.slds120,'assets/styles/salesforce-lightning-design-system.css')}"/> 
    <apex:form id="theForm">
    <div>
    </div>
        <div align="Left" id="PatentInfo">

      
<div style="width:100%;"> 
                <div style="float:left;">
                    <div style="display:{!IF(caseManagerSign != Null,'block','None')} ">
                        <apex:image url="{!URLFOR($Action.Attachment.Download, caseManagerSign.Id)}" rendered="{!caseManagerSign != Null}" width="30%" height="30%"/><br/>
                        <apex:outputLabel value="Signed By: "/>
                        <apex:outputText value="{!Preventative_Screening__c.Last_Modified_for_App__c}"  /><br/>
                        </div>                                                   
                </div>      
            </div>
</div>

</apex:form>

</apex:page>

Image of error
User-added image


Any ideas on how to successfully remove this Apex outputfield and avoid this error?

 
Sunad RasaneSunad Rasane
Please use the following code-
/*Description : This class is a controller extension for 'Contract_Template' Page. This class contians the functionality to send email to the Customer and render the PDF for display*/
public Class ContractDocumentExtDIABTest{
    public Preventative_Screening__c contractDocument{get;set;}
    Public Attachment patientSign{get;set;}
    Public Attachment caseManagerSign{get;set;}
    public String chooserender{get;set;}
    Public List<contact> currContactLst;
    public ContractDocumentExtDIABTest(ApexPages.StandardController stdController){
        String parentid;         
        contractDocument= (Preventative_Screening__c) stdController.getRecord();            
       /* if(contractDocument != Null){
            currContactLst = [SELECT ID FROM Contact WHERE Accountid  = : contractDocument.Account__r.id];
        }      */  
        List<Attachment> attachmentLst = [SELECT ID,CreatedDate  FROM Attachment WHERE parentId =: contractDocument.id order by CreatedDate asc  ];        
        if(attachmentLst.size() == 1){
            caseManagerSign = attachmentLst[0];  
            } 
        if(attachmentLst.size() == 2){
           caseManagerSign = attachmentLst[0];
           patientSign = attachmentLst[1];                    
        }       
        if(ApexPages.currentPage().getParameters().get('renderAs') != null){
            chooserender = 'print';
        }else{
            chooserender = Null;    
        } 
    }
    /*public void sendEmail(){        
        contractDocument = [SELECT ID,Account__r.email__c,Account__r.id FROM Preventative_Screening__c WHERE ID =:contractDocument.id ];        
        if(contractDocument.Account__r.email__c == Null || currContactLst.size() == 0 ){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'Please enter the Email address in Account'));                               
        }else{
            System.PageReference currentPage = ApexPages.currentPage();
            Blob pdfData = currentPage.getContentAsPDF();            
            Messaging.EmailFileAttachment emailMessage = new Messaging.EmailFileAttachment();
            emailMessage.setContentType('application/pdf');
            emailMessage.setFileName('Contact.pdf');
            emailMessage.setInline(false);
            if (Test.IsRunningTest()){
                emailMessage.Body = Blob.valueOf('UNIT.TEST');                 
            }else{
              emailMessage.Body = pdfData;     
            }        
            try{
                List<Messaging.SingleEmailMessage> emailMessageLst= new List<Messaging.SingleEmailMessage>();
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                String[] toAddresses = new String[] {contractDocument.Account.email__c};
                mail.setToAddresses(toAddresses);
                EmailTemplate currEmailTemplate = [SELECT ID from EmailTemplate WHERE Name = : Label.Email_Template_Name_for_Contract_Document];
                mail.setTemplateId(currEmailTemplate.id);
                mail.setWhatId(contractDocument.Account__r.id);
                mail.setTargetObjectId(currContactLst[0].id);
                mail.setTreatTargetObjectAsRecipient(true);
                mail.setSaveAsActivity(true);                    
                mail.setFileAttachments(new Messaging.EmailFileAttachment[] { emailMessage});                    
                emailMessageLst.add(mail);             
                Messaging.sendEmail(emailMessageLst);   
            }Catch(Exception ex){
                System.debug('Exception Message'+ex.getmessage());
            } 
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,'Email sent successfully'));  
        }    
    }*/
    
}

Please reply if it helps and make sure to mark it as the best answer so it will be helpful for others.
Thanks
Dushyant SonwarDushyant Sonwar
Alex,

The reason why this error comes is explained below.

You have commented your sendEmail  method ,so error mentioned is not coming because of sendEmail

Your constructor is having some issues.

When you use a standardController and use a getRecord() method , it gets only those fields that you are using in the visualforce page. So  when you remove outputText({!Preventative_Screening__c.Account__r.) field getRecord does not query that Account__r , but you are using in your apex class line no12 (contractDocument.Account__r.id). So it throws an error

Solution:
Query the record using the recordId and mention the fields that you want on the page to use and bind the object with those field expressions.

Example (in this one field is queried , but you need to query all the fields that you want to use it in visualforce page):
contractDocument = [Select Account__r.name from  Preventative_Screening__c where id=:stdController.getId()];
 
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_pages_standardcontroller.htm

Hope this helps!