function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Laytro80Laytro80 

Simple question newbie and running out of time

Below is a simple VF page and Controller. You will not on the Controller I hard code the

 

 

where nights__c = 4

 

 I want to make this dynamic using a input field on the VF page.

 

 

 

VF Page

 

 

<apex:page controller="Resmanager">
    <apex:sectionHeader title="Availability Search"/> 
                 
              <apex:pageBlock title="Reservations">
              <apex:pageBlockSection >
                    <apex:pageBlockTable value="{!Reservations}" var="res">
                        <apex:column value="{!res.Start_Date__c}"  />
                        <apex:column value="{!res.End_Date__c}"  />
                        <apex:column value="{!res.Nights__c}"  />
                    </apex:pageBlockTable>
               </apex:pageBlockSection>
               
               </apex:pageBlock>

</apex:page>

 

 

APEX Controller

 

 

public class Resmanager {
    
    public List<Reservation__c> getReservations(){
        List<Reservation__c> listBd=[select r.id, r.Start_Date__c, R.End_Date__c, nights__c from Reservation__c r where nights__c = 4];
        return listBd;   
    }
   
}

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
dfiredfire

Now I am just fumbling around as well, being I am some what new to apex myself. try this;

 

   Integer minNumNights {set;

get {
if(this == null)
this = mnnDefault;
return minNumNights;
} }

All Answers

dfiredfire

Why not create a field in your controller called something like

 

 

Integer minNumNights;
Integer mnnDefault = 4;

public getMinNumNights() {
if(minNumNights == null)
minNumNights = mnnDefault;
return minNumNights;
}

 

 

 

 

 

Then in your VF page something like

<apex:inputtext value="{!min_num_nights}"/>

where you can dynamically change the value

 

Then in your SOQL query

 

List<Reservation__c> listBd=[select r.id, r.Start_Date__c, R.End_Date__c, nights__c from Reservation__c r where nights__c = :min_num_nights];
dfiredfire

typo, meant to write

List listBd=[select r.id, r.Start_Date__c, R.End_Date__c, nights__c from Reservation__c r where nights__c = :minNumNights];

 

 

Laytro80Laytro80

Thanks for this I get this error - Resmanager Compile Error: Invalid constructor name: getMinNumNights at line 6 column 12


 

public class Resmanager {

    Integer minNumNights;
    Integer mnnDefault = 4;

    public getMinNumNights() {
       if(minNumNights == null)
          minNumNights = mnnDefault;
       return minNumNights;
    }  
        
    public List<Reservation__c> getReservations(){
        List<Reservation__c> listBd=[select r.id, r.Start_Date__c, R.End_Date__c, nights__c from Reservation__c r where nights__c = :minNumNights];
        return listBd;   
    }
   
}

 

 

<apex:page controller="Resmanager">
    <apex:sectionHeader title="Availability Search"/> 
        <apex:form >
            <apex:pageBlock mode="edit" id="block"> 
                <apex:pageBlockSection >        
                <apex:pageBlockSectionItem >
                            <apex:outputLabel for="searchText">Search Text</apex:outputLabel>    
                            <apex:inputtext value="{!minNumNights}"/>                    
                </apex:pageBlockSectionItem>
                </apex:pageBlockSection>
                </apex:pageBlock>    
              <apex:pageBlock title="Reservations">
              <apex:pageBlockSection >
                    <apex:pageBlockTable value="{!Reservations}" var="res">
                        <apex:column value="{!res.Start_Date__c}"  />
                        <apex:column value="{!res.End_Date__c}"  />
                        <apex:column value="{!res.Nights__c}"  />
                    </apex:pageBlockTable>
               </apex:pageBlockSection>               
               </apex:pageBlock>
     </apex:form>
</apex:page>

 

 

 

dfiredfire

sorry I typed to quickly.

 

you need to specify the return type so it should be

 

    public Integer getMinNumNights() {
if(minNumNights == null)
minNumNights = mnnDefault;
return minNumNights;
}
Laytro80Laytro80

Thanks for the fast reply, that fixed the problem but I now have another issue - sorry! 

 

I get this error -> Read only property '[Resmanager].minNumNights'

 

 

    <apex:sectionHeader title="Availability Search"/> 
        <apex:form >
            <apex:pageBlock mode="edit" id="block"> 
                <apex:pageBlockSection >        
                <apex:pageBlockSectionItem >
                            <apex:outputLabel for="searchText">Amount of Nights</apex:outputLabel>    
                            <apex:inputtext value="{!minNumNights}"/>                    
                </apex:pageBlockSectionItem>
                </apex:pageBlockSection>
                </apex:pageBlock>    
              <apex:pageBlock title="Reservations">
              <apex:pageBlockSection >
                    <apex:pageBlockTable value="{!Reservations}" var="res">
                        <apex:column value="{!res.Start_Date__c}"  />
                        <apex:column value="{!res.End_Date__c}"  />
                        <apex:column value="{!res.Nights__c}"  />
                    </apex:pageBlockTable>
               </apex:pageBlockSection>               
               </apex:pageBlock>
     </apex:form>

 

 

dfiredfire

Please don't apologize. I also ask for help plenty of times.

 

