0

enter image description here

Appointment: API Name Appointment__c enter image description here

Doctor: API Name Doctors__c

I am trying to list all the records of the selected doctor

public class AppointmentsController {

private List<Doctors__c>doctorList = new List<Doctors__c>();
private List<Patient__c>patientList = new List<Patient__c>();
private List<Appointment__c>appointmentList = new List<Appointment__c>();
public Doctors__c selectedDoctor {get;set;}
public Patient__c selectedPatient {get;set;}
public  List<Appointment__c>doctorAppointmentsList{get;set;}
public DateTime appointmentDate {
    get{
        return appointmentDate;  
    }
    set;
}
public Id selectedDoctorId{
    get;
    set{
       selectedDoctorId=value;
       updateDoctor();
       updateAppointments();
       }
}
public AppointmentsController (){
newAppointment=new Appointment__c();
patientList =[SELECT Id,Name FROM Patient__c ORDER BY Name ASC];   
doctorList = [SELECT Id,Name,Working_Hours_Start__c,Working_Hours_End__c FROM Doctors__c ORDER BY Name ASC];
appointmentList=[SELECT Id,Name,Doctor__c,Appointment_Date__c,Duration_in_minutes__c,Patient__c FROM Appointment__c ORDER BY Name ASC];    
selectedDoctor =  doctorList[0];
selectedPatient= patientList[0];
}
private void updateAppointments()
{
    for(Appointment__c appointment :  appointmentList) {
       
              if(selectedDoctorId==appointment.Doctor__c.Id)
              {
                doctorAppointmentsList.add(appointment);
              }

    }
}
private void updateDoctor()
{
    for(Doctors__c doctor :  doctorList) {
       
              if(this.selectedDoctorId==doctor.Id)
              {
                selectedDoctor=doctor;
              }

    }
}

updateAppointments() error in this function

0

1 Answer 1

1

I would recommend completing the module Apex Basics and Database on Trailhead. Two of the units in that module cover how to query and access data across relationships.

However, in this case, you don't need to traverse a relationship at all.

appointment.Doctor__c.Id

should simply be replaced by

appointment.Doctor__c

The relationship field itself contains the related record Id.


This code is at risk of a number of governor limits problems (in particular the Visualforce view state limit), as well as general poor performance, due to:

  • executing and storing unfiltered queries, which return all records in the database.
  • performing filtering in Apex instead of appropriately filtering SOQL queries to show relevant data.

I would recommend you consider removing your unfiltered queries and rebuilding updateDoctor() to perform a new SOQL query when the selected doctor changes to acquire the doctor record and their appointments, using a parent-child relationship query (covered in the module linked above). You would then be able to remove updateAppointments() entirely.

2
  • Thank you very much for your answer, I have to display the records on the apex page in accordance with the selected doctor and since the request is quite costly, I decided to filter in the controller
    – Vanya
    Commented Feb 23, 2021 at 8:36
  • @Vanya I am recommending you filter in SOQL, not in Apex.
    – David Reed
    Commented Feb 23, 2021 at 14:46

You must log in to answer this question.

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