0

I am facing an unexpected token error when I am trying to insert a record through execute anonymous window.

list<location_tracing__c> newRecs = new list<location_tracing__c>();
location_tracing__c loc1 = new location_tracing__c(Name = 'Bangalore');
loc1.PersonId__c = 'Rajeswari';
loc1.Visit_date__c = 2021-04-30;
loc1.location__c = 'Bangalore Cantonment';
newRecs.add(loc1);

when I click on execute button, I am getting the error like below

Line: 4, Column: 20 Unexpected token '='.

i.e on the visit_date__c line.

I am unable to get any clue why this error happens, even though everything seems to be okay to the best of my analysis. Please some one guide me..

1 Answer 1

0

Date values must use the appropriate Date.newInstance method:

list<location_tracing__c> newRecs = new list<location_tracing__c>();
location_tracing__c loc1 = new location_tracing__c(Name = 'Bangalore');
loc1.PersonId__c = 'Rajeswari';
loc1.Visit_date__c = Date.newInstance(2021,4,30);
loc1.location__c = 'Bangalore Cantonment';
newRecs.add(loc1);

In this case, the error is a red herring because the compiler misinterpreted what you were attempting to accomplish.

You can't write just 2021-04-30, as the compiler would interpret that as "subtract 4 from 2021, then subtract 30 from that", which ends up being a number, not a date you can assign.

I'm not entirely sure why it gave you that error instead of something like "cannot assign Integer to Date", though.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .