function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
hometeamhometeam 

Date Variable

I have what should be a simple question but for the life of me I can't figure it out.  (Yes, I'm a newbee to Apex) :)

 

I have a test class that I'm working on and I need to set the date field to a specific date.  Ideally, I'd like to set the date value to July 15 of the current year.  How do I do this?  Below is the code, that doesn't work, and the value I'm trying to set is Exp_Date__c (a date field)

 

 

Account a1 = new Account(Name='Test1', Status__c='Active', 'VCM', RecordTypeId='01280000000G1LH','00580000003DryK', Dues__c=100, Exp_Date__c=2010-07-15);

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

You need to construct a new instance of a date and use that:

 

e.g.

 

 

Account a1 = new Account(Name='Test1', Status__c='Active', 'VCM', RecordTypeId='01280000000G1LH','00580000003DryK', Dues__c=100, 
Exp_Date__c=Date.newInstance(2010, 07, 15));

 

 

 

All Answers

bob_buzzardbob_buzzard

You need to construct a new instance of a date and use that:

 

e.g.

 

 

Account a1 = new Account(Name='Test1', Status__c='Active', 'VCM', RecordTypeId='01280000000G1LH','00580000003DryK', Dues__c=100, 
Exp_Date__c=Date.newInstance(2010, 07, 15));

 

 

 

This was selected as the best answer
hometeamhometeam

Thank you for the quick reply.  It works perfectly now.   I had tried datetime.newInstance but not date.newInstance.  Thanks again for your help.