The fault is mine. I forgot to include that you need to also create a setter. Unless you need to do some processing of the value when setting, which I don't think you do in this case, use the following when specifying the field

 

Integer minNumNights {set;}

 

this will create a default setter to allow you to set the variable.

laytro1978laytro1978

Thanks I think we are getting closer.

 

Error: Resmanager Compile Error: Variable is not visible: minNumNights at line 7 column 11 

 

 

public class Resmanager {

    Integer mnnDefault = 0;
    Integer minNumNights {set;}

    public Integer getMinNumNights() {
       if(minNumNights == null)
          minNumNights = mnnDefault;
       return minNumNights;
    }   
        
    public List<Reservation__c> getReservations(){
        List<Reservation__c> listBd=[select r.id, r.Start_Date__c, R.End_Date__c, nights__c from Reservation__c r where nights__c = :minNumNights];
        return listBd;   
    }
   
}

 

 

dfiredfire

Apparently you need to create the get; as well. I thought that since you are creating a get method, that is was unncessary but I added it and the error went away.

 

so try

Integer minNumNights {get; set;}

 

and see what happens

laytro1978laytro1978

I back to the

Error: Read only property '[Resmanager].minNumNights'

 

public class Resmanager {

    Integer minNumNights {get; set;}
    Integer mnnDefault = 4;

    public Integer getMinNumNights() {
       if(minNumNights == null)
          minNumNights = mnnDefault;
       return minNumNights;
    }   
        
    public List<Reservation__c> getReservations(){
        List<Reservation__c> listBd=[select r.id, r.Start_Date__c, R.End_Date__c, nights__c from Reservation__c r where nights__c = :minNumNights];
        return listBd;   
    }
   
}

 

 

<apex:page controller="Resmanager">
    <apex:sectionHeader title="Availability Search"/> 
        <apex:form >
            <apex:pageBlock mode="edit" id="block"> 
                <apex:pageBlockSection >        
                <apex:pageBlockSectionItem >
                            <apex:outputLabel for="searchText">Amount of Nights</apex:outputLabel>    
                            <apex:inputtext value="{!minNumNights}"/>               
                </apex:pageBlockSectionItem>
                </apex:pageBlockSection>
                </apex:pageBlock>    
              <apex:pageBlock title="Reservations">
              <apex:pageBlockSection >
                    <apex:pageBlockTable value="{!Reservations}" var="res">
                        <apex:column value="{!res.Start_Date__c}"  />
                        <apex:column value="{!res.End_Date__c}"  />
                        <apex:column value="{!res.Nights__c}"  />
                    </apex:pageBlockTable>
               </apex:pageBlockSection>               
               </apex:pageBlock>
     </apex:form>
</apex:page>

 

 

 

 

dfiredfire

Now I am just fumbling around as well, being I am some what new to apex myself. try this;

 

   Integer minNumNights {set;

get {
if(this == null)
this = mnnDefault;
return minNumNights;
} }
This was selected as the best answer
laytro1978laytro1978

Still having problems but have changed a bit of the code.  I think the read only problem was because I was not using the right type.  Now I can render a field and if I hit enter with or without a number the query runs but it only brings back the default minimum so it's ignoring the field but it seems like progress.

 

To be honest this query came from somebody that want a quick solution.  I have now built a report which is a temporary fix and buys me a few weeks.

 

Here is the VF page

 

 

<apex:page controller="Resmanager">
    <apex:sectionHeader title="Availability Search"/> 
        <apex:form >
             <apex:pageBlock mode="edit" id="block"> 
                <apex:pageBlockButtons >
                </apex:pageBlockButtons>
                <apex:pageBlockSection >        
                    <apex:pageBlockSectionItem >
                                <apex:outputLabel for="searchText">Amount of Nights</apex:outputLabel>  
                                <input type="number" name="{!minNumNights}"/>  
                    </apex:pageBlockSectionItem>
                </apex:pageBlockSection>
                </apex:pageBlock>    
              <apex:pageBlock title="Reservations">
                  <apex:pageBlockSection >
                        <apex:pageBlockTable value="{!Reservations}" var="res">
                            <apex:column value="{!res.Start_Date__c}"  />
                            <apex:column value="{!res.End_Date__c}"  />
                            <apex:column value="{!res.Nights__c}"  />
                        </apex:pageBlockTable>
                   </apex:pageBlockSection>               
               </apex:pageBlock>
     </apex:form>
</apex:page>

 

 

And the APEX code

 

 

public class Resmanager {

    Integer minNumNights {get; set;}
    Integer mnnDefault = 4;

    public Integer getMinNumNights() {
       if(minNumNights == null)
          minNumNights = mnnDefault;
       return minNumNights;
    }   
        
    public List<Reservation__c> getReservations(){
        List<Reservation__c> listBd=[select r.id, r.Start_Date__c, R.End_Date__c, nights__c from Reservation__c r where nights__c = :minNumNights];
        return listBd;   
    }
   
}

 

 

Thanks again for all your help.

 

Cheers R

 

 

laytro1978laytro1978

Thanks again for all your help got there in the end.

 

 

dfiredfire

glad it worked out for you. :manhappy