You need to sign in to do that
Don't have an account?

How to handle dates in javascript/xml
I am attempting to write an sforce control / WIL that will create a copy of an existing opportunity. We are looking at using record types to "lock down" our closed opportunities. This control will be used when sales needs to handle a contract renewal. The control would insert a new opportunity using some of the fields on the locked opportunity and reset the record type so the new opp can be edited. .
The problem I am having is in setting the Close Date on the opportunity. Javascript's Date object doesn't seem to work nor does passing the date as a string. I saw one message on this board regarding the use of the primitive datatypes but haven't been able to figure out how to incorporate that into the xml. I am using the Enterprise WSDL.
Anyone have any ideas?
(The script I am using came from a message on this board regarding how to perform a calculation and udpate the opportunity record from a WIL.)
Hi ddoelling,
Typically, and I'm not sure if this is the case with you, when using javascript you create a soap message as a string then send the message to the appropriate endpoint.
When using a date in this scenario, you want to create the part of the message that includes the date like this:
<CloseDate>2005-02-03T10:23:21.000-08.00</CloseDate>
This is ISO 8601 date/time format with a time zone offset. To build this part of the message in javascript you would do:
var closeDate = "<CloseDate>2005-02-03T10:23:21.000-08.00</CloseDate>";
As you can see, it would be best to have a function that would return the ISO formatted date.
Actually the answer was very simple. I just need to make sure that the month and day were 2 positions. Here is the code from my scontrol.
var close= new Date();
Starting Clone Opportunity");document.write("
//*** default close date to 30 days from now.
//*** API requires ISO format for date yyyy/mm/dd so add zero to
//** pad month and day to 2 positions
//** Caution: javascript //*** uses arrays to store date info (Jan = 0).
//*** so you must add 2 to get next month
var closemonth = close.getMonth() + 2;
if (closemonth.length = 1 ) {
closemonth = "0" + closemonth;
}
var closeday = close.getDate();
if (closeday.length = 1) {
closeday = "0" + closeday;
}
var closedt=close.getFullYear() + "-" + closemonth.toString() + "-" + closeday.toString();
Let's try that one again without the formatting codes
var close= new Date();
//*** default close date to 30 days from now.
//*** API requires ISO format for date yyyy/mm/dd so add zero to any
//** pad month and day to 2 positions
//** Caution: javascript //*** uses arrays to store date info (Jan = 0).
//*** so you must add 2 to get next month
var closemonth = close.getMonth() + 2;
if (closemonth.length = 1 ) {
closemonth = "0" + closemonth;
}
var closeday = close.getDate();
if (closeday.length = 1) {
closeday = "0" + closeday;
}
var closedt=close.getFullYear() + "-" + closemonth.toString() + "-" + closeday.toString();
Many Thanks !
If i end up writing the converse, i'll try to post that also, ie: ISO date --> javascript date, this should allow date math in javascript.
Heres a function that will handle time as well.
function toIsoDateTime(theDate) {
var today = theDate;
var year = today.getYear();
if (year < 2000) { // Y2K Fix, Isaac Powell
year = year + 1900; // http://onyx.idbsu.edu/~ipowell
}
var month = today.getMonth() + 1;
var day = today.getDate();
var hour = today.getHours();
var hourUTC = today.getUTCHours();
var diff = hour - hourUTC;
var hourdifference = Math.abs(diff);
var minute = today.getMinutes();
var minuteUTC = today.getUTCMinutes();
var minutedifference;
var second = today.getSeconds();
var timezone;
if (minute != minuteUTC && minuteUTC < 30 && diff < 0) { hourdifference--; }
if (minute != minuteUTC && minuteUTC > 30 && diff > 0) { hourdifference--; }
if (minute != minuteUTC) {
minutedifference = ":30";
} else {
minutedifference = ":00";
}
if (hourdifference < 10) {
timezone = "0" + hourdifference + minutedifference;
} else {
timezone = "" + hourdifference + minutedifference;
}
if (diff < 0) {
timezone = "-" + timezone;
} else {
timezone = "+" + timezone;
}
if (month <= 9) month = "0" + month;
if (day <= 9) day = "0" + day;
if (hour <= 9) hour = "0" + hour;
if (minute <= 9) minute = "0" + minute;
if (second <= 9) second = "0" + second;
return year + "-" + month + "-" + day + "T" + hour + ":" + minute + ":" + second + timezone;
}
Last week as of 8/1 this worked, now it fails with
Error: Extract Date: value not of required type: 2005-08-09T15:29:15.000+08.00
Any idea whats wrong with my date/time format