[SalesForce] Disable Custom Button in Standard Page Layout

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');
    }

Best Answer

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");
Related Topic