Find your content:

Search form

You are here

Count Custom Button Clicks

 
Share

I've added a custom button to the opportunity layout. I would like to count the number of times this button was clicked.

    !REQUIRESCRIPT("/soap/ajax/26.0/connection.js")} //adds the proper code for inclusion of AJAX toolkit

var url = parent.location.href; //string for the URL of the current page
var count=0;
function myFun(f) {
count += 1;
//f.myText.value = count;
f = count;
}
          var OpportunityObj = new sforce.SObject("Opportunity"); 
              OpportunityObj.Id = '{!Opportunity.Id}';
          var f;
              OpportunityObj.Timing__c= myFun(f);
          var result = sforce.connection.update([OpportunityObj]); 
          parent.location.href = url; //refresh the page

Itay


Attribution to: Itay B

Possible Suggestion/Solution #1

my solution is below, thanks for the redirection:)

  {

{!REQUIRESCRIPT("/soap/ajax/26.0/connection.js")} //adds the proper code for inclusion of AJAX toolkit 

var url = parent.location.href; //string for the URL of the current page 

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

OpportunityObj.Id = '{!Opportunity.Id}'; 
OpportunityObj.Timing__c= '{!Opportunity.Timing__c}';        
OpportunityObj.Timing__c= parseInt(OpportunityObj.Timing__c)+1; 

var result = sforce.connection.update([OpportunityObj]); //push the updated records        back to Salesforce 
parent.location.href = url; //refresh the page 

Attribution to: Itay B

Possible Suggestion/Solution #2

Your function is defined as:

function myFun(f) 

i.e. accepting 1 parameter 'f'

However, you call it as:

OpportunityObj.Timing__c= myFun();

So f is effectively null when you reference it here:

f.myText.value = count;

This gives you the error because f is effectively 'undefined' (i.e. there is no variable called 'f' in the scope of your myFun function). You need to pass in f (whatever it is) to your myFun function.


Attribution to: Phil Hawthorn
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/4770

My Block Status

My Block Content