+ Start a Discussion
Khalid AbdullahKhalid Abdullah 

Apex Class: How to manipulate a field from a List

Hello all,

my code is below:
 
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];
I want to create a String within the class which appends the appopriate date value based on the boolean "IsAllDayEvent" field.
When I try a "For" statement or an "If" statement, it won't let me perform the following:

String StartDateVF;
IF(events.IsAllDayEvent == true) {
StartDateVF = StartDateUTC__c
} else {
StartDateVF = StartDateTime;
}

The error that I'm getting is because the List "Events" can't grab the field "IsAllDayEvent" for some reason, even though a SOQL query has been run on the events list. I'm not sure why the list created in the first section of code won't let me grab the "IsAllDayEvent" field. Do I need to set this part up differently?
Best Answer chosen by Khalid Abdullah
Naval Sharma4Naval Sharma4
Hi Khalid,

If you want to access those variables in VF page then define them as public. Use this code.
String StartDateVF1 {get; private set;}
String StartDateVF2 {get; private set;}
//Method starts from here
public yourMethod(){
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();
                
                StartDateVF1 = dt.format('MM/DD/YYYY');
                StartDateVF2 = dt.format('MM/DD/YYYY HH:MM');
                if(evt.IsAllDayEvent == true) {
                    StartDateVF1 = evt.StartDateUTC__c;
                } else {
                    StartDateVF2 = evt.StartDateUTC__c;
}

<apex:column styleClass="{!IF(DATEVALUE(e.StartDateTime)<TODAY(),'meetingToday','')}">
                <apex:outputText value="{!IF(e.IsAllDayEvent == true, StartDateVF1, StartDateVF2)}"/>
            </apex:column>

 

All Answers

Naval Sharma4Naval Sharma4
Hi Khalid,

Use the following code.
for(Event evt : Events){
   String StartDateVF;
   IF(events.IsAllDayEvent == true) {
       StartDateVF = StartDateUTC__c
   } else {
       StartDateVF = StartDateTime;
   }
}

Let me know if you still need my help.

Thanks,
Naval
Naval Sharma4Naval Sharma4
Khalid,

You can go through following link if you want to know more about iterating over SOQL results.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_loops_for_SOQL.htm
Abhishek BansalAbhishek Bansal
Hi Khalid,

I have updated your code as per yopur requirement. Please use the below code :
events = new List<Event>();
try {
Set<Id> eventIds = new Set<Id>();
for (EventRelation e : [SELECT EventId FROM EventRelation &nbsp;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){
	String StartDateVF;
	if(evt.IsAllDayEvent == true) {
		StartDateVF = evt.StartDateUTC__c;
	} 
	else {
		StartDateVF = evt.StartDateTime;
	}
}


If you still face any issue with the above code than please let us know the exact error message that you recieve.

Thanks,
Abhishek Bansal

Khalid AbdullahKhalid Abdullah
Thank you both, both of your codes are very similar. I modified your code a little bit. 
 
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;

I thought that I would be able to grab the new variables in my VisualForce page:
 
<apex:column styleClass="{!IF(DATEVALUE(e.StartDateTime)<TODAY(),'meetingToday','')}">
                <apex:outputText value="{!IF(e.IsAllDayEvent == true, evt.StartDateVF1, evt.StartDateVF2)}"/>
            </apex:column>

However, the error I'm getting now says "Unknown property 'ContactStandardController.evt'"

I know this means that I haven't assigned the variables to the contact relation (e), but I'm not sure how to do this. Thanks again for all your help!
Naval Sharma4Naval Sharma4
Hi Khalid,

If you want to access those variables in VF page then define them as public. Use this code.
String StartDateVF1 {get; private set;}
String StartDateVF2 {get; private set;}
//Method starts from here
public yourMethod(){
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();
                
                StartDateVF1 = dt.format('MM/DD/YYYY');
                StartDateVF2 = dt.format('MM/DD/YYYY HH:MM');
                if(evt.IsAllDayEvent == true) {
                    StartDateVF1 = evt.StartDateUTC__c;
                } else {
                    StartDateVF2 = evt.StartDateUTC__c;
}

<apex:column styleClass="{!IF(DATEVALUE(e.StartDateTime)<TODAY(),'meetingToday','')}">
                <apex:outputText value="{!IF(e.IsAllDayEvent == true, StartDateVF1, StartDateVF2)}"/>
            </apex:column>

 
This was selected as the best answer
Khalid AbdullahKhalid Abdullah
Awesome!! Please note I had to create the string at the top as a public string. If you just declare String by itself, it won't work.

So like this: 
 
public with sharing class UpcomingMeetings {
    public List<Event> events { get; private set; }    
    public String StartDateVF1 {get; private set; }
    public String StartDateVF2 {get; private set; }
    public String summary { get; private set; }
// method here

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;
                }
           }

 
Khalid AbdullahKhalid Abdullah
Okay so now it's telling me "Unkown property 'VisualforceArrayList.StartDateVF1'

It did seem like it was working before but it just errored out. What else can I do here to get these variables available to the VisualForce page??