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
PFangPFang 

Timestamp Custom Button

Hi guys!

 

I checked out a certain JAVA fix and noticed that it may just be the solution to this brickwall I ran into. I have the following custom button OnClick Javascript code on a sample object that should imprint both Date and Time values on a DateTime field. I did use the new Date() syntax but it only works for Date fields and not DateTime. I was wondering, how this post would fit into what I've formulated. Thanks in advance!

 

Mine:

 

{!REQUIRESCRIPT("/soap/ajax/22.0/connection.js")} 
var v=new sforce.SObject("Opportunity"); 
v.JavaDateButtonTest__c="{!Opportunity.JavaDateButtonTest__c}"; 
v.id = "{!Opportunity.Id}"; 
if((v.JavaDateButtonTest__c=='')) 

var date = new Date('{!TODAY()}'); 
v.JavaDateButtonTest__c=date; 
result=sforce.connection.update([v]); 
}

 

-----------------------------------------------------------

 

Fix:

 

function fixTime(time){
   if(time < 10) {time = "0" + time};
   return time;
}
function fixDate(date){
  var Month = fixTime(date.getMonth() + 1);
  var Day = fixTime(date.getDate());
  var UTC = date.toUTCString();
  var Time = UTC.substring(UTC.indexOf(':')-2, UTC.indexOf(':')+6);

  var Minutes = fixTime(date.getMinutes());
  var Seconds = fixTime(date.getSeconds());
  return date.getFullYear() + "-" + Month + "-" + Day + "T" + Time; 
}

 -----------------------------

 

I also got this from another site and I'm not sure if this is usable for what I cited above:

 

http://www.ladysign-apps.com/blog/code/salesforce/salesforce-datetime-functions/comment-page-1/#comm...

 

/**
* Set Javascript Date Time to Salesforce Date Time;
* @param dateTimeObj - date time string
* @return salesforce date time object
**/
function setStringToDateTime(dateTimeObj){
    var dateTimeObj;
 
    if (dateTimeObj != null) {
        dateTimeObj = sforce.internal.stringToDateTime(dateTimeObj);
    }
 
    return dateTimeObj;
}
PFangPFang

Sorry bout the spam, got it fixed through this and don't mind the Opportunity Object, I was just experimenting with that one.

 

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

var v=new sforce.SObject("Opportunity"); 

v.JavaDateButtonTest__c="{!Opportunity.JavaDateButtonTest__c}"; 

v.id = "{!Opportunity.Id}"; 

alert(v.JavaDateButtonTest__c); 

if((v.JavaDateButtonTest__c=='')) 

var d =new Date(); 

var n=d.toJSON(); 

v.JavaDateButtonTest__c=n; 

result=sforce.connection.update([v]); 

location.reload(true);

navigateToUrl('https://na1.salesforce.com//secur/logout.jsp'); 

Pradeepkumar Dani 5Pradeepkumar Dani 5
@PFang:
d.toJSON();
this worked great!!! Thank you so much.