2

I'm getting this error while running this VF Page

Error is: SObject row was retrieved via SOQL without querying the requested field: Attachment.ParentId

VF Page:

    <apex:page showHeader="false"  controller="Controller004"  sidebar="false" >
<apex:messages />
<apex:pageBlock >
<apex:pageBlockSection title="Record of avalaible Doctors" >
<apex:form >
<apex:pageBlockTable value="{!wc}" var="a" columns="4" cellpadding="15px">

 <apex:column headerValue="Name">
 <apex:outputField value="{!a.doctor.Full_Name__c}"  />
 </apex:column>
 <apex:column headerValue="Fee">
<apex:outputField value="{!a.doctor.Fee__c}"/>
</apex:column>
<apex:column headerValue="Schedule">
<apex:outputField value="{!a.doctor.schedule__c}"/>
</apex:column>
<apex:column headerValue="Photo" >
<apex:image url="{!a.url}" height="300px" width="200px" />
</apex:column>
</apex:pageBlockTable>
</apex:form>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:page>

Controller of this page is:

    global class Controller004{   
 public List<Doctor__c> obj {get;set;}
 public List<WrapperClass> wc{get;set;}

 public Controller004(){

 obj=[select Full_Name__c, Fee__c, schedule__c from Doctor__c];
 Map<Id,Id> mapDocToAtt = new Map<Id,Id>();
 for(Attachment a : [select id from Attachment where ParentId in:obj])
 { 
    mapDocToAtt.put(a.ParentId, a.Id);
 }

 for(Doctor__c doctor : [select Full_Name__c, Fee__c, schedule__c from Doctor__c])
 {
    wc = new List<WrapperClass>();
    wc.add(new WrapperClass(doctor, '/servlet/servlet.FileDownload?file='+mapDocToAtt.get(doctor.Id)));

 }
}

 public class WrapperClass{
 public Doctor__c doctor{get;set;}
 public String url{get;set;}
 public WrapperClass(Doctor__c doctor, String url){
 this.doctor=doctor;
 this.url=url;

 }



 }


}
2
  • 3
    The query should be select id, Parentid. You should query for all the fields you use or else you get this error. Commented Apr 10, 2016 at 6:35
  • What @igneshwaran said. If you put this in an answer, I will upvote it.
    – mkorman
    Commented Apr 10, 2016 at 8:03

2 Answers 2

1

You have to reference some fields (id, ParentId) in your controller that you are calling in the next queries.

This solution should work for you.

    global class Controller004{   
    public List<Doctor__c> obj {get;set;}
    public List<WrapperClass> wc{get;set;}

    public Controller004(){

    obj=[select Id, Full_Name__c, Fee__c, schedule__c from Doctor__c];
    Map<Id,Id> mapDocToAtt = new Map<Id,Id>();
    for(Attachment a : [select id,ParentId from Attachment where ParentId in:obj])
    { 
      mapDocToAtt.put(a.ParentId, a.Id);
    }

    for(Doctor__c doctor : [select Id,Full_Name__c, Fee__c, schedule__c from Doctor__c])
    {
      wc = new List<WrapperClass>();
      wc.add(new WrapperClass(doctor, '/servlet/servlet.FileDownload?file='+mapDocToAtt.get(doctor.Id)));

   }
  }

   public class WrapperClass{
   public Doctor__c doctor{get;set;}
   public String url{get;set;}
   public WrapperClass(Doctor__c doctor, String url){
   this.doctor=doctor;
   this.url=url;

   }
   }
  }

Let me know if this suits you.

0

Good Morning,

How said @Vigneshwaran G. You need to add ParentId to the Attachment query and the Id to the doctor query, because you are using these field without query it.

First error:

for(Attachment a : [select id from Attachment where ParentId in:obj])
 { 
    mapDocToAtt.put(a.ParentId, a.Id);
    //Here you are using ParentId but your query doesn't have it

 }

Second error:

for(Doctor__c doctor : [select Full_Name__c, Fee__c, schedule__c from Doctor__c])
 {
    wc = new List<WrapperClass>();
    wc.add(new WrapperClass(doctor, '/servlet/servlet.FileDownload?file='+mapDocToAtt.get(doctor.Id)));
//Here you are using the doctor.Id and your query doesn't have it.
 }

Regards

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .