There seems to be a common problem using the getRecord function, so I am going to write it down here. This problem has haunted me a few times now.
Whenever you use a controller extension, extending a standard object, you can use the getRecord() to get the reference to the record. For example, if you have an extension to the contact object, this can be what your Apex class looks like:
public class ContactExtension
{
private Contact contact;
public ContactExtension(ApexPages.StandardController sController)
{
this.contact = (Contact)sController.getRecord();
}
}
Besides the obvious constructor, you will most likely have some other methods inside your class to manipulate the contact. Let's say you want to provide a form so the user can change email address and phone number of the contact. Your VisualForce class looks very simple
<apex:page standardController="Contact" extensions="ContactExtension">
{!greeting}
<apex:form>
<apex:inputField value="{!contact.Email" />
<apex:inputField value="{!contact.MobilePhone}" />
<apex:commandButton action="{!save}" value="Save" />
</apex:form>
</apex:page>
The {!greeting} is going to display some greeting messages. That means we will need to write a getGreeting() method in the class.
If this is the definition of your getGreeting() inside the class,
public String getGreeting()
{
return 'Hi ' + contact.FirstName + ' how have you been? Please update the following.';
}
you will then get an error message that looks like this:
System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Contact.FirstName
Most of us think that as soon as we call the getRecord() method, then the entire record is loaded into the contact variable. However if you have an object with 5000 fields, and you only need to use 2 or 3 in your Visualforce pages, do you think it really makes sense to load all 5000 fields for that record into the contact variable? Probably not.
Then what can you do? The easiest way is to do add this into your Visualforce page:
<apex:outputText value="{!contact.FirstName}"/>
If you say, but I don't want to display the contact name twice! Then you can simply change this to:
<apex:outputText value="{!contact.FirstName}" rendered="false" />
By adding rendered="false", that will hide this variable. Or perhaps use <apex:variable> instead, such as:
<apex:variable value="{!contact.FirstName}" var="contactFName" />
When you load this Visualforce page, the standard controller will scan through your Visualforce page and knows that you need this field, and when making the getRecord() method call, it automatically and implicitly adds this FirstName field into the SOQL.
Remember, if you want to use any field in your extension via the getRecord() method, you need to let it be known by making sure that the field exists in your Visualforce page!
(Another alternative is to write your own SOQL in your Apex class, but why make an extra SOQL call when you can avoid it?)
You can use the below code when the controller variable is a ApexPages.StandardController before you call getRecord.
ReplyDeletecontroller.addFields(new List{'FirstName'});
James Kent
http://www.blueengine.com.au
Super helpful, exactly what I needed. The comment about addFields was great too, except it left out "", as in:
ReplyDeletecontroller.addFields(new List{'FirstName'});
Oh, i see this site's comments nix anything in angle brackets. So, after List should be "String" inside of angle brackets. Meaning in the below example, replace String in square brackets with String in angle brackets:
DeletestdController.addFields(new List[String]{'FirstName'});
Thanks!!! Saved my time
ReplyDeleteI simply wanted to thank you so much again. I am not sure the things that I might have gone through without the type of hints revealed by you regarding that situation.
ReplyDeletejava training in chennai
Appreciating the persistence you put into your blog and detailed information you provide.
ReplyDeleteBest selenium training Institute in chennai
The young boys ended up stimulated to read through them and now have unquestionably been having fun with these things.
ReplyDeleteHadoop Training in Chennai
Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
ReplyDeleteDevops Training in pune
Devops Training in Chennai
Devops Training in Bangalore
AWS Training in chennai
AWS Training in bangalore
Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
ReplyDeleterpa training in Chennai
rpa training in anna nagar | rpa training in marathahalli
rpa training in btm | rpa training in kalyan nagar
rpa training in electronic city | rpa training in chennai
rpa online training | selenium training in training
After reading this web site I am very satisfied simply because this site is providing comprehensive knowledge for you to audience. Thank you to the perform as well as discuss anything incredibly important in my opinion. We loose time waiting for your next article writing in addition to I beg one to get back to pay a visit to our website in
ReplyDeletepython training in tambaram
python training in annanagar
python training in OMR
python training in chennai
I am sure this post has helped me save many hours of browsing other related posts just to find what I was looking for. Many thanks!
ReplyDeleteData Science course in kalyan nagar | Data Science course in OMR
Data Science course in chennai | Data science course in velachery
Data science course in jaya nagar
Hiiii....Thanks for sharing Great information...Nice post...Keep move on...
ReplyDeleteSalesforce Training in Hyderabad
Excellent post!!! In this competitive market, customer relationship management plays a significant role in determining a business success.
ReplyDeleteSalesforce Training | Online Course | Certification in chennai | Salesforce Training | Online Course | Certification in bangalore | Salesforce Training | Online Course | Certification in hyderabad | Salesforce Training | Online Course | Certification in pune
You Are doing a great job. I would like to appreciate your work for good accuracy.make somo more.
ReplyDeleteAi & Artificial Intelligence Course in Chennai
PHP Training in Chennai
Ethical Hacking Course in Chennai Blue Prism Training in Chennai
UiPath Training in Chennai
kralbet
ReplyDeletebetpark
tipobet
slot siteleri
kibris bahis siteleri
poker siteleri
bonus veren siteler
mobil ödeme bahis
betmatik
4T4GG7
betmatik
ReplyDeletekralbet
betpark
tipobet
slot siteleri
kibris bahis siteleri
poker siteleri
bonus veren siteler
mobil ödeme bahis
10TN
شركة كشف تسربات المياه بالاحساء HjpMLlPWB1
ReplyDeleteشركة تنظيف شقق بالقطيف T8GNulXjAy
ReplyDelete