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
Kevin Jackson 11Kevin Jackson 11 

Returning a date variable in APEX

This is probably just a novice error, but I could use some help on this.  I am trying to return a date field to use on my vf page.  I am taking a date from the Contacct object.  This is my method to get the Contact object:
//  IDENTIFY CONTACT ASSOCIATED WITH CURRENT USER
    public Contact getContact () {
        return[SELECT Id, name, email, MailingAddress, Phone, MailingCity, MailingState, 
                                 MailingCountry,     MailingPostalCode, MailingStreet, 
                                 Membership_Category__c, Membership_End_Date__c, KI_Status__c, 
                                 Membership_Other_Charateristic__c 
               FROM Contact 
               WHERE Id IN (SELECT 	ContactId
                             From User
                             WHERE Id=:UserInfo.getUserId())
              				 LIMIT 1];
	}

I then want to add some years to the date (to make it easy I am just using the integer 3).  I am obviously doing it wrong, but not sure how to fix.  Here is what I tried,
 
// UDPATE MEMBERSHIP END DATE
    public date newEndDate {get;set;}
    
    Public static date UpdateEndDate (){
        Date newEndDate = Contact.Membership_End_Date__c.addYears(3);
       return newEndDate;
   }


 
Best Answer chosen by Kevin Jackson 11
Vivian Charlie 1208Vivian Charlie 1208

Kevin,

 

I believe the updated code would be as below

1. Apex Class

public without sharing class className{
    public Contact objContact {get;set;} // only needed if you need to bind contact details on the VF page as well
    
    public date getNewEndDate(){
        Date dtReturnValue; // declare a variable with value as null to be returned in case no matches found
        list<Contact> lstC = [Select Id, Name, Birthdate from Contact limit 1]; // use desired condition in where clause
        
        // check if the query returned any results
        if(lstC != null && !lstC.isEmpty()){
            objContact = lstC[0]; // assigned 1st element of the list to our object instance
            
            // check if the contact has value assigned to the birthdate field
            if(objContact.Birthdate != null){
                dtReturnValue = objContact.Birthdate.addYears(3);
            }
        }
        return dtReturnValue;
    }
}

 

2. Visualforce Page

<apex:page controller="className"> {!newEndDate} </apex:page>

 

Thanks

Vivian

All Answers

Vivian Charlie 1208Vivian Charlie 1208

Hi Kevin,

 

I would suggest some changes

 

Public without sharing class className{
    public Contact objContact {get;set}
    public date newEndDate {get;set;}
    
    public className(){
        list<Contact> lstC = [Your contact query here LIMIT 1];
        if(lstC != null && !lstC.isEmpty()){
            objContact = lstC[0];
            newEndDate = objContact.Membership_End_Date__c.addYears(3);
        }
    }
}

Kevin Jackson 11Kevin Jackson 11
Thanks for the responses.  Not quite there yet.

DS I removed the static, it made it slightly better but it still causes problems.

