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
Shamus Kelley 4Shamus Kelley 4 

Incremental counter each time a custom button is clicked

I'm looking to track/count each time a custom button is clicked. i.e. each time Send Quote is clicked, i'd like to see the count go up incrementally. *This custom button is an onclick javascript. Thank you in advance. 
Best Answer chosen by Shamus Kelley 4
Terence_ChiuTerence_Chiu
***Ignore the previous post, I hit a key that automatically submitted the reply by accident. 

You can try the following. From the code you posted it seems like you have this button from the Opportunity object and not the Quote 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{

   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;

       var result = sforce.connection.update([updOpp]);
    }
    catch(ccex){
      console.log('click count increment error: ' + ccex);
    }

    // check that Opp has the necessary approvals in order to continue
    var strCheck = "{!Opportunity.Approval_Required__c}".toLowerCase();
    if ( strCheck.indexOf("quote") == -1) {
        try{
            var result = sforce.apex.execute(
                'Opportunity_Buttons', // class name
                'createQuote', // method name
                { // arguments
                    oppIDraw:"{!Opportunity.Id}",
                    generateQuoteNum:"true"
                }
            );
            
            if (result[0].success == 'true') {
                //window.location = result[0].msg;
                window.open(result[0].msg, "quoteWin");
            } else alert(result[0].msg);
        }
        catch(ex){
            alert("Error: "+ex);
        }
    } else alert("No Quote can be generated because Approvals are required");
}
catch (ex) {
    alert("Error: " + ex);
}

 

All Answers

Terence_ChiuTerence_Chiu
Shamus, below example is assuming you need a button on the Quote object. Replace the custom field name with the Number custom field you have or will be creating. The button only increments the field value, so you will need to incorporate all or parts of the example code to fit your needs. Let me know if this helps.
 
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}

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

clickCnt = parseFloat(clickCntStr);


++clickCnt;

var updQuote = new sforce.SObject("Quote");
updQuote.Id = '{!Quote.Id}';
updQuote.Click_Counter__c = clickCnt;


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

if(result[0].getBoolean("success")){
alert("update successful");
window.top.location.href = '/' + '{!Quote.Id}';
}
else{
alert("update error");
}

 
Shamus Kelley 4Shamus Kelley 4
Terence, Thank you for the quick reply. This is pretty new to me and i'm unsure of how this will fit in with the current code. Can I simply insert your work into the following code or does it need more configuration? Thank you again!

try{
    // check that Opp has the necessary approvals in order to continue
    var strCheck = "{!Opportunity.Approval_Required__c}".toLowerCase();
    if ( strCheck.indexOf("quote") == -1) {
        try{
            var result = sforce.apex.execute(
                'Opportunity_Buttons', // class name
                'createQuote', // method name
                { // arguments
                    oppIDraw:"{!Opportunity.Id}",
                    generateQuoteNum:"true"
                }
            );
            
            if (result[0].success == 'true') {
                //window.location = result[0].msg;
                window.open(result[0].msg, "quoteWin");
            } else alert(result[0].msg);
        }
        catch(ex){
            alert("Error: "+ex);
        }
    } else alert("No Quote can be generated because Approvals are required");
}
catch (ex) {
    alert("Error: " + ex);
}
Terence_ChiuTerence_Chiu
You can try the following. From the code you posted it seems like you have this button from the Opportunity object and not the Quote object. Please note that you will have to replace
 
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}

try{

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

clickCnt = parseFloat(clickCntStr);


++clickCnt;

var updQuote = new sforce.SObject("Quote");
updQuote.Id = '{!Quote.Id}';
updQuote.Click_Counter__c = clickCnt;


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

if(result[0].getBoolean("success")){
alert("update successful");
window.top.location.href = '/' + '{!Quote.Id}';
}
else{
alert("update error");
}
   }
   catch(){

   }

    // check that Opp has the necessary approvals in order to continue
    var strCheck = "{!Opportunity.Approval_Required__c}".toLowerCase();
    if ( strCheck.indexOf("quote") == -1) {
        try{
            var result = sforce.apex.execute(
                'Opportunity_Buttons', // class name
                'createQuote', // method name
                { // arguments
                    oppIDraw:"{!Opportunity.Id}",
                    generateQuoteNum:"true"
                }
            );
            
            if (result[0].success == 'true') {
                //window.location = result[0].msg;
                window.open(result[0].msg, "quoteWin");
            } else alert(result[0].msg);
        }
        catch(ex){
            alert("Error: "+ex);
        }
    } else alert("No Quote can be generated because Approvals are required");
}
catch (ex) {
    alert("Error: " + ex);
}

 
Terence_ChiuTerence_Chiu
***Ignore the previous post, I hit a key that automatically submitted the reply by accident. 

