+ Start a Discussion
Khalid AbdullahKhalid Abdullah 

VisualForce: Why won't VF grab my variable?

Hello all!

I have the following sets of code intended to display any upcoming meetings directly on a contact's page:

Apex Class
public with sharing class UpcomingMeetings {
    public List<Event> events { get; private set; }
    public List<Event> evt {get; private set;}    
    public String StartDateVF1 {get; private set; }
    public String StartDateVF2 {get; private set; }
    public String summary { get; private set; }
    public String riaAdvisor { get; private set; }
    
     
    public UpcomingMeetings(ApexPages.StandardController controller) {
        Id contactId = controller.getRecord().Id;   
       system.debug('Contact id ---'+ contactId);
 
       Contact c = [SELECT id,Is_RIA_Advisor__c,RIA_Wholesaler_Name__c from Contact where Id=:contactId];
        
       riaAdvisor='';
       if (c.Is_RIA_Advisor__c)
        {
          riaAdvisor = 'This is a RIA contact. Please direct all calls to '+c.RIA_Wholesaler_Name__c+'<br/>';
        }      
        

       
        events = new List<Event>();
        try {
            
            Set<Id> eventIds = new Set<Id>();
            for (EventRelation e : [SELECT EventId FROM EventRelation  WHERE RelationId =:contactId AND Status NOT IN ('Declined','Uninvited')]) {
               eventIds.add(e.EventId);
            }
               events = [SELECT Id, Subject, StartDateTime, StartDateUTC__c, EndDateTime, IsAllDayEvent FROM Event WHERE (WhoId=:contactId OR Id IN :eventIds) AND Event.Cancelled__c <> TRUE AND ActivityDate>=TODAY ORDER BY ActivityDate ASC];             
            
            for(Event evt : events) {
                DateTime dt = DateTime.now();
                
                String StartDateVF1 = dt.format('MM/DD/YYYY');
                String StartDateVF2 = dt.format('MM/DD/YYYY HH:MM');
                if(evt.IsAllDayEvent == true) {
                    StartDateVF1 = evt.StartDateUTC__c;
                } else {
                    StartDateVF2 = evt.StartDateUTC__c;
                }
           }
VisualForce page:
<apex:page standardController="Contact" extensions="UpcomingMeetings" sidebar="false" showHeader="false">
    <style type="text/css">
    .meetingToday {
//        font-weight:bold;
        font-style:italic
    }
    </style>

    <apex:pageMessage severity="INFO" strength="1" summary="{!summary}" escape="false">
        <apex:dataTable value="{!events}" var="e" width="100%" cellpadding="1" cellspacing="1" rows="3">
            <apex:column >
                <li/>
            </apex:column>
            <apex:column >
                <apex:outputLink value="/{!e.Id}" target="_blank" styleClass="{!IF(DATEVALUE(e.StartDateTime)<TODAY(),'meetingToday','')}"><apex:outputField value="{!e.Subject}"/></apex:outputLink>
            </apex:column>
            <apex:column styleClass="{!IF(DATEVALUE(e.StartDateTime)<TODAY(),'meetingToday','')}">
                <apex:outputText value="from"/>
            </apex:column>
            <apex:column styleClass="{!IF(DATEVALUE(e.StartDateTime)<TODAY(),'meetingToday','')}">
                <apex:outputText value="{!IF(e.IsAllDayEvent == true, StartDateVF1, StartDateVF2)}"/>
            </apex:column>

The error message I'm getting is "Unknown Property 'ContactStandardController.StartDateVF1".

I know this means I haven't assigned the StartDateVF1 and StartDateVF2 variables to the contact relationship yet, but I'm not sure how to do this. Would anyone be able to help me?
Temoc MunozTemoc Munoz
Hi Khalid.

You will need to create a Wrapper class for Events that relates both StarDateVF1 and StartDateVF2 
 
public with sharing class UpcomingMeetings 
{
    public List<Event> evt {get; private set;}   
    public String summary { get; private set; }
    public String riaAdvisor { get; private set; }
    public List<EventWrapper> evtWrapper {get; set;}

    ......
    Line 31: 
    List<Event> events = [SELECT Id, Subject, StartDateTime, StartDateUTC__c, EndDateTime, IsAllDayEvent FROM Event WHERE (WhoId=:contactId OR Id IN :eventIds) AND Event.Cancelled__c <>TRUE AND ActivityDate>=TODAY ORDER BY ActivityDate ASC]; 

    evtWrapper = new List<EventWrapper>();

    for(Event evt : events) {
               EventWrapper evtW = new EventWrapper();
               evtW.event = evt;
               DateTime dt = DateTime.now();
                
               evtW.StarDateVF1  = dt.format('MM/DD/YYYY');
               evtW.StartDateVF2 = dt.format('MM/DD/YYYY HH:MM');
               if(evt.IsAllDayEvent == true) {
                   evtW.StartDateVF1 = evt.StartDateUTC__c;
               } else {
                   evtW.StartDateVF2 = evt.StartDateUTC__c;
               }
               evtWrapper.add(evtW);
    }
 ........................
    class EventWrapper
    {
        public Event evt { get; private set; }
        public String StartDateVF1 {get; private set; }
        public String StartDateVF2 {get; private set; }
    
        public EventWrapper(){}
    }
}

And in your VisualForce Page:
<apex:dataTable value="{!evtWrapper}" var="e" width="100%" cellpadding="1" cellspacing="1"rows="3">
....
<apex:outputText value="{!IF(e.IsAllDayEvent == true, e.StartDateVF1, e.StartDateVF2)}"/>


Let me know if you need help with the complete code.
 

Thanks
 

Temoc MunozTemoc Munoz
Also, you may need to set this variable like this:
Before:
public Event evt { get; private set; }
public String StartDateVF1 {get; private set; }
public String StartDateVF2 {get; private set; }

After:
 
public Event evt { get; set; }
public String StartDateVF1 {get; set; }
public String StartDateVF2 {get; set; }

 
Amit Chaudhary 8Amit Chaudhary 8

Please update your setter as public like below code.

public List<Event> events { get; set; }
public List<Event> evt {get; set;}
public String StartDateVF1 {get; set; }
public String StartDateVF2 {get; set; }
public String summary { get; set; }
public String riaAdvisor { get; set; }
 
public with sharing class UpcomingMeetings {
    public List<Event> events { get; set; }
    public List<Event> evt {get; set;}    
    public String StartDateVF1 {get; set; }
    public String StartDateVF2 {get; set; }
    public String summary { get; set; }
    public String riaAdvisor { get; set; }
    
     
    public UpcomingMeetings(ApexPages.StandardController controller) {
        Id contactId = controller.getRecord().Id;   
       system.debug('Contact id ---'+ contactId);
 
       Contact c = [SELECT id,Is_RIA_Advisor__c,RIA_Wholesaler_Name__c from Contact where Id=:contactId];
        
       riaAdvisor='';
       if (c.Is_RIA_Advisor__c)
        {
          riaAdvisor = 'This is a RIA contact. Please direct all calls to '+c.RIA_Wholesaler_Name__c+'<br/>';
        }      
        

       
        events = new List<Event>();
        try {
            
            Set<Id> eventIds = new Set<Id>();
            for (EventRelation e : [SELECT EventId FROM EventRelation  WHERE RelationId =:contactId AND Status NOT IN ('Declined','Uninvited')]) {
               eventIds.add(e.EventId);
            }
               events = [SELECT Id, Subject, StartDateTime, StartDateUTC__c, EndDateTime, IsAllDayEvent FROM Event WHERE (WhoId=:contactId OR Id IN :eventIds) AND Event.Cancelled__c <> TRUE AND ActivityDate>=TODAY ORDER BY ActivityDate ASC];             
            
            for(Event evt : events) {
                DateTime dt = DateTime.now();
                
                String StartDateVF1 = dt.format('MM/DD/YYYY');
                String StartDateVF2 = dt.format('MM/DD/YYYY HH:MM');
                if(evt.IsAllDayEvent == true) {
                    StartDateVF1 = evt.StartDateUTC__c;
                } else {
                    StartDateVF2 = evt.StartDateUTC__c;
                }
           }

Let us know if this will help you
 
Khalid AbdullahKhalid Abdullah
Hello Temoc,

Ran into a problem on the visualforce page.

The apex class you created for me works well, but I had to eliminate the following piece of code you created:
 
evtW.event = evt;


Otherwise, I kept getting an error.

When I make the changes to the Apex Class (updated entire code below), I keep getting this error:

"Unknown property 'UpcomingMeetings.EventWrapper.StartDateTime' "

If I change all the StartDateTime fields to StartDateVF1/StartDateVF2 fields, the error changes to:

"Unknown property 'UpcomingMeetings.EventWrapper.Id'"

<apex:page standardController="Contact" extensions="UpcomingMeetings" sidebar="false" showHeader="false">
    <style type="text/css">
    .meetingToday {
//        font-weight:bold;
        font-style:italic
    }
    </style>

    <apex:pageMessage severity="INFO" strength="1" summary="{!summary}" escape="false">
        <apex:dataTable value="{!evtWrapper}" var="e" width="100%" cellpadding="1" cellspacing="1" rows="3">
            <apex:column >
                <li/>
            </apex:column>
            <apex:column >
                <apex:outputLink value="/{!e.Id}" target="_blank" styleClass="{!IF(DATEVALUE(e.StartDateTime)<TODAY(),'meetingToday','')}"><apex:outputField value="{!e.Subject}"/></apex:outputLink>
            </apex:column>
            <apex:column styleClass="{!IF(DATEVALUE(e.StartDateTime)<TODAY(),'meetingToday','')}">
                <apex:outputText value="from"/>
            </apex:column>
            <apex:column styleClass="{!IF(DATEVALUE(e.StartDateTime)<TODAY(),'meetingToday','')}">
                <apex:outputText value="{!e.StartDateVF1 }"/>
            </apex:column>
            <apex:column styleClass="{!IF(DATEVALUE(e.StartDateTime)<TODAY(),'meetingToday','')}">
                <apex:outputText value="to"/>
            </apex:column>
            <apex:column styleClass="{!IF(DATEVALUE(e.StartDateTime)<TODAY(),'meetingToday','')}">
                <apex:outputText value="{!e.EndDateTime}"/>
            </apex:column>
        </apex:dataTable>
    </apex:pageMessage>
</apex:page>

 
Khalid AbdullahKhalid Abdullah
Sorry the code above is for VisualForce, not an Apex Class.
Temoc MunozTemoc Munoz
Hi Khalid,

you need to access the startDateTime through the wrapper first then the Event:
 
e.evt.StartDateTime