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
Raghavendra M 13Raghavendra M 13 

When clicking on button i have to get current time? below is my code getting error

Time field data type is Time
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}

try{
    var accToUpdate = new sforce.SObject("Account");

    accToUpdate.Id = "{!Account.Id}";
    var today = new Date();

   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.getHours() + ":"+date.getMinutes();
       }
    accToUpdate.Current_Time__c= fixDate(today);
    
    var result = 
        sforce.connection.update([accToUpdate]);

    if(result[0].success == "true"){
        location.reload();
    }
    else{
        alert("An Error has Occurred. Error: " + 
            result[0].errors.message
        );
    }
}
catch(e){
    alert(
        "An Error has Occurred. Error: " + 
        e
    );
}
GhanshyamChoudhariGhanshyamChoudhari
The code looks correct only. is Current_Time__c field available?please mention the error which you are getting.
 
Raghavendra M 13Raghavendra M 13
User-added image
Jack Yu@TokyoJack Yu@Tokyo
Hi Raghavendra
You need turn the time string to YYYY-MM-DDTHH:MM:SSZ, for example "2018-04-18T15:46:00Z"
it works fine.
i just modified your code, please use it .

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

try{
    var accToUpdate = new sforce.SObject("Account");

    accToUpdate.Id = "{!Account.Id}";
    var today = new Date();

   function fixTime(time){
       if(time < 10) {time = "0" + time};
         return time;
    }




function formatDate(date) {
    var d = new Date(date),
        month = '' + (d.getMonth() + 1),
        day = '' + d.getDate(),
        year = d.getFullYear();

    if (month.length < 2) month = '0' + month;
    if (day.length < 2) day = '0' + day;

    return [year, month, day].join('-');
}


    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 "T"+date.getHours() + ":"+date.getMinutes() +":00Z";
       }
    accToUpdate.Current_Time__c= formatDate(today)+fixDate(today);
    
    var result = 
        sforce.connection.update([accToUpdate]);

    if(result[0].success == "true"){
        alert(accToUpdate.Current_Time__c);//it works fine.
        location.reload();
    }
    else{
        alert("An Error has Occurred. Error: " + 
            result[0].errors.message
        );
    }
}
catch(e){
    alert(
        "An Error has Occurred. Error: " + 
        e
    );
}
Jack Yu@TokyoJack Yu@Tokyo
Hi  GhanshyamChoudhari

The below error message appears when we click the custom button.
An Error has Occurred. Error: {faultcode:'soapenv:Client', faultstring:''15:31' is not a valid value for the type xsd:dateTime', }

Current_Time__c  was created , it is not a problem.

The key point is the time format

 
Sorna JenefaSorna Jenefa
Hi,

Please try the below one:

Displaying field is Text field - Test__c

Custom Button coding:

{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}
try{
    var accToUpdate = new sforce.SObject("Account");
    accToUpdate.Id = "{!Account.Id}";
    var today = new Date();
   function fixTime(time){
       if(time < 10) {time = "0" + time};
         return time;
    }
    function fixDate(date){
                    var hours = date.getHours();
                    var ampm = hours >= 12 ? 'PM' : 'AM';
                    return fixTime(date.getHours()) + ":"+fixTime(date.getMinutes())+" "+ampm;
       }
    accToUpdate.Test__c= fixDate(today);
    var result = sforce.connection.update([accToUpdate]);
    if(result[0].success == "true"){
        location.reload();
    }
    else{
        alert("An Error has Occurred. Error: " + result[0].errors.message);
    }
}
catch(e){
    alert("An Error has Occurred. Error: " + e);
}

Let me know if its works as you excepted.

Thanks 
Jenefa
Sweet Potato Tec
 
Raghavendra M 13Raghavendra M 13
Not Working for Time field data type is Time(Beta)
Jack Yu@TokyoJack Yu@Tokyo
Hi Raghavendra

can you show me error message? 
Jack Yu@TokyoJack Yu@Tokyo
Hi Raghavendra

It is ok Now , sorry for the bug. i fixed it. 

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

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

try{
    var accToUpdate = new sforce.SObject("Account");

    accToUpdate.Id = "{!Account.Id}";
    var today = new Date();

   function fixTime(time){
       if(time < 10) {time = "0" + time};
         return time;
    }




function formatDate(date) {
    var d = new Date(date),
        month = '' + (d.getMonth() + 1),
        day = '' + d.getDate(),
        year = d.getFullYear();

    if (month.length < 2) month = '0' + month;
    if (day.length < 2) day = '0' + day;

    return [year, month, day].join('-');
}

    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());
          
          var Hours = fixTime(date.getHours());


          return "T"+ Hours + ":"+Minutes +":00Z";
       }
    accToUpdate.Current_Time__c= formatDate(today)+fixDate(today);
    
    var result = 
        sforce.connection.update([accToUpdate]);

    if(result[0].success == "true"){
       alert(accToUpdate.Current_Time__c);
        location.reload();
    }
    else{
        alert("An Error has Occurred. Error: " + 
            result[0].errors.message
        );
    }
}
catch(e){
    alert(
        "An Error has Occurred. Error: " + 
        e
    );
}

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

User-added image



User-added image
Raghavendra M 13Raghavendra M 13
My requirement is only getting the time
Raghavendra M 13Raghavendra M 13
Am using Datatype Time
Jack Yu@TokyoJack Yu@Tokyo
Hi Raghavendra M 13

Now it is perfect, please use it. 
------------------------------------------------------------------------------------------
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}

try{
    var accToUpdate = new sforce.SObject("Account");

    accToUpdate.Id = "{!Account.Id}";
    var today = new Date();

   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());
          
          var Hours = fixTime(date.getHours());
          
          return ""+Hours + ":"+ Minutes + ":"+ Seconds+""; // format: 09:00:00
       }
    accToUpdate.Current_Time__c= fixDate(today);
    
    

    
    
    var result = 
        sforce.connection.update([accToUpdate]);

    if(result[0].success == "true"){
        location.reload();
    }
    else{
        alert("An Error has Occurred. Error: " + 
            result[0].errors.message
        );
    }
}
catch(e){
    alert(
        "An Error has Occurred. Error: " + 
        e
    );
}
------------------------------------------------------------------------------------------

User-added image