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
mukul shah 14mukul shah 14 

Button active or inactive based on picklist value

Hi Everyone,

I have a button and picklist field .. I have to keep the button active when the status is  A and B  and keep it inactive when the status is C. How can I do this? Do I need to write a trigger? Pls help

Thanks 
​Mukul
Rohit K SethiRohit K Sethi
Hi Mukul,

<head>
        <script   src="https://code.jquery.com/jquery-2.2.3.min.js"></script>
        <script>
            function btnDisable(label){
                setTimeout(function(){
                    var pick1Text = $('[id$=pick1]').find('option:selected').text();
                    if(pick1Text=="A" || pick1Text=="B"){
                        $('[id$=myButton]').prop("disabled",true);
                        $('[id$=myButton]').addClass('btnDisabled');
                    }
                    else{
                        $('[id$=myButton]').prop("disabled",false);
                        $('[id$=myButton]').removeClass('btnDisabled');
                    }
                },500);
            }
        </script>
    </head>
    <apex:form>
        Select Value ::
        <apex:inputField id="pick1" value="{!contact.status__c}" onchange="btnDisable(this);"/><br/>
        <apex:commandButton value="Click Me" id="myButton"/>
    </apex:form>
You don't need to write any trigger. 

Thanks.
mukul shah 14mukul shah 14
Hi Rohit,

I already have some script on that button to set a particular value on click..

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

var p = new sforce.SObject('Account');
p.id = "{!Account.Id}";
p.Account_Sync_Status__c = "Submitted";
result = sforce.connection.update([p]);
location.reload(true);

Now i need to add script to keep it active when the picklist value is New/Modified and Inactive/greyed out when value is Submitted/Synced.
How would i merge the code with the existing one?

Thanks!!
Rohit K SethiRohit K Sethi
Hi Mukul,

You are calling asynchronous call that is 
                result = sforce.connection.update([p]);
and after that you are reloading the page this will immediately reload the page . So you need to set callback in sforce request and that callback will execute the reload code.One more thing you should Set that is session id.

I am posting code that can help you to better understand.
sforce.connection.sessionId = '{!$Api.Session_ID}';
                var callback = {                
                 onSuccess:function(result){        alert("success" + result);      location.reload(true); }, 
                 onFailure:function(error){         alert("error" + error);    location.reload(true); } 
                };
                var p = new sforce.SObject('Account');
                p.id = "{!Account.id}";
                p.Name = "rohit";
                result = sforce.connection.update([p],callback);

If this post solves your problem kindly mark it as solution.
Thanks.