You need to sign in to do that
Don't have an account?
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');
The problem was that I was initializing the Date instance with a two-digit year.
All Answers
What logic are you using the populate the Clsoedate?
CloseDate is a Date field, trim off the 00:00:00 you are attempting to insert date/time
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?
The problem was that I was initializing the Date instance with a two-digit year.