I just struck with one of the issue when developing something in visualforce page.
The problem is when I load the visualforce page am seeing this below error
The problem is when I load the visualforce page am seeing this below error
Error : SObject row was retrieved via SOQL without querying the requested field
I was trying to fetch the field in controller like below,
Public class caseController{
public Case cas {get; set;}
private ApexPages.StandardController controller {get; set;}
public string caseOwnId{get;set;}
public caseController(ApexPages.StandardController controller) {
this.controller = controller;
cas = (Case) controller.getRecord();
string caseOwnId = cas.OwnerId;
}
When I use the string field caseOwnId in visualforce page to display the owner id, it is saying that 'SObject row was retrieved via SOQL without querying the requested field'. Isn't this weird?.
Usually, we will see this error if we missed the field while querying. right?.
After some time I found the solution for this as well. Ofcourse the solution is very new to me.
The solution is, we can add the field API name in the controller itself. see below,
controller.addFields(new List<String>{'OwnerId'});
We have method called addFields in the controller. By using that, we just need to add the api name of the field to the list. That's it. So, the full constructor would be,
Public class caseController{
public Case cas {get; set;}
private ApexPages.StandardController controller {get; set;}
public string caseOwnId{get;set;}
public caseController(ApexPages.StandardController controller) {
this.controller = controller;
controller.addFields(new List<String>{'OwnerId'});
cas = (Case) controller.getRecord();
string caseOwnId = cas.OwnerId;
}
Thanks a lot!!! I've been searching for a solution for this problem, and your code just helped me solve this. Just to add some more automation to this, if you want to add lots of fields (as in my case), you can use something like this to loop through all the fields object and add all of them (using Opportunity as example):
ReplyDeleteMap opp_camp_list = Schema.SObjectType.Opportunity.fields.getMap ();
For (String opp_camp: opp_camp_list.keySet ())
Controller.addFields (new List {opp_camp});
And, to related objects, you make almost the same thing, just concating the related object's name, with a dot (.), with its fields names, like this (using OpportunityLineItem from the Opportunity):
Map item_camp_list = Schema.SObjectType.OpportunityLineItem.fields.getMap ();
For (String item_camp: item_camp_list.keySet ())
Controller.addFields (new List {'OpportunityLineItems.' + Item_camp});
Sorry for my poor english, and thank you again.
Great!. Adds some light on this article. But always fetch only necessary fields:).
DeleteVery Useful article
ReplyDeletedata science course
This post is very simple to read and appreciate without leaving any details out. Great work!
ReplyDeleteartificial intelligence course in noida
I feel extremely glad to have seen your site page and anticipate such a large number of additionally engaging occasions perusing here. Much obliged again for all the subtleties.
ReplyDeletehrdf contribution
I think I have never watched such online diaries ever that has absolute things with all nuances which I need. So thoughtfully update this ever for us.
ReplyDeletesupply chain analytics beginner's guide
What an extremely wonderful post this is. Genuinely, perhaps the best post I've at any point seen to find in as long as I can remember. Goodness, simply keep it up.
ReplyDeletedigital marketing course
With so many books and articles coming up to give gateway to make-money-online field and confusing reader even more on the actual way of earning money,
ReplyDeletebest institute for data science in hyderabad
I've read this post and if I could I desire to suggest you some interesting things or suggestions. Perhaps you could write next articles referring to this article. I want to read more things about it!
ReplyDeletedata scientist courses
Incredibly conventional blog and articles. I am realy very happy to visit your blog. Directly I am found which I truly need. Thankful to you and keeping it together for your new post.
ReplyDeletedata science course in gurgaon
Your content is very unique and understandable useful for the readers keep update more article like this.
ReplyDeletedigital marketing classes in aurangabad
Informative article. Thanks for sharing with us.keep it up.
ReplyDeletedata scientist course aurangabad
Impressive. Your story always bring hope and new energy. Keep up the good work.
ReplyDeletedata scientist course
Excellent effort to make this blog more wonderful and attractive.
ReplyDeletefull stack web development course
Great tips and very easy to understand. This will definitely be very useful for me when I get a chance to start my blog.
ReplyDeletefull stack development course