Нужна помощь Unknown property

вылазит ошибка Unknown property ‘HospitalController.doctors’
я уже что-то не соображаю в чем дело

<apex:page docType="html-5.0" controller="HospitalController" showHeader="false" sidebar="false">
    <apex:form >
        <apex:pageBlock title="Appointmets Table">
            
            <!-- Select a doctor -->
            <apex:pageBlockSection >                
                <apex:selectList label="Select a doctor" value="{!selectedDoctor}" size="1"> 
					<apex:selectOptions value="{!doctors}"/>
                    <apex:actionSupport event="onchange" 
                                        reRender="app_list1, app_list2, app_list3"/>
                </apex:selectList>
            </apex:pageBlockSection>
            
            <!-- Select a patient -->
            <apex:pageBlockSection >
                <apex:selectList label="Select a patient" value="{!selectedPatient}" size="1"> 
					<apex:selectOptions value="{!patients}"/>
                </apex:selectList>
            </apex:pageBlockSection>
               
            <!-- Appointment date -->
            <apex:pageBlockSection >
                <apex:inputField label="Appointment date" 
                                 value="{!appt.Appointment_Date__c}"
                                 required="false"/>
            </apex:pageBlockSection>
            
            <!-- Duration -->
            <apex:pageBlockSection >
                <apex:inputField label="Duration in minutes" 
                            value="{!appt.Duration__c}"
                           	required="false"/>
            </apex:pageBlockSection>
            
            <!-- Add new appointment button -->
            <apex:pageBlockSection >
                <apex:commandButton immediate="false" reRender="app_list3" action="{!addNewAppt}" value="Add New Appointment" />
            </apex:pageBlockSection>
            
            <apex:pageBlockTable id="app_list3" value="{!tableAppointments}" var="apt">
                <apex:column headerValue="Action">
                	<apex:outputLink value="{!URLFOR('/' + apt.Id)}">
                    	View
                    </apex:outputLink>
                </apex:column>
                <apex:column headerValue="Doctor's Name" value="{!apt.Doctor__c}"/>
                <apex:column headerValue="Patient's Name" value="{!apt.Patient__c}"/>
                <apex:column headerValue="Appointment Date" value="{!apt.Appointment_Date__c}"/>
                <apex:column headerValue="Duration in minutes" value="{!apt.Duration__c}"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>


