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
TTTTTT 

Setting DateTime field via custom button...HELP

We're attempting to utilize a custom button to set the value of a custom date/time field. The custom field (PM_REVIEW_DATE) is defined as DateTime. All the button attempts to do is set the field to Now() and then update the case and reload the window. When we click the button, we get the following error:

A problem with the OnClick JavaScript for this button or link was encountered:

{faultcode:'soapenv:Client',faultstring:"9/12/2007 12:15 PM' is not a valid value for the type xsd:dateTime',}

The javascript defined with the button is as follows:

{!REQUIRESCRIPT("/soap/ajax/8.0/connection.js")}
var c = new sforce.SObject("Case");
c.id = "{!Case.Id}";
c.PM_Review_Date__c='{!NOW()}';
result = sforce.connection.update([c]);
window.location.reload();



As I understand it, NOW returns a datetime. Why am I getting the above error and how do I correct?

Thanks in advance for the help!

Message Edited by TTT on 09-12-200710:02 AM

sean33043sean33043
Datetime fields need to set using ISO-8601 format. Which is like:
'YYYY-MM-ddThh:mm:ss.mil+hh:mm' 
(year-month-day"T"hour:min:sec.millisec"plus or minus timezone hour minutes")
 
There ought to be a prewritten function that takes "NOW()" and formats it to what Salesforce uses.
 
In the past, I have just parsed the date string and re assembled it into 'yyyy' + 'mm"... etc
A bit of a pain.
 
Sean
 
SteveBowerSteveBower
Probably not supported by Salesforce, but the /soap/ajax/9.0/connection.js javascript file that you import into your s-controls does indeed have a function which does this for you.

sforce.internal.dateTimeToString = function(theDate) {  etc.   }

theDate is a Javascript Date object. Since you want today, your code could
be:

var today = new Date();
c.PM_Review_Date__c = sforce.internal.dateTimeToString(today);

There are a variety of other date handling routines there as well. Personally
It would be nice if Salesforce stepped up and owned these as part of the
product offering.

Best, Steve.




JPlayEHRJPlayEHR

This worked beautifully for me when trying to populate the current date/time into a dateTime field using onClick Javacscript.

 

-Thanks Steve! 

Pradeepkumar Dani 5Pradeepkumar Dani 5
var today = new Date();
opp.MY_Date_Time__c = today.toJSON();

This workd for me!!!