+ Start a Discussion
Matthew Ritter 17Matthew Ritter 17 

date time field to date value apex

public static List<Lead> leadsThatMissed(List<Lead> leads) {
		List<Lead> missedLeads = new List<Lead>();
		for (Lead ld : leads) {
			if( ld.Next_Call_Date__c == Date.today().addDays(-1)) {
				ld.Call_Type__c = 'Missed';
				ld.Next_Call_Date__c = 
					ld.Next_Call_Date__c.addDays(1);
				missedLeads.add(ld);
			}
		}
		return missedLeads;
	}
In the above snippet on an Apex class, the ld.Next_Call_Date__c field is a date/time field and I need to get the date value from that field so the comparison works. Basically, I want to say If DATEVALUE(Next_Call_Date__C) == YESTERDAY then do everyhting in the code. 

I tried ld.Next_Call_Date__c.date but that did not work. Any thoughts? Thanks.
 
Best Answer chosen by Matthew Ritter 17
EldonEldon
HI,

You can use date() which Returns the Date component of a Datetime in the local time zone of the context user.
ld.Next_Call_Date__c.date();

If that doesnt work try to create a new date instance like below
 
public static List<Lead> leadsThatMissed(List<Lead> leads) {
		List<Lead> missedLeads = new List<Lead>();
		for (Lead ld : leads) {

                     Date ldDate = date.newinstance(ld.Next_Call_Date__c.year(), ld.Next_Call_Date__c.month(), ld.Next_Call_Date__c.day());


			if( ldDate  == Date.today().addDays(-1)) {
				ld.Call_Type__c = 'Missed';
				ld.Next_Call_Date__c = 
					ld.Next_Call_Date__c.addDays(1);
				missedLeads.add(ld);
			}
		}
		return missedLeads;
	}

Regards

 

All Answers

EldonEldon
HI,

You can use date() which Returns the Date component of a Datetime in the local time zone of the context user.
ld.Next_Call_Date__c.date();

If that doesnt work try to create a new date instance like below
 
public static List<Lead> leadsThatMissed(List<Lead> leads) {
		List<Lead> missedLeads = new List<Lead>();
		for (Lead ld : leads) {

                     Date ldDate = date.newinstance(ld.Next_Call_Date__c.year(), ld.Next_Call_Date__c.month(), ld.Next_Call_Date__c.day());


			if( ldDate  == Date.today().addDays(-1)) {
				ld.Call_Type__c = 'Missed';
				ld.Next_Call_Date__c = 
					ld.Next_Call_Date__c.addDays(1);
				missedLeads.add(ld);
			}
		}
		return missedLeads;
	}

Regards

 
This was selected as the best answer
Matthew Ritter 17Matthew Ritter 17
Awesome, thank you. I will give it a go.