You can try the following. From the code you posted it seems like you have this button from the Opportunity object and not the Quote 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{

   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;

       var result = sforce.connection.update([updOpp]);
    }
    catch(ccex){
      console.log('click count increment error: ' + ccex);
    }

    // check that Opp has the necessary approvals in order to continue
    var strCheck = "{!Opportunity.Approval_Required__c}".toLowerCase();
    if ( strCheck.indexOf("quote") == -1) {
        try{
            var result = sforce.apex.execute(
                'Opportunity_Buttons', // class name
                'createQuote', // method name
                { // arguments
                    oppIDraw:"{!Opportunity.Id}",
                    generateQuoteNum:"true"
                }
            );
            
            if (result[0].success == 'true') {
                //window.location = result[0].msg;
                window.open(result[0].msg, "quoteWin");
            } else alert(result[0].msg);
        }
        catch(ex){
            alert("Error: "+ex);
        }
    } else alert("No Quote can be generated because Approvals are required");
}
catch (ex) {
    alert("Error: " + ex);
}

 
This was selected as the best answer
Shamus Kelley 4Shamus Kelley 4
Terence - this is fantastic! thank you for your help on this! - shamus 
Ram Holla 1Ram Holla 1
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
 

   try{

       var clickCntStr = '{!Opportunity__c.Count__c}';
       var oppname = '{!Opportunity__c.Name}';

       clickCntStr = clickCntStr == null || clickCntStr == "" ? "0" : clickCntStr;

       clickCnt = parseFloat(clickCntStr);

       ++clickCnt;

       var updOpp = new sforce.SObject("Opportunity__c");

       updOpp.Id = '{!Opportunity__c.Id}';

       updOpp.Count__c = clickCnt;
       
    


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

       location.reload(true);

    }

    catch(ccex){

      console.log('click count increment error: ' + ccex);

    }

This is fine but also I have a standard name field as sson as button has been clicked 

Name should increment in the below way: 1 , 2 , 3, 4 are the Count__c in the above mentioned script 

AHG/OPP/2018/1
AHG/OPP/2018/2
AHG/OPP/2018/3
AHG/OPP/2018/4
...................

 
Shamus Kelley 4Shamus Kelley 4
Hi All - 

I'm circling back to this request as i'm having trouble implementing the code. I've tried both suggestions thus far but without 100% success. I get the following error message: "Error: TypeError: Cannot read property 'execute' of undefined"

This is the script I'm working with so far:

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

   try{

       var clickCntStr = '{!Opportunity.Quote_Generated_Count__c}';
       var oppname = '{!Opportunity.Name}';

       clickCntStr = clickCntStr == null || clickCntStr == "" ? "0" : clickCntStr;

       clickCnt = parseFloat(clickCntStr);

       ++clickCnt;

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

       updOpp.Id = '{!Opportunity.Id}';

       updOpp.Quote_Generated_Count__c = clickCnt;
       
           var result = sforce.connection.update([updOpp]);

       location.reload(true);

    }

    catch(ccex){

      console.log('click count increment error: ' + ccex);

    }
try{
    // check that Opp has the necessary approvals in order to continue
    var strCheck = "{!Opportunity.Approval_Required__c}".toLowerCase();
    if ( strCheck.indexOf("quote") == -1) {
        try{
            var result = sforce.apex.execute(
                'Opportunity_Buttons', // class name
                'createQuote', // method name
                { // arguments
                    oppIDraw:"{!Opportunity.Id}",
                    generateQuoteNum:"true"
                }
            );
            
            if (result[0].success == 'true') {
                //window.location = result[0].msg;
                window.open(result[0].msg, "quoteWin");
            } else alert(result[0].msg);
        }
        catch(ex){
            alert("Error: "+ex);
        }
    } else alert("No Quote can be generated because Approvals are required");
}
catch (ex) {
    alert("Error: " + ex);
}


Any help on resolving is greatly appreciated!