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
DeptonDepton 

Date value in test method?

Hi,

 

I have a date field mandatory in a custom object, when writting the test method i get an error:



 

 Compile Error: Invalid initial expression type for field Start_Date__c

Error: Compile Error: Invalid initial expression type for field Start_Date__c, expecting: Date at line 14 column 44

 

 

How do I have to set the date??

 

I have tried:

dd/mm/yyyy

dd/mm/yy

ddmmyy

'Today'

 

Thank you!

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

Just do this

 

cusObj.Start_Date__c = Date.today(); // will set the todays date

 

If you want to give any particular date

 

cusObj.Start_Date__c = Date.newInstance(1980 , 10 ,20); //yyyy , mm , dd

 

 

All Answers

jyovasjyovas

You can do something like

 

Datetime currTime = System.now();

corpAction.Action_Date__c = Date.newInstance(currTime.year(), currTime.month(), currTime.day());

 

Thanks,

-John

Shashikant SharmaShashikant Sharma

Just do this

 

cusObj.Start_Date__c = Date.today(); // will set the todays date

 

If you want to give any particular date

 

cusObj.Start_Date__c = Date.newInstance(1980 , 10 ,20); //yyyy , mm , dd

 

 

This was selected as the best answer
DeptonDepton

Thanks!! to both of U!!!

 

 

I was using:

 

 Date.today() '


instead of just 

 

 Date.today()