+ Start a Discussion
Shamrock SnowmanShamrock Snowman 

pass today's date to a field in a button

I am trying to use a button with onclick javascript to pass today's date into an existing date field, but I get the error message "12/23/2013 is not a valid value for the type xsd:date".  What is the correct way to pass today's date?  My code is below.

{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")} /*call the ajax connection*/

var currentDate= new Date();
var month = currentDate.getMonth() + 1;
var day = currentDate.getDate();
var year = currentDate.getFullYear();
var today = (month + "/" + day + "/" + year);

var requestFetchInstall= new sforce.SObject("P4_Contract__c"); /*Initialize a new
sobject to work with, this would need to be changed whatever custom object */
requestFetchInstall.Id = "{!P4_Contract__c.Id}"; /* set var to the record id we are working on */
requestFetchInstall.Fetch_Install_Requested__c =(today); /*set a custom field, you would have to rename it to yours, to the value desired, or any field you choose */
result = sforce.connection.update([requestFetchInstall]); /* update the record we initalized with the id *

window.location.reload(); /* reload the page */

Why don't you try using apex function for date, 

var today = System.Today();
Hi Snowman,


requestFetchInstall.Fetch_Install_Requested__c =(today);


requestFetchInstall.Fetch_Install_Requested__c =new Date();

Executing new Date() always returns current local time zone date and time.

- Harsha