You need to sign in to do that
Don't have an account?
AlecS
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); } }
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:
All Answers
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:
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.
Can you show me how that can be done with an example?
Thanks.!
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:
Your help is greatly appreciated!
You have to go "through" the relationship to get to the 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: