+ Start a Discussion
Tina HadariTina Hadari 

Passing a datetime parameter from visualforce being converted to string automatically.

So I have a set of three visual force pages. Each is a pageblock table showing results from custom controller that is limiting based on parameters passed from commandlinks on the previous pages. One of the limiting parameters is a datetime, however when I pass the parameter to the controller it comes out as a string. Either I am converting this somewhere (can the list<object> handle datetime fields?) or the visualforce page is converting the datetime field to a string that looks like this:

Fri Nov 30 21:55:38 GMT 2012

And I need to either cast this back into a datetime variable or figure out how to pass it as a datetime. Any help that could be provided would be really appreciated! 

 Here is my controller:
 
public with sharing class attendance_class_joins {

    // an instance variable for the standard controller
    private ApexPages.StandardController controller {get; set;}
    // the object being referenced via url
    //private Lesson__c lesson {get; set;}
    // the variable getting set from the command button 
    public String className {get; set;}
    public String classTimeStr {get; set;}
    //public DateTime ClassTime {get; set;}
    
    
    //---------------------------------------------------------------------
    //Create list for student attendance of all active classes 
    //by using SOQL (seriously salesforce?) to generate list
    
    public ApexPages.StandardSetController activeclassList {
       get {
            if(activeclassList == null) {
                activeclassList = new ApexPages.StandardSetController(
                    Database.getQueryLocator([SELECT Name, Teacher__r.name 
                                              FROM Lesson__c 
                                              WHERE Inactive_lesson__c = False]));
            }
            return activeclassList;
        }
        set;
    }
    
    // Initialize setCon and return a list of records
    public List<Lesson__c> getActiveClasses() {
        return (List<Lesson__c>) activeClassList.getRecords();
    }
    
    
    // handle the action of the commandButton
    public PageReference processLinkClickSA() {
        System.debug('className: '+className);
        return page.lessonselection;
        //Now do something
    	 //go to page http://cs16.salesforce.com/apex/lessonselection
        //return null;
    }
    //------------------------------------------------------------------
    //Create the list for the lesson selection page.
    
    public ApexPages.StandardSetController classtimeList {
       get {
            if(classtimeList == null) {
                classtimeList = new ApexPages.StandardSetController(
                    Database.getQueryLocator([SELECT Name, Date_Time__c 
                                              FROM Lesson_Attendance__c 
                                              WHERE Lesson_ID__r.Name in (:className)
                                              AND Attendance__c = Null]));
            }
            return classtimeList;
        }
        set;
    }
    
    // Initialize setCon and return a list of records
    public List<Lesson_Attendance__c> getClassTimes() {
        return (List<Lesson_Attendance__c>) classtimeList.getRecords();
    }
    
    // handle the action of the commandButton
    public PageReference processLinkClickLS() {
        System.debug('className: '+className);
        //datetime classTime = DateTime.parse(classTimeStr);
        return page.takeroll;
    	 //Now do something
    	 //go to page http://cs16.salesforce.com/apex/lessonselection
        //return null;
    }
    
    //---------------------------------------------------------
    
    //serialize the date/time function gotten on the last page. 
    //datetime classTime = DateTime.parse(classTimeStr);
    //public DateTime classTimeStr() {
    //datetime classTime = ApexPages.currentPage().getParameters().get('classTimeStr');
    //return (DateTime)Json.deserialize(classTimeStr, DateTime.class);
	//}
    //Create the list for the take roll page
 	public ApexPages.StandardSetController rollcallList {
       get {
            if(rollcallList == null) {
                rollcallList = new ApexPages.StandardSetController(
                    Database.getQueryLocator([SELECT Student_ID__r.Name, Attendance__c, Notes__c 
                                              FROM Lesson_Attendance__c
                                              WHERE Lesson_ID__r.Name in (:className)]));
                                              	//AND Date_Time__c = :classTime]));
            }
            return rollcallList;
        }
        set;
    }
    
    // Initialize Roll call and return a list of records from the above query.
    public List<Lesson_Attendance__c> RollCall {get {
        return (List<Lesson_Attendance__c>) RollCallList.getRecords();
    }}
    
    //Custom save functionality.
    public PageReference save() {
        update RollCall;
        return page.Test_Page;
    }

}
And here are my visual force pages
 
