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
Durai SaravananDurai Saravanan 

Getting Date from Today()

Hi,

Good Day!

How should i assign today date to 'Actual_start_date__c', When Start Project(Custom button) is clicked. The javascript code for the custom button is given below. 
 
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")} 

// identify the record 
var o = new sforce.SObject("Project__c"); 
o.id = "{!Project__c.Id}"; 

// Update the Record Type 
o.RecordTypeId = '01210000000FCJwAAO'; 
o.Status__c = 'Active';
o.Actual_start_date__c = {!TODAY()};

// save the change 
//sforce.connection.update(o); 
var result = sforce.connection.update([o]); 

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

if( result[0].success === "true" ) { 
window.location.reload(); 
} 
else { 
alert( result[0].errors.message ); 
}


Thanks in advance,
Durairaj
Best Answer chosen by Durai Saravanan
Deepali KulshresthaDeepali Kulshrestha
Hi Durai,
The problem with the code is that you are not using the xsd format that must be specified while using javascript and that's the reason you are able to see this error.
Please do follow the code provided below and now you will surely be able to assign date to your custom field.

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

// identify the record 
var o = new sforce.SObject("Project__c"); 
o.id = "{!Project__c.Id}"; 

// Update the Record Type 
o.RecordTypeId = '01210000000FCJwAAO'; 
o.Status__c = 'Active'; 
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 = yyyy + '-' + mm+ '-' +dd; 



o.Actual_start_date__c =today; 


// save the change 
//sforce.connection.update(o); 
var result = sforce.connection.update([o]); 

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

if( result[0].success === "true" ) { 
window.location.reload(); 

else { 
alert( result[0].errors.message ); 
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
 

All Answers

Manvendra Chaturvedi 26Manvendra Chaturvedi 26
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 = mm + '/' + dd + '/' + yyyy;
document.write(today);
Durai SaravananDurai Saravanan
Thanks for your reply, but i am still getting some error.User-added image
Deepali KulshresthaDeepali Kulshrestha
Hi Durai,
The problem with the code is that you are not using the xsd format that must be specified while using javascript and that's the reason you are able to see this error.
Please do follow the code provided below and now you will surely be able to assign date to your custom field.

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

// identify the record 
var o = new sforce.SObject("Project__c"); 
o.id = "{!Project__c.Id}"; 

// Update the Record Type 
o.RecordTypeId = '01210000000FCJwAAO'; 
o.Status__c = 'Active'; 
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 = yyyy + '-' + mm+ '-' +dd; 



o.Actual_start_date__c =today; 


// save the change 
//sforce.connection.update(o); 
var result = sforce.connection.update([o]); 

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

if( result[0].success === "true" ) { 
window.location.reload(); 

else { 
alert( result[0].errors.message ); 
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
 
This was selected as the best answer
Durai SaravananDurai Saravanan
Hi Deepali, 

Even after trying your code, I am getting the same error as i mentioned in the previous screenshot

Thanks