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
Subhodeep Dey 2Subhodeep Dey 2 

How to get last activity date of a custom button in a object

Hi,

 

I have a task assigned to get the last clicked and how many times it's clicked and clicked by whome for a custom button (Javascript) for any object.

Ajay K DubediAjay K Dubedi
Hi Subhodeep,

You can try the following. From the code you posted, it seems like you have this button from the Opportunity object.
Please note that you will have to replace Click_Counter__c with the custom field you created to store the click counts.

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

   try{
       var clickCntStr = '{!Opportunity.Click_Counter__c}';
       clickCntStr = clickCntStr == null || clickCntStr == "" ? "0" : clickCntStr;

       clickCnt = parseFloat(clickCntStr);
       ++clickCnt;

       var updOpp = new sforce.SObject("Opportunity");
       updOpp.Id = '{!Opportunity.Id}';
       updOpp.Click_Counter__c = clickCnt;
       updOpp.LastActivityDate = Date.today();

       var result = sforce.connection.update([updOpp]);
       if(result[0].getBoolean("success")){
        alert("update successful");
        window.top.location.href = '/' + '{!Opportunity.Id}';
       }
       else{
        alert("update error");
       }
    }
    catch(ccex){
      console.log('click count increment error: ' + ccex);
    }
    
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks,
Ajay Dubedi