4

We have a custom button in Opportunity standard page layout and when clicked it calls a webservice class (Apex class called by OnClick Javascript via AJAX)

It is a synchronous call as such it takes about 5-8 secs after clicking to get a response from the external system.

We have instructed the users to wait for those 5-8 secs instead of double clicking the same button.

But we still find several users tend to click the button multiple times which in turn sends multiple payload for the same opportunity to the external system.

So I am wondering whether there is any way to disable the custom button after clicking once or atleast popup an alert stating that a previous click's action is under process ?

Below is our code

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

    var oId = '{!Opportunity.Id}';
    var SAPId = '{!Opportunity.SAP_Id__c}';
    var uId = sforce.connection.getUserInfo().userId;
    var oStage = '{!Opportunity.StageName}';

    if(oStage != 'Introduction')
    {

    alert('Opportunity can be sent to SAP only in Introduction stage');

    }
    else if(SAPId == '')
    {

    var canSend = confirm("Do you want to send this to SAP ?");
    if(canSend)
    {

    sforce.apex.execute("SAPExternalCall","SAPExternalCallMethod",{oppId:"{!Opportunity.Id}", accId:"{!Opportunity.AccountId}"});


    var newRecords = [];
    var c = new sforce.SObject("Integration_Log__c");
    c.OpportunityId__c = oId;
    c.RequesterId__c = uId;
    c.RequestTime__c = Date();
    newRecords.push(c);
    insert_result = sforce.connection.create(newRecords);

    alert("Opportunity sent to SAP successfully");

    }

    window.location.reload();
    }
    else 
    {
    alert('Opportunity has already been escalated to SAP');
    }
  • Have you looked at the other posts on here to disable js button on click? Can't easily check on mobile but this smells like a possible dupe. – Adrian Larson Mar 1 '16 at 4:09
  • 1
    Most of the posts talk of using "this.btn = disabled" . These lines do not work at all (or atleast from Winter 16 as one of the post has mentioned). – Rainmaker Mar 1 '16 at 4:13
  • Just checking you did your due diligence! – Adrian Larson Mar 1 '16 at 4:23
  • We have something similar in our org and we show a alert message for the first click saying that integration started and it may take upto 5-6 seconds. But even then some crazy users click the button again in that gap so we created a field on the object to track the status so it will be set to pending for the first click and if the user double clicks it will check for that flag status and throw an alert saying be patient :) – javanoob Mar 1 '16 at 5:12
4

Custom button this event point to window property in javascript so we can't use that to disable the button but as an alternative option we can search for that button using standard javascript getElementsByName method as shown below

enter image description here

Get the button name and put the following code at the beginning of your button

var btnService = document.getElementsByName("tm"); /*tm is button name*/
btnService[0].value='Please wait...'; //Change the text
btnService[0].disabled=true;

after completing the process you can again set this to disable=false after following statement

alert("Opportunity sent to SAP successfully");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.