+ Start a Discussion
pluviosillapluviosilla 

Invalid date in CloseDate (Opportunity Object) Causes DML Error During Upsert

I can't figure out what I'm doing wrong.

 

I have an instance op of an Opportunity object and the following code:

 

System.debug('@@@@@@@@@@@@ The year is: ' + nYear);
System.debug('@@@@@@@@@@@@ The month is: ' + nMonth);
System.debug('@@@@@@@@@@@@ The day is: ' + nDay);
Date dt = Date.newInstance(nYear, nMonth, nDay);
System.debug('@@@@@@@@@@@@ The complete date is: ' + dt); 
op.CloseDate = dt;

 This appears to work fine and produces the expected spew in the Developer Console:

 

[392]|DEBUG|@@@@@@@@@@@@ The year is: 12
[394]|DEBUG|@@@@@@@@@@@@ The month is: 11
[396]|DEBUG|@@@@@@@@@@@@ The day is: 28
[398]|DEBUG|@@@@@@@@@@@@ The complete date is: 0012-11-28 00:00:00
etc. (same for multiple Opportunity records)

 

But when controller attempts to upsert these Opportunity records a DML error occurs with an error message complaining that value in CloseDate is not a valid date.

 

Close Date: Close Date: invalid date: Mon Nov 28 00:00:00 GMT 12
Close Date: Close Date: invalid date: Sat Nov 05 00:00:00 GMT 12
Close Date: Close Date: invalid date: Tue Nov 01 00:00:00 GMT 12
Close Date: Close Date: invalid date: Sun Nov 06 00:00:00 GMT 12
Close Date: Close Date: invalid date: Wed Nov 09 00:00:00 GMT 12
Close Date: Close Date: invalid date: Thu Nov 10 00:00:00 GMT 12
Close Date: Close Date: invalid date: Fri Nov 11 00:00:00 GMT 12
Close Date: Close Date: invalid date: Sat Nov 12 00:00:00 GMT 12
Close Date: Close Date: invalid date: Sun Nov 13 00:00:00 GMT 12
Close Date: Close Date: invalid date: Mon Nov 14 00:00:00 GMT 12
Close Date: Close Date: invalid date: Mon Nov 21 00:00:00 GMT 12
Close Date: Close Date: invalid date: Wed Nov 23 00:00:00 GMT 12
Close Date: Close Date: invalid date: Sat Nov 26 00:00:00 GMT 12
Close Date: Close Date: invalid date: Sun Nov 27 00:00:00 GMT 12
Close Date: Close Date: invalid date: Wed Nov 30 00:00:00 GMT 12

 

None of the Opportunity records are inserted into the database. All of them fail for this sole reason. I know that the date is the only problem, because the upsert works just fine if I hard code a date with a statement like this:

 

op.CloseDate = Date.parse('12/27/2009');

 

 

Best Answer chosen by Admin (Salesforce Developers) 
pluviosillapluviosilla

The problem was that I was initializing the Date instance with a two-digit year. 

All Answers

cl0s3rcl0s3r

What logic are you using the populate the Clsoedate? 

pluviosillapluviosilla
Sorry, I don't understand the question. I populate a list of Opportunities objects with data and then do an upsert. It works like a charm, except when I try to assign dates to the CloseDate field. Here's the logic: Date dt = Date.newInstance(nYear, nMonth, nDay); op.CloseDate = dt; nYear, nMonth & nDay are all integers, and in fact, after initializing the new instance of the Date object, I print it and it looks fine. Here's the spew: [398]|DEBUG|@@@@@@@@@@@@ The complete date is: 0012-11-28 00:00:00 What's wrong with that date? Why is the DML operation rejecting it? Does this have anything to do with locales?
cl0s3rcl0s3r

CloseDate is a Date field, trim off the 00:00:00 you are attempting to insert date/time

pluviosillapluviosilla

The spew does suggest a DateTime format, but that's not what I am assigning to CloseDate. I am assign a Date variable. 

 

What is wrong with this code?

 

Date dt = Date.newInstance(nYear, nMonth, nDay);
op.CloseDate = dt;

 

 

 

 

 

 

 

pluviosillapluviosilla

The problem was that I was initializing the Date instance with a two-digit year. 

This was selected as the best answer