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
Matthew HoldgateMatthew Holdgate 

How to update custom Date field using onclick Javascript Custom Button

Hi I have the code that worked when the day was bigger than the 10th of a month but when I do it when the date is say the 5th etc it fails because the code doesn't return DD and just returns D (i think this is the problem) I'm guessing the same will happen in January with the month too

{!REQUIRESCRIPT("/soap/ajax/37.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/37.0/apex.js")}
 
var acc = new sforce.SObject("DOUVersion__c"); 

NULL = " ";

acc.Id = "{!DOUVersion__c.Id}";
acc.RetiredHidden__c = NULL;                  
acc.Retired__c = "true";               
acc.Date_of_Retirement__c = "{!YEAR(TODAY())}-{!MONTH(TODAY())}-{!DAY(TODAY())}"; 

         
result = sforce.connection.update([acc]); 
window.location.reload();

your help would be appreciated
Best Answer chosen by Matthew Holdgate
Navin SoniNavin Soni
Hi Matthew Holdgate,

{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/20.0/apex.js")}
var acc = new sforce.SObject("DOUVersion__c");
acc.id = "{!DOUVersion__c.Id}";
var DateofRetirement = "{!DOUVersion__c.Date_of_Retirement__c }";
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10) {
    dd='0'+dd
}
if(mm<10) {
    mm='0'+mm
}
today = dd+'/'+mm+'/'+yyyy;
var myDate = new Date();
myDate.setDate(today);
acc.Date_of_Retirement__c = myDate;
sforce.connection.update([acc]);
window.location.reload();

All Answers

Matthew HoldgateMatthew Holdgate
I get an message saying variable myDate is not defined? any ideas
Navin SoniNavin Soni
Hi Matthew Holdgate,

{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/20.0/apex.js")}
var acc = new sforce.SObject("DOUVersion__c");
acc.id = "{!DOUVersion__c.Id}";
var DateofRetirement = "{!DOUVersion__c.Date_of_Retirement__c }";
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10) {
    dd='0'+dd
}
if(mm<10) {
    mm='0'+mm
}
today = dd+'/'+mm+'/'+yyyy;
var myDate = new Date();
myDate.setDate(today);
acc.Date_of_Retirement__c = myDate;
sforce.connection.update([acc]);
window.location.reload();
This was selected as the best answer