Charlie,  it says that it is an invalid constructor with this line:      public className(){
Kevin Jackson 11Kevin Jackson 11
I have updated to this:
 
Public without sharing class className{
    public Contact objContact {get;set;}
    public date newEndDate {get;set;}
    
    public date xDate (){
        list<Contact> lstC = [SELECT Id, Membership_End_Date__c
               FROM Contact 
               WHERE Id IN (SELECT 	ContactId
                             From User
                             WHERE Id=:UserInfo.getUserId()) LIMIT 1];
        if(lstC != null && !lstC.isEmpty()){
            objContact = lstC[0];
            newEndDate = objContact.Membership_End_Date__c.addYears(3);
        }
        return newEndDate;
    }
}

But when I call it in VF using either {!xDate}  or {!xDate.newEndDate}  it says unknwon property 'myController.xDate'
Vivian Charlie 1208Vivian Charlie 1208

Kevin,

 

Use the newEndDate variable on the visualforce page instead of xDate where you display the date.

You can make xDate methods as public void xDate.There is no return needed at line 15. Does you page use only a controller or also standardcontroller

<apex:page controller="className"/>

Or

<apex:page standardcontroller= "ObjectName" extensions="className"/>

Kevin Jackson 11Kevin Jackson 11
Charlie,

I changed to public void.   In my VF I call {!newEndDate}   but I still get  unknwon property 'myController.newEndDate'

I am only using the controller I created,  No StandardController.

Cheers.
Vivian Charlie 1208Vivian Charlie 1208

Kevin,

 

I believe you are trying to call newEndDate as a method in some action attribute. Please can you post the VF page code.

 

Your end code should look a bit like this

public without sharing class className{
    public Contact objContact {get;set}
    public date newEndDate {get;set;}
    
    public className(){
        list<Contact> lstC = [Your contact query here LIMIT 1];
        if(lstC != null && !lstC.isEmpty()){
            objContact = lstC[0];
            newEndDate = objContact.Membership_End_Date__c.addYears(3);
        }
    }
}

<Apex:page controller = "className">
    <apex:form>
        <apex:input value="{!newEndDate}" type="date"/>
    </apex:form>
</Apex:page>

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_input.htm

Kevin Jackson 11Kevin Jackson 11
Charlie,

Here is the full page.

The relevant part is near the bottom in the outputlabel section.  I tried to add the code you suggested, but it gives me an error.  I want to display the field and not have it as an input.

Thanks for all your help here!

 
<apex:page controller="ParameterController">
    <h1>Your latest Membership Status</h1>
  
              <apex:pageBlock >
        <apex:pageBlockSection > 
            <apex:form >
                 
            <p> Membership Category: <b> {!Contact.Membership_Category__c}</b></p>
            <p>Sub Category:        <b>  {!Contact.Membership_Other_Charateristic__c}</b></p>
            <p> Membership End Date: <b> <apex:outputText value="{0, date, MMMM d','  yyyy}">
    <apex:param value="{!Contact.Membership_End_Date__c}" /> 
</apex:outputText> </b> </p>
               
          <p>  KI Access:       <b> {!Contact.KI_Status__c}</b></p><p></p>
         
          <p> How Long to extend your ISN membership?
          
  <apex:selectList size="1" value="{!SelectedValue}" label="SelectedValue" id="SelectedValue">
        <b>  <apex:selectOptions value="{!dynamiclist}" /></b>
       <p></p><apex:variable value="SelectedValue" var="Selection1"/>
             <apex:actionSupport event="onchange" reRender="one" action="{!Selecty}" />
             
      
            
</apex:selectList></p><p></p>


         <!--  <Apex:commandButton action="{!createMembership}" value="Create" /> -->
           <Apex:commandButton action="{!gotoPage2}" value="Make Payment" />
      <Apex:commandButton action="{!createMembership}" value="Create" /> 
 

    
             </apex:form>            
            
        <apex:outputLabel id="one" > <b>     You have selected: {!selectedValue} years.       
   The total cost will be: ${!selectedValue*203} 
       Your End date will be: {!newEndDate}  </b> </apex:outputLabel>       
     
         </apex:pageBlockSection>
    </apex:pageBlock> 
</apex:page>

 
Vivian Charlie 1208Vivian Charlie 1208

Kevin,

 

Just to verify

1. The newEndDate variable is part of ParameterController class

2. The newEndDate variable is declared as public date newEndDate {get;set;}

Kevin Jackson 11Kevin Jackson 11
yes to both.
 
Vivian Charlie 1208Vivian Charlie 1208
Kevin,


Just to verify, in one of your previous comment you specified that you created a new class (className) as suggested by me, with the code changes discussed.

Public without sharing class className{
    public Contact objContact {get;set;}
    public date newEndDate {get;set;}
    
    public date xDate (){
        list<Contact> lstC = [SELECT Id, Membership_End_Date__c
               FROM Contact 
               WHERE Id IN (SELECT     ContactId
                             From User
                             WHERE Id=:UserInfo.getUserId()) LIMIT 1];
        if(lstC != null && !lstC.isEmpty()){
            objContact = lstC[0];
            newEndDate = objContact.Membership_End_Date__c.addYears(3);
        }
        return newEndDate;
    }
}


In the previous comment with the VF page code I see that the code reads
<apex:page controller="ParameterController">, How are the ParameterController class and className class related? Are the separate classes you have?


Below is a code snippet I used to test variable assignment and access on the VF page and everything works fine.

public class ExampleController {
    public DateTime t1 {get;set;}
    
    public ExampleController (){
        t1 = System.now();
    }
}

<apex:page controller="ExampleController">
    T1: {!t1} <br/>
    <apex:form >
        <apex:commandLink value="refresh"/>
    </apex:form>
</apex:page>
Kevin Jackson 11Kevin Jackson 11
Charlie,

It is solved now.  I used your suggestions with one small change:

public date getNewEndDate() {

The get was important!  

If you repost your first answer and just add the get then I will mark as the best answer.  I do not want to mark the others as best answers, because they were incomplete and I want others to be able to benefit.

Thanks SO much for your help!
Vivian Charlie 1208Vivian Charlie 1208

Kevin,

 

I believe the updated code would be as below

1. Apex Class

public without sharing class className{
    public Contact objContact {get;set;} // only needed if you need to bind contact details on the VF page as well
    
    public date getNewEndDate(){
        Date dtReturnValue; // declare a variable with value as null to be returned in case no matches found
        list<Contact> lstC = [Select Id, Name, Birthdate from Contact limit 1]; // use desired condition in where clause
        
        // check if the query returned any results
        if(lstC != null && !lstC.isEmpty()){
            objContact = lstC[0]; // assigned 1st element of the list to our object instance
            
            // check if the contact has value assigned to the birthdate field
            if(objContact.Birthdate != null){
                dtReturnValue = objContact.Birthdate.addYears(3);
            }
        }
        return dtReturnValue;
    }
}

 

2. Visualforce Page

<apex:page controller="className"> {!newEndDate} </apex:page>

 

Thanks

Vivian

This was selected as the best answer