Thursday 13 April 2017

SObject row was retrieved via SOQL without querying the requested field in Controller

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

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;
}

15 comments:

  1. 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):

    Map 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.

    ReplyDelete
    Replies
    1. Great!. Adds some light on this article. But always fetch only necessary fields:).

      Delete
  2. This post is very simple to read and appreciate without leaving any details out. Great work!
    artificial intelligence course in noida

    ReplyDelete
  3. 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.
    hrdf contribution

    ReplyDelete
  4. 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.
    supply chain analytics beginner's guide

    ReplyDelete
  5. 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.
    digital marketing course

    ReplyDelete
  6. 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,
    best institute for data science in hyderabad

    ReplyDelete
  7. 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!
    data scientist courses

    ReplyDelete
  8. 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.
    data science course in gurgaon

    ReplyDelete
  9. Your content is very unique and understandable useful for the readers keep update more article like this.
    digital marketing classes in aurangabad

    ReplyDelete
  10. Impressive. Your story always bring hope and new energy. Keep up the good work.
    data scientist course

    ReplyDelete
  11. Excellent effort to make this blog more wonderful and attractive.
    full stack web development course

    ReplyDelete
  12. Great tips and very easy to understand. This will definitely be very useful for me when I get a chance to start my blog.
    full stack development course

    ReplyDelete