<apex:page controller="attendance_class_joins">
  <h1>Student Attendance</h1>
  <h1>Student Attendance</h1>
  <body>This is where you can take roll and check students attendance records.</body>
  <body>Please select the class series you would like to take attendance for: </body>
      
      <apex:form >
     <apex:pageBlock title="List of Active Classes">
          <apex:pageBlockSection columns="1" >
              <apex:pageBlockTable value="{!ActiveClasses}" var="c">
                  <apex:column headerValue="Lesson Name">
                      <apex:outputText value="{!c.Name}"/>
                  </apex:column>
                  <apex:column headerValue="Teacher">
                      <apex:outputText value="{!c.Teacher__r.Name}"/>
                  </apex:column>
                  <apex:column headerValue="Choose Class">
                  	<apex:commandLink value="Next" action="{!processLinkClickSA}">
                        <apex:param name="classNameParam"
                                    value="{!c.Name}"
                                    assignTo="{!className}"/>
                  </apex:commandLink>
                  </apex:column>
                </apex:pageBlockTable>
          </apex:pageBlockSection>
      </apex:pageBlock>
    </apex:form>


</apex:page>




<apex:page controller="attendance_class_joins">
  <h1>Class Selection</h1>
  <body>Select the individual meeting of {!className} that you would like to take attendance for:</body>
      <apex:Form >
    	<apex:pageBlock title="List of {!className} Sessions">
          <apex:pageBlockSection columns="1" >
              <apex:pageBlockTable value="{!ClassTimes}" var="d">
                  <apex:column headerValue="Date/Time">
                      <apex:outputText value="{0,date,yyyy-MM-dd HH:mm:ss}">
                          <apex:param value="{!d.Date_Time__c}"/>
                          </apex:outputText>
                  </apex:column>
              		<apex:column headerValue="Choose Class">
                  		<apex:commandLink value="Next" action="{!processLinkClickLS}">
                        	<apex:param name="classTimeParam"
                                        value="{!d.Date_Time__c}"
                                		assignTo="{!classTimeStr}"/>
                  		</apex:commandLink>
                  	</apex:column>  
              </apex:pageBlockTable>
          </apex:pageBlockSection>
      </apex:pageBlock>
    </apex:Form>
</apex:page>


<apex:page controller="attendance_class_joins">
    <h1>
        Take roll call, select the correct attendance value from the dropdown for each student and add notes where appropriate. Then click save to commit.
    </h1>
    <apex:form >
        <apex:pageBlock title="Roll Call" >
            <apex:pageMessages />
            <apex:pageBlockButtons >
                <apex:commandButton value="Save"
                                    action="{!save}"/>
            </apex:pageBlockButtons>
            <apex:pageBlockTable value="{!rollcall}" var="stud">
                <apex:column headerValue="Student">
                    <apex:outputText value="{!stud.Student_ID__r.Name}"/>
                </apex:column>
                <apex:column headerValue="Attended?">
                    <apex:inputField value="{!stud.Attendance__c}"/>
                </apex:column>
                <apex:column headerValue="Notes">
                    <apex:inputField value="{!stud.Notes__c}"/>
                </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>




 
Blake TanonBlake Tanon
I think it's coming in as a String because you're using inputText instead of inputField,  you're talking about the d.date_time__c field correct?
Tina HadariTina Hadari
Yep, I am trying to pull that field into a variable in the controller, but it seems to be casting it as a string. 

Now I just tried to switch the outputText to an outputField in the SalesForce page and I am still encountering the error, is this what you where referring too?

But it did send me down a path to make sure that you could assign date/time values to a param, and I am not sure you can. Any thoughts?

Thanks for your response.
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi Tina,

Why are you not using DateTime Property in your class, i think it should aotomatically be DateTime then.
apex;param tag does not have its own data type defibation tag.
public DateTime classTimeStr {get; set;}
Tina HadariTina Hadari
That was originally how I had it. If I try to make that variable a datetime variable I get the following error:

Visualforce ErrorHelp for this Page
Value 'Tue Feb 10 00:00:00 GMT 2015' cannot be converted from Text to DateTime
Error is in expression '{!classTime}' in page lessonselection


I changed it from a datetime to string so that I could see if I could write something to parse the string and create a datetime value, but figured there must be an easier way. 
Tina HadariTina Hadari
Just to be clear I changed the code the visual force page to assign the parameter to the datetime value !classTime as well. 
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi Tina,

Try to convert string to DateTime As below in your code.
 
String stringDate = '2013-10-05 20:03:20';

Datetime myDate = datetime.valueOf(stringDate);
Let us know if it helps you.
 
Tina HadariTina Hadari
Hmmm, it doesn't seem to like the string we are feeding it:

System.TypeException: Invalid date/time: Fri Nov 30 21:55:38 GMT 2012
Class.attendance_class_joins.<init>: line 12, column 1

Thanks for all your help so far! Any other thoughts?
Laura SpellmanLaura Spellman
Hey there! I see that this is an old post, but I have come across the same issue. Did you find a solution?