You need to sign in to do that
Don't have an account?
Kevin 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:
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,
// 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; }
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
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);
}
}
}
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(){
But when I call it in VF using either {!xDate} or {!xDate.newEndDate} it says unknwon property 'myController.xDate'
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"/>
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.
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
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!
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;}
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>
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!
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