```code
public class HospitalController {
 
    public Appointment__c appt {get; set;}
    public String selectedDoctor {get; set;}
    public String selectedPatient {get; set;}
    public Datetime appDate {get; set;}
    public Integer duration {get; set;}
 
    public HospitalController() {
        this.appt = new Appointment__c();
        this.selectedDoctor = '';
        this.selectedPatient = '';
        this.appDate = System.now();
        this.duration = 30;
    }
 
    public PageReference addNewAppt() {
        try {
            this.appt.Doctor__c = selectedDoctor;
            this.appt.Patient__c = selectedPatient;

            
            insert this.appt;
        }
        catch (Exception ex) {
            ApexPages.addMessages(ex);
        }
        
        return NULL;
    }
    
}

Такое случается обычно из-за того что нет доступа к переменной. У вас отсутствует List doctors в контроллере.
Вот пример: https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/apex_pages_selectoption.htm

1 Like
<apex:pageBlockSection >                
                <apex:selectList label="Select a doctor" value="{!selectedDoctor}" size="1"> 
					<apex:selectOptions value="{!doctors}"/>
                    <apex:actionSupport event="onchange" 
                                        reRender="app_list1, app_list2, app_list3"/>
                </apex:selectList>
            </apex:pageBlockSection>

Смотрите, у Вас в SelectList должен отображаться List doctors. В контроллере у Вас эта коллекция не объявлена.
Объявите ее в контроллере

public List<SelectOptions > doctors {get; set;}

А в конструкторе инициализируйте

for(Doctor__c doctor : [SELECT Name FROM Doctor__c]) {
    
            doctors.add(new SelectOption(doctor.Name, doctor.Name));
        }
4 Likes
public class HospitalController {
 
    public Appointment__c appt {get; set;}
    public String selectedDoctor {get; set;}
    public String selectedPatient {get; set;}
    public Datetime appDate {get; set;}
    public Integer duration {get; set;}
 public List<Doctor__c> DoctorList {get;set;}
    public List<SelectOption> doctorSelectOptionList {get;set;}
    public List<Patient__c> PatientList {get;set;}
    public List<SelectOption> patientSelectOptionList {get;set;} 
 
    public HospitalController() {
        this.appt = new Appointment__c();
        this.selectedDoctor = '';
        this.selectedPatient = '';
        this.appDate = System.now();
        this.duration = 30;
        
            
        doctorSelectOptionList = new List<SelectOption>();
        patientSelectOptionList = new List<SelectOption>();
     
        

        DoctorList = [SELECT iD, Name FROM Doctor__c];
        for (Doctor__c doc : DoctorList){ 
            doctorSelectOptionList.add(new SelectOption(doc.Id, doc.Name)); 
        }
             
        PatientList = [SELECT Id, Name FROM Patient__c];
        for (Patient__c patient : PatientList){
            patientSelectOptionList.add(new SelectOption(patient.Id, patient.Name)); 
        }
    }
    }
 
   <apex:page docType="html-5.0" controller="HospitalController" showHeader="false" sidebar="false">
<apex:form >
<apex:pageBlock title="Appointmets Table">

<!-- Select a doctor -->
<apex:pageBlockSection>
 Select a doctor:
<apex:selectList size="1">
<apex:selectOptions value="{!doctorSelectOptionList}"/>
<apex:actionSupport event="onchange" 
reRender="app_list1, app_list2, app_list3"/>
</apex:selectList>
</apex:pageBlockSection>

<!-- Select a patient -->
<apex:pageBlockSection>
Select a patient:
<apex:selectList size="1">
<apex:selectOptions value="{!patientSelectOptionList}"/>
</apex:selectList>
</apex:pageBlockSection>


<!-- Appointment date -->
<apex:pageBlockSection >
<apex:inputField label="Appointment date" 
value="{!appt.Appointment_Date__c}"
required="false"/>
</apex:pageBlockSection>

<!-- Duration -->
<apex:pageBlockSection >
<apex:inputField label="Duration in minutes" 
value="{!appt.Duration__c}"
required="false"/>
</apex:pageBlockSection>

<!-- Add new appointment button -->
<apex:pageBlockSection >
<apex:commandButton immediate="false" reRender="app_list3" action="{!addNewAppt}" value="Add New Appointment" />
</apex:pageBlockSection>
<!-- Pagination -->
<table style="width: 100%"><tr>
<td>
Page: <apex:outputText 
value=" {!PageNumber} of {! CEILING(ResultSize / PageSize) }"/>
</td> 
<td align="center">
<!-- Previous page -->
<!-- active -->
<apex:commandLink action="{! Previous }" value="« Previous"
rendered="{! HasPrevious }"/>
<!-- inactive (no earlier pages) -->
<apex:outputText style="color: #ccc;" value="« Previous"
rendered="{! NOT(HasPrevious) }"/>
&nbsp;&nbsp; 
<!-- Next page -->
<!-- active -->
<apex:commandLink action="{! Next }" value="Next »"
rendered="{! HasNext }"/>
<!-- inactive (no more pages) -->
<apex:outputText style="color: #ccc;" value="Next »"
rendered="{! NOT(HasNext) }"/>
</td>

<td align="right">
<!-- Records per page -->
Records per page:
<apex:selectList value="{! PageSize }" size="1">
<apex:selectOption itemValue="5" itemLabel="5"/>
<apex:selectOption itemValue="20" itemLabel="20"/>
<apex:actionSupport event="onchange" reRender="contacts_list"/>
</apex:selectList>
</td>
</tr></table>

<apex:pageBlockTable id="app_list3" value="{!tableAppointments}" var="apt">
<apex:column headerValue="Action">
<apex:outputLink value="{!URLFOR('/' + apt.Id)}">
View
</apex:outputLink>
</apex:column>
<apex:column headerValue="Doctor's Name" value="{!apt.Doctor__c}"/>
<apex:column headerValue="Patient's Name" value="{!apt.Patient__c}"/>
<apex:column headerValue="Appointment Date" value="{!apt.Appointment_Date__c}"/>
<apex:column headerValue="Duration in minutes" value="{!apt.Duration__c}"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>

сделала так, но выдаёт уже ошибку Unknown property ‘HospitalController.doctorSelectOptionList’

Странно, должно работать. Сделал также как у вас, работает.
Controller

public with sharing class SelectListTest {
    public List<Lead> leads {get;set;}
    public List<SelectOption> leadsList {get;set;}

    public SelectListTest() {
        leadsList = new List<SelectOption>();
        leads = [SELECT id, Name FROM Lead];

        for (Lead lead : leads){ 
            leadsList.add(new SelectOption(lead.Id, lead.Name)); 
        }
              
    }
}

VF page

<apex:page docType="html-5.0" controller="SelectListTest" showHeader="false" sidebar="false">
<apex:form>
    <apex:pageBlock>
        <apex:pageBlockSection>
            <apex:selectList size="1">
                <apex:selectOptions value="{!leadsList}"/>
            </apex:selectList>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>
</apex:page>

Вот работающий код

<apex:page docType="html-5.0" controller="accountTestController" showHeader="false" sidebar="false">
	<apex:form >
		<apex:pageBlock title="Appointmets Table">
    <!-- Select a account -->
        <apex:pageBlockSection>
         Select an account:
            <apex:selectList size="1" value="{!selectedAccount}">
                 <apex:selectOptions value="{!accountSelectOptionList}"/>
            </apex:selectList>
        </apex:pageBlockSection>
		</apex:pageBlock>
	</apex:form>
</apex:page>


public class accountTestController {
    
    public String selectedAccount {get; set;}
    public List<SelectOption> accountSelectOptionList {get;set;}
    
    public accountTestController() {
        List<Account> accounts = [SELECT Name From Account];
        accountSelectOptionList = new List<SelectOption>();
        
        for(Account account : accounts) {
            accountSelectOptionList.add(new SelectOption(account.Id, account.Name)); 
        }
    }
}
2 Likes

Попробуйте пересохранить код.

2 Likes

Unknown method ‘HospitalController.addNewAppt()’ вылетает ошибка

Добавьте в контроллер такой метод. Его у Вас нет.

public PageReference addNewAppt() {
     
     // Здесь нужная вам логика
     return null;
}
1 Like
public class HospitalController {

public Appointment__c appt {get; set;}
public String selectedDoctor {get; set;}
public String selectedPatient {get; set;}
public Datetime appDate {get; set;}
public List<Doctor__c> DoctorList {get;set;}
public List<SelectOption> doctorSelectOptionList {get;set;}
public List<Patient__c> PatientList {get;set;}
public List<SelectOption> patientSelectOptionList {get;set;} 

public PageReference redirectToMyVF(Id accId) {	
PageReference myVFPage = new PageReference('/apex/myVFPage');
myVFPage.setRedirect(true);
myVFPage.getParameters().put('myId', accId);
return myVFPage;

}

public HospitalController() {
    this.appt = new Appointment__c();
    this.selectedDoctor = '';
    this.selectedPatient = '';
    this.appDate = System.now();
    
    
        
    doctorSelectOptionList = new List<SelectOption>();
    patientSelectOptionList = new List<SelectOption>();
 
    

    DoctorList = [SELECT iD, Name FROM Doctor__c];
    for (Doctor__c doc : DoctorList){ 
        doctorSelectOptionList.add(new SelectOption(doc.Id, doc.Name)); 
    }
         
    PatientList = [SELECT Id, Name FROM Patient__c];
    for (Patient__c patient : PatientList){
        patientSelectOptionList.add(new SelectOption(patient.Id, patient.Name)); 
    }
}
}

VF

<apex:page docType="html-5.0" controller="HospitalController" showHeader="false" sidebar="false">
<apex:form >
<apex:pageBlock title="Appointmets Table">


<!-- Select a doctor -->
<apex:pageBlockSection>
 Select a doctor:
<apex:selectList size="1" value="{!selectedDoctor}">
<apex:selectOptions value="{!doctorSelectOptionList}"/>
<apex:actionSupport event="onchange" 
reRender="app_list1, app_list2, app_list3"/>
</apex:selectList>
</apex:pageBlockSection>

<!-- Select a patient -->
<apex:pageBlockSection>
Select a patient:
<apex:selectList size="1">
<apex:selectOptions value="{!patientSelectOptionList}"/>
</apex:selectList>
</apex:pageBlockSection>


<!-- Appointment date -->
<apex:pageBlockSection >
<apex:inputField label="Appointment date" 
value="{!appt.Appointment_Date__c}"
required="false"/>
</apex:pageBlockSection>



<!-- Add new appointment button -->
<apex:pageBlockSection >
<apex:commandButton immediate="false" reRender="app_list3" action="{!addNewAppt}" value="Add New Appointment" />
</apex:pageBlockSection>
    
    
<!-- Pagination -->
<table style="width: 100%"><tr>
<td>
Page: <apex:outputText 
value="{!PageNumber} of {!CEILING(ResultSize / PageSize)}"/>
</td> 
<td align="center">
<!-- Previous page -->
<!-- active -->
<apex:commandLink action="{!Previous}" value="« Previous"
rendered="{!HasPrevious}"/>
<!-- inactive (no earlier pages) -->
<apex:outputText style="color: #ccc;" value="« Previous"
rendered="{!NOT(HasPrevious)}"/>
&nbsp;&nbsp; 
<!-- Next page -->
<!-- active -->
<apex:commandLink action="{!Next}" value="Next »"
rendered="{!HasNext}"/>
<!-- inactive (no more pages) -->
<apex:outputText style="color: #ccc;" value="Next »"
rendered="{!NOT(HasNext)}"/>
</td>

<td align="right">
<!-- Records per page -->
Records per page:
<apex:selectList value="{!PageSize}" size="1">
<apex:selectOption itemValue="5" itemLabel="5"/>
<apex:selectOption itemValue="20" itemLabel="20"/>
<apex:actionSupport event="onchange" reRender="contacts_list"/>
</apex:selectList>
</td>
</tr></table>

<apex:pageBlockTable id="app_list3" value="{!tableAppointments}" var="apt">
<apex:column headerValue="Action">
<apex:outputLink value="{!URLFOR('/' + apt.Id)}">
View
</apex:outputLink>
</apex:column>
<apex:column headerValue="Doctor's Name" value="{!apt.Doctor__c}"/>
<apex:column headerValue="Patient's Name" value="{!apt.Patient__c}"/>
<apex:column headerValue="Appointment Date" value="{!apt.Appointment_Date__c}"/>

</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>

в итоге сделала так, пишет ошибку Unknown method ‘HospitalController.addNewAppt()’
:upside_down_face::upside_down_face::upside_down_face:

Я не вижу этого метода у Вас в контроллере. Просто вставьте его после конструктора и сохраните изменения.

public PageReference addNewAppt() {
     
     // Здесь нужная вам логика
     return null;
}
1 Like

сделала, прошлой ошибки нету, но появилась новая Unknown property ‘HospitalController.PageNumber’

Добавьте в контроллер свойство
public Integer PageNumber {get; set;}

Если у Вас выдает ошибку Unknown property что-то, значит это что-то не объявлено в контроллере.

1 Like

Эта ошибка вылетает в случае, если у вас нет такого компонента в контроллере. В данном случае, метода. Убедитесь что вашем контроллере он существует.

1 Like

спасибо! теперь понятно! а если ошибка Unknown method ‘HospitalController.Previous()’ ? тоже метода нет?

Да, но иногда такая ошибка может быть если у метода нет приставки get, и при этом , метод предназначен для возврата записей(для таблицы например).

public class HospitalController {
 public Appointment__c appt {get; set;}
    public String selectedDoctor {get; set;}
    public String selectedPatient {get; set;}
    public Datetime appDate {get; set;}
    public List<Doctor__c> DoctorList {get;set;}
    public List<SelectOption> doctorSelectOptionList {get;set;}
    public List<Patient__c> PatientList {get;set;}
    public List<SelectOption> patientSelectOptionList {get;set;} 
    public Integer duration {get; set;}
    public PageReference addNewAppt() {
     return null;
}
    public Integer PageNumber {get; set;}
    Public Integer PageSize {get;set;}
    Public Integer ResultSize {get;set;} 


    public PageReference redirectToMyVF(Id accId) {	
	PageReference myVFPage = new PageReference('/apex/myVFPage');
	myVFPage.setRedirect(true);
	myVFPage.getParameters().put('myId', accId);
	return myVFPage;
}
 
    public HospitalController() {
        this.appt = new Appointment__c();
        this.selectedDoctor = '';
        this.selectedPatient = '';
        this.appDate = System.now();
        this.duration = 30;
        
        
            
        doctorSelectOptionList = new List<SelectOption>();
        patientSelectOptionList = new List<SelectOption>();
     
        

        DoctorList = [SELECT iD, Name FROM Doctor__c];
        for (Doctor__c doc : DoctorList){ 
            doctorSelectOptionList.add(new SelectOption(doc.Id, doc.Name)); 
        }
             
        PatientList = [SELECT Id, Name FROM Patient__c];
        for (Patient__c patient : PatientList){
            patientSelectOptionList.add(new SelectOption(patient.Id, patient.Name)); 
        }
    }
    }
<apex:page docType="html-5.0" controller="HospitalController" showHeader="false" sidebar="false">
<apex:form >
<apex:pageBlock title="Appointmets Table">


<!-- Select a doctor -->
<apex:pageBlockSection>
 Select a doctor:
<apex:selectList size="1" value="{!selectedDoctor}">
<apex:selectOptions value="{!doctorSelectOptionList}"/>
<apex:actionSupport event="onchange" 
reRender="app_list1, app_list2, app_list3"/>
</apex:selectList>
</apex:pageBlockSection>

<!-- Select a patient -->
<apex:pageBlockSection>
Select a patient:
<apex:selectList size="1">
<apex:selectOptions value="{!patientSelectOptionList}"/>
</apex:selectList>
</apex:pageBlockSection>


<!-- Appointment date -->
<apex:pageBlockSection >
<apex:inputField label="Appointment date" 
value="{!appt.Appointment_Date__c}"
required="false"/>
</apex:pageBlockSection>

<!-- Duration -->
            <apex:pageBlockSection >
                <apex:inputField label="Duration in minutes" 
                            value="{!appt.Duration_in_minutes__c}"
                           	required="false"/>
            </apex:pageBlockSection>
            

<!-- Add new appointment button -->
<apex:pageBlockSection >
<apex:commandButton immediate="false" reRender="app_list3" action="{!addNewAppt}" value="Add New Appointment" />
</apex:pageBlockSection>
    
    
<!-- Pagination -->
    
<table style="width: 100%"><tr>
<td>
Page: <apex:outputText 
value="{!PageNumber} of {!CEILING(ResultSize / PageSize)}"/>
</td> 
<td align="center">
<!-- Previous page -->
<!-- active -->
<apex:commandLink action="{! Previous }" value="« Previous"
rendered="{!HasPrevious }"/>
<!-- inactive (no earlier pages) -->
<apex:outputText style="color: #ccc;" value=" Previous"
rendered="{!NOT(HasPrevious) }"/>

<!-- Next page -->
<!-- active -->
<apex:commandLink action="{!Next}" value="Next »"
rendered="{!HasNext}"/>
<!-- inactive (no more pages) -->
<apex:outputText style="color: #ccc;" value="Next »"
rendered="{!NOT(HasNext)}"/>
</td>

<td align="right">
<!-- Records per page -->
Records per page:
<apex:selectList value="{!PageSize}" size="1">
<apex:selectOption itemValue="5" itemLabel="5"/>
<apex:selectOption itemValue="20" itemLabel="20"/>
<apex:actionSupport event="onchange" reRender="contacts_list"/>
</apex:selectList>
</td>
</tr></table>

<apex:pageBlockTable id="app_list3" value="{!tableAppointments}" var="apt">
<apex:column headerValue="Action">
<apex:outputLink value="{!URLFOR('/' + apt.Id)}">
View
</apex:outputLink>
</apex:column>
<apex:column headerValue="Doctor's Name" value="{!apt.Doctor__c}"/>
<apex:column headerValue="Patient's Name" value="{!apt.Patient__c}"/>
<apex:column headerValue="Appointment Date" value="{!apt.Appointment_Date__c}"/>
<apex:column headerValue="Duration in minutes" value="{!apt.Duration_in_minutes__c}"/>

</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>

код получился такой, но всё равно ошибка вылазит, я уже ничего не понимаю(((
Unknown method ‘HospitalController.Previous()’

А чего по вашему мнению не хватает в контроллере? Вы это уже делали ранее. Имена методов должны совпадать с теми что вызываются в контроллере. Вы пытаетесь обратиться в методу privious(), где VF page должна его найти ?

1 Like

понятно, я просто не очень пока что понимаю как добавлять методы в контроллер

Тогда стоит начать с прочтения вот этой темы: Старт в Salesforce и с чего начать?
А после найти на trailhead вот этот модуль: Quick Start Apex