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
ArmanMArmanM 

Auto Stamp todays date when a custom button is clicked

Hi,
 
I have custom field 'Request Date' and custom button called 'Send Request' that redirects to send an email template. I need to stamp today's date into the custom field when the button is clicked and the email has been sent. 
 
Is there any way of doing this? How?
 
Thanks,
Arman
Best Answer chosen by ArmanM
Prateek Singh SengarPrateek Singh Sengar
Hi Arman,
The approach will change slightly then, You can make your custom button to call javascript. You can then update the field with todays value in javascript and then redirect to your custom url.

sample code:
{!REQUIRESCRIPT("/soap/ajax/33.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/33.0/apex.js")} 

var accnt = new sforce.SObject("Account");
accnt.Id = '{!Account.Id}';
//set todays date new Date() will return todays date
accnt.Request_date__c = new Date();

var result = sforce.connection.update([accnt]);
if(result[0].getBoolean("success"))
{
   alert('Account updated successfully');
   // redirect to ur url
}
else{
  alert('Error : '+result);
}


 

All Answers

Prateek Singh SengarPrateek Singh Sengar
Hi Arman,
Does you custom button calls a visualforce page or executed apex method via javascript? If so you can update the custom field with 
system.today().format()

ArmanMArmanM
Hey Prateek,
I am actually using custom url on the button to display a email template. I am not sure if that method you mentioned would still work. 

 
Prateek Singh SengarPrateek Singh Sengar
Hi Arman,
The approach will change slightly then, You can make your custom button to call javascript. You can then update the field with todays value in javascript and then redirect to your custom url.

sample code:
{!REQUIRESCRIPT("/soap/ajax/33.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/33.0/apex.js")} 

var accnt = new sforce.SObject("Account");
accnt.Id = '{!Account.Id}';
//set todays date new Date() will return todays date
accnt.Request_date__c = new Date();

var result = sforce.connection.update([accnt]);
if(result[0].getBoolean("success"))
{
   alert('Account updated successfully');
   // redirect to ur url
}
else{
  alert('Error : '+result);
}


 
This was selected as the best answer
ArmanMArmanM
Thanks Preteek, 

However the porcess has changed to stampping Today() when the record is saved. Any idea on how to do this instead?
ArmanMArmanM
Actually got it ! thank you