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
laxmi narayan 11laxmi narayan 11 

Update date field in salesforce using javascript custom button

hi there,

I need to update the date Data type field in salesforce using javascript code.

{!REQUIRESCRIPT("/soap/ajax/39.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/39.0/apex.js")} 
sforce.connection.sessionId = "{!$Api.Session_ID}"; 
var record = new sforce.SObject("Account"); 
var currentDate = new Date(); 
var date = currentDate.getDate(); 
var month = currentDate.getMonth(); 
var year = currentDate.getFullYear(); 
var monthDateYear = (month+1) + "/" + date + "/" + year; 
record.Membership_Change_Dates__c = new Date(monthDateYear);
sforce.connection.update([record]);


It returns DateTime, not date.

please assist!

Thanks 
LN 
Best Answer chosen by laxmi narayan 11
Khan AnasKhan Anas (Salesforce Developers) 
By default, JavaScript will use the browser's time zone and display a date as a full-text string:
Mon Aug 05 2019 12:47:18 GMT+0530 (India Standard Time)

https://www.w3schools.com/js/js_dates.asp

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#Methods

P.S.: The above code is working fine in my org for updating the date field.

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Laxmi,

Greetings to you!

You just need to add below line of code:
record.id ="{!Account.Id}";

Try below code:
{!REQUIRESCRIPT("/soap/ajax/39.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/39.0/apex.js")} 
var record = new sforce.SObject("Account"); 
record.id ="{!Account.Id}";
var currentDate = new Date(); 
var date = currentDate.getDate(); 
var month = currentDate.getMonth(); 
var year = currentDate.getFullYear(); 
var monthDateYear = (month+1) + "/" + date + "/" + year; 
record. Membership_Change_Dates__c = new Date(monthDateYear);
sforce.connection.update([record]);
location.reload();

Or you can use the below code:
{!REQUIRESCRIPT("/soap/ajax/39.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/39.0/apex.js")} 
var record = new sforce.SObject("Account"); 
record.id ="{!Account.Id}";
record. Membership_Change_Dates__c = new Date();
sforce.connection.update([record]);
location.reload();

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
laxmi narayan 11laxmi narayan 11
Hi 
Khan Anas  

I tried this but it always returns me date time

Thanks
LN
Khan AnasKhan Anas (Salesforce Developers) 
By default, JavaScript will use the browser's time zone and display a date as a full-text string:
Mon Aug 05 2019 12:47:18 GMT+0530 (India Standard Time)

https://www.w3schools.com/js/js_dates.asp

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#Methods

P.S.: The above code is working fine in my org for updating the date field.
This was selected as the best answer