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
ddoellingddoelling 

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

 

DevAngelDevAngel

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.

Ron HessRon Hess
I could sure use a function like this, ( javascript dates -> ISO 8601 date string) ; if anyone has one already written and available please post src or link to 
ddoellingddoelling

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();
document.write("

Starting Clone Opportunity");

//*** 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();

ddoellingddoelling

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();

Ron HessRon Hess

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.

DevAngelDevAngel

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;

}

RYakerRYaker
I have used this example to try and format a date string to be used with the API via PHP.
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
SuperfellSuperfell
it should be +08:00 not +08.00