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


  • 1
    Itay - Instead of modifying your question to show the solution please post the solution as a separate answer and leave the question intact. Otherwise the question and answer for this will appear broken and out of context.
    – Mike Chale
    Nov 28, 2012 at 12:33

2 Answers 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.

  • i've updated the code (added var f and f = count). I don't get any errors, but the field is not updated according to my clicks...what am I doing wrong??
    – Itay B
    Nov 26, 2012 at 15:52
  • You need to add a return statement to you function, e.g. return count Nov 26, 2012 at 16:01
  • 1
    @ItayB, also, since you are no longer using your variable f, you can remove that too. This is really a javascript question, i.e. not Salesforce related if you have further Javascript questions they might be better posted on a Javascript forum. Nov 26, 2012 at 16:21
  • I've managed to fix the code, published it so users can re-use this.
    – Itay B
    Nov 27, 2012 at 11:32
  • 1
    @ItayB, looks like you've edited your question so that the answer now looks out of context - perhaps it would have been better to answer your own question if you have found a more applicable workaround. Just a suggestion :) Nov 27, 2012 at 11:41

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 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .