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

Date Format Compile Errors

Here's the thing:

Trying to insert record with some apex-ifty code.
Schema says field is date type.
SOQL Dumper spits out 2004-04-05 format
I attempt to insert field_name__c='2004-04-05' in Apex Code
I fail.

Error: Compile Error: Invalid initial expression type for field field_name__c, expecting: Date at line #


Read Cookbook, Getting started, and forum pages.
I think because you are trying to assign a literal to a date field.  Try creating a variable that converts your string literal to a date value.
Try removing the quotes
remove the quotes.

Simply removing the quotes does not work.  It then throws this error:


 Compile Error: unexpected token: '2004-04-05' at line 0 column -1


This is what I have in the assignment line:


Current_Status_Date__c = 2004-04-05




It worked for me to put in a variable instead of a literal.


Current_Status_Date__c =



Outside of an SOQL statement, you use Date.newInstance.  For example:



myObj.Field_Name__c = Date.newInstance(1991,5,23);

Inside SOQL, do not use quotes:




for( account a:[select id,name from account where some_field__c = 1991-05-23])