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
dylan_thazdylan_thaz 

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 #

Why?

Read Cookbook, Getting started, and forum pages.
jyotijyoti
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.
awadhiyaawadhiya
Try removing the quotes
SuperfellSuperfell
remove the quotes.
mhamberg1mhamberg1

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

 

 

mhamberg1mhamberg1

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

 

Current_Status_Date__c = date.today()

 

sfdcfoxsfdcfox

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])