+ Start a Discussion
Javier CastroJavier Castro 

Get date from salesforce to visualforce

Hi, I'm new in Salesforce and I'm making a visualforce:
I have this summarized visualforce code:
<!--The code is summarized, this is not my complete code-->

<div>
<apex:form id="tabla2">
        <apex:pageBlock >
          <apex:pageBlockTable value="{!lstCitas}" var="citas" styleClass="table table-hover">
            <apex:column value="{!citas.Name}"/>
            <apex:column value="{!citas.CloseDate}"/>
            <apex:column >
              <apex:commandButton value="My button" styleClass="btn btn-primary" action="{!mostrarCitaIndividualDoctor}" rerender="p1">
                  <apex:param name="nombre" value="{!citas.Name}" assignTo="{!nombre}"/>
                  <apex:param name="fecha" value="{!citas.CloseDate}" assignTo="{!fecha}"/>
              </apex:commandButton>
            </apex:column>
          </apex:pageBlockTable>
        </apex:pageBlock>
</apex:form>
</div>

<!--More code-->

<div id="p1">
<apex:form >
              <label>Nombre: {!nombreC}</label>
              <label>Fecha de cierre: {!fechaC}</label>
</apex:form>
</div>

And my summarized controller code:
 
public class controlador{

    public String nombre {get; set;}
    public String fecha {get; set;}
    public String nombreC {get; set;}
    public String fechaC{get; set;}
   
    /*More code*/

    public void mostrarCitaIndividualDoctor(){
        nombreC = this.nombre;
        fechaC = this.fecha;
    }

    /*More code*/
}
The table shows the values properly, when I click the button, I get the name and the date, and I show this values in the other div(p1). It works for the name but in the date I always get a null value.
I have tried to change to Date the varaible in the controller but it still doesn't work for me.

How can I get the date?

Thanks in advance.

 
Best Answer chosen by Javier Castro
Javier CastroJavier Castro
Thanks for your reply Axel, but unfortunately it doesn't work for me. 
To solve my issue I've had to add another <apex:param> like this:
<apex:param name="idcita" value="{!citas.Id}" assignTo="{!idCita}"/>

Then, I've had to change to Date my var and make the query in my controller
 
public String nombre {get; set;}
public Date fecha {get; set;}
public String nombreC {get; set;}
public String fechaC{get; set;}

fecha = [SELECT CloseDate FROM Opportunity WHERE Id = :idCita LIMIT 1].CloseDate;

​And finally format the date as I wanted
 
fechaC = DateTime.newInstance(fecha.year(), fecha.month(), fecha.day()).format('DD/MM/YYYY');

I will check my complete code because I should be able to get the date without making the query, but for now, this is what is working for me. 
Thanks anyway for the inconveniences.
 

All Answers

Axel DuhêmeAxel Duhême
Hi Javier,

Maybe you could try something like this?

<apex:column>
    <apex:outputText value="{0, date, MMMM d',' yyyy}">
        <apex:param value="{!citas.CloseDate}" />
    </apex:outputText>
</apex:column>

I got the idea from the top reply on this post:
https://salesforce.stackexchange.com/questions/22321/visualforce-date-formatting

Cheers,

Axel.
Javier CastroJavier Castro
Thanks for your reply Axel, but unfortunately it doesn't work for me. 
To solve my issue I've had to add another <apex:param> like this:
<apex:param name="idcita" value="{!citas.Id}" assignTo="{!idCita}"/>

Then, I've had to change to Date my var and make the query in my controller
 
public String nombre {get; set;}
public Date fecha {get; set;}
public String nombreC {get; set;}
public String fechaC{get; set;}

fecha = [SELECT CloseDate FROM Opportunity WHERE Id = :idCita LIMIT 1].CloseDate;

​And finally format the date as I wanted
 
fechaC = DateTime.newInstance(fecha.year(), fecha.month(), fecha.day()).format('DD/MM/YYYY');

I will check my complete code because I should be able to get the date without making the query, but for now, this is what is working for me. 
Thanks anyway for the inconveniences.
 
This was selected as the best answer
Axel DuhêmeAxel Duhême
I'm glad you got it firgured out somehow! Good day to you.