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
AlecSAlecS 

Apex Class - Access fields of custom object and fields of standard objects

I have an Apex class that is trying to get information from my custom object Invoice and send an email.

 

However, I get this error message:

SObject row was retrieved via SOQL without querying the requested field: Invoice__c.Invoice_Due_Date__c
Error is in expression '{!doMyApexLogic}' in component <apex:page> in page apexbuttonpage

 My code is here:

 

public class ApexButtonPageController {
    Invoice__c theInvoice;
    

    public ApexButtonPageController(ApexPages.StandardController stdController) {
        // get the current Invoice
        theInvoice = (Invoice__c)stdController.getRecord();
    }

    public PageReference doMyApexLogic() {
        //logic to send email out
        
        
        
        // First, reserve email capacity for the current Apex transaction to ensure
                        
// that we won't exceed our daily email limits when sending email after
                        
// the current transaction is committed.
Messaging.reserveSingleEmailCapacity(2);

// Processes and actions involved in the Apex transaction occur next,
// which conclude with sending a single email.

// Now create a new single email message object
// that will send out a single email to the addresses in the To, CC & BCC list.
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

// Strings to hold the email addresses to which you are sending the email.
String[] toAddresses = new String[] {'name@domain.com'}; 
  

// Assign the addresses for the To and CC lists to the mail object.
mail.setToAddresses(toAddresses);

// Specify the address used when the recipients reply to the email. 
mail.setReplyTo('test@test.com');

// Specify the name used as the display name.
mail.setSenderDisplayName('Billing');

// Specify the subject line for your email address.
mail.setSubject('Invoice');

// Set BCC as false
mail.setBccSender(false);

// Optionally append the salesforce.com email signature to the email.
// The email address of the user executing the Apex Code will be used.
mail.setUseSignature(false);


mail.setHtmlBody('Dear theInvoice.Email_Invoice_To__c <p>'+
     'Please find attached the invoice on your account that will be due on'+ theInvoice.Invoice_Due_Date__c+'.<p>'+
                'The invoice summary is as follows:<p>'+
'Account Name'+                      theInvoice.Account_Name__c+'<p>'+
'Contract Number'+                   theInvoice.Contract__c+'<p>'+

'Invoice Date'+                      theInvoice.Targeted_Date__c+'<p>'+
//'Invoice Due Date                  {!Invoice__c.Targeted_Date__c}<p>'+
//Invoice Number                    {!Invoice__c.Invoice__c}
//Invoice Amount                    USD {!Invoice__c.Amount_Invoiced__c}

'');

// Send the email you have created.
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });      
        
        
        update theInvoice;
        //</demo logic>
        return new PageReference('/' + theInvoice.id);
    }
}

 

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

Your page isn't querying Invoice_Due_Date__c automatically (it's not anywhere in the page's markup), and so fails to query, thus causing the error. Use this in your page:

 

<apex:outputText rendered="false" value="{!Invoice__c.Invoice_Due_Date__c}"/>

 

All Answers

sfdcfoxsfdcfox

Your page isn't querying Invoice_Due_Date__c automatically (it's not anywhere in the page's markup), and so fails to query, thus causing the error. Use this in your page:

 

<apex:outputText rendered="false" value="{!Invoice__c.Invoice_Due_Date__c}"/>

 

This was selected as the best answer
arizonaarizona

Just to add to sfdcfox's answer.  You can also query for the values in your controller.  I like to do that instead of creating hidden fields.  It is a matter of preference.

AlecSAlecS
Arizona,

Can you show me how that can be done with an example?

Thanks.!
AlecSAlecS

Thanks sfdcfox. That worked.

 

However, my email output shows the salesforce Ids and not the real value:

 

My page code is as follows:

<apex:page standardController="Invoice__c" extensions="ApexButtonPageController" action="{!doMyApexLogic}">
<apex:outputText value=" {0,date,yyyy.MM.dd}">
    <apex:param value="{!Invoice__c.Invoice_Due_Date__c}" /> 
</apex:outputText>
<apex:outputText rendered="false" value="{!Invoice__c.Account_Name__c}">
</apex:outputText>
<apex:outputText rendered="false" value="{!Invoice__c.Contract__c}">
</apex:outputText>
<apex:outputText rendered="false" value="{!Invoice__c.Targeted_Date__c}">
</apex:outputText>
<apex:outputText rendered="false" value="{!Invoice__c.Email_Invoice_To__c}">
</apex:outputText>
<apex:outputText rendered="false" value="{!Invoice__c.Amount_Invoiced__c}">
</apex:outputText>
</apex:page>

 

And my email output is:

Dear 003W000000Db0R5IAJ, //this is a lookup field to Contact

Please find attached the invoice on your account that will be due on 2014-02-15 00:00:00.

The invoice summary is as follows: 

Account Name 001W0000008Qm0CIAS //lookup filed to Account

Contract Number 800W0000000F307IAC //lookup field to Contract

Invoice Date 2014-01-16 00:00:00

Invoice Amount USD 500

 Your help is greatly appreciated!

sfdcfoxsfdcfox

You have to go "through" the relationship to get to the name:

 

{!Invoice__c.Account_Name__r.Name}
{!Invoice__c.Contract__r.Name}
{!Invoice__c.Email_Invoice_To__r.Name}

Update your Apex Code to use similar names; also check to make sure that the fields are not empty before attempting to go through them:

 

if(invoiceRecord.Account_Name__r!=null) {
  accountName = invoiceRecord.Account_Name__r.Name;
}

 

 

AlecSAlecS
Thanks for helping me with this too - it worked just the way it should.