1

I'm trying to display Picklist field as Radio buttons on Visualforce page. On selection of any radio button, how to make other buttons disabled?

For instance there is a field by named "Lead Source" on Contact Object with LOVs as Web, Phone Inquiry, Partner Referral, Purchased List, Other.

Once the user selects "Web", rest of the radio buttons has to be disabled. enter image description here

Page:

<apex:page standardController="Contact" extensions="DisableRadioButton">
<apex:form>
    <apex:pageBlock>
        <apex:pageBlockSection>
            <apex:selectRadio value="{!Contact.LeadSource}">
                    <apex:selectoptions value="{!types}"></apex:selectoptions>
             </apex:selectRadio>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>

Controller:

public class DisableRadioButton{

public DisableRadioButton(ApexPages.StandardController controller) {

}
    public List<SelectOption> getTypes(){
        Schema.sObjectType sobject_type = contact.getSObjectType();

        Schema.DescribeSObjectResult sobject_describe = sobject_type.getDescribe();

        Map<String, Schema.SObjectField> field_map = sobject_describe.fields.getMap();

        List<Schema.PicklistEntry> pick_list_values = field_map.get('LeadSource').getDescribe().getPickListValues();

        List<selectOption> options = new List<selectOption>();

       for (Schema.PicklistEntry a : pick_list_values) {
                  options.add(new selectOption(a.getLabel(), a.getValue()));
            }
        return options;

    }
}

I have referred few solutions by using actionsupport and jQuery, but in all those there were two different fields, based on selection of one field the other field can be disabled. In my case Lead Source is one field and LOVs are radio buttons.

Any approach that can anyone suggest?

  • jus my thought from a user perspective, after selecting the radio button 1, if I want to change to another radio button how will I do it if you disable the other options ? should I reload the page ? – Vamsi Krishna Mar 9 '17 at 12:13
  • Good Point @VamsiKrishna This requirement is for Community users. Actually records will be created by Non-community user(Salesforce user) and these records will be accessed by Community User. If any record "Lead Source" is selected as "Web", then that user shouldn't able to edit this field at all. But look and feel should be as Radio buttons. – Salesforce Developer Mar 9 '17 at 12:25
  • If Community users can't change then you can disable the <apex:selectRadio for community users. – Saroj Bera Mar 9 '17 at 13:03
  • Thanks for reply @SarojBera "Web" can only be selected by an internal user, disabled externally to community Users. Rest of the radio buttons, community users can update. If i disable at <apex:selectRadio> level, all values will be disabled. So it shouldn't be like that. – Salesforce Developer Mar 9 '17 at 13:08
  • Change the field visibility or make it read only for the community user profile. That's the proper way to solve this issue. – crmprogdev Mar 9 '17 at 13:10
2

One solution is to use isDisabled property of SelectOption class. Check the SelectOption Constructors detail page, there is a constructor which can take 3 arguments (value, label, isDisabled). So if you set the value as true, the user can't select this radio option.

You can use this constructor to make this option as Conditional Read-Only.

Some code example

public List<SelectOption> getItems() {
    List<SelectOption> options = new List<SelectOption>();
    Boolean condition = false;
    if(condition){
       options.add(new SelectOption('US','US',true));
    }else{
        options.add(new SelectOption('US','US'));
    }
    options.add(new SelectOption('CANADA','Canada'));
    options.add(new SelectOption('MEXICO','Mexico'));
    return options;
}
0

Based on your comments, trehe Community User's Profile should have the visibility setting on this field set to not visibile or to read-only in order to solve this issue. You don't need to do anything special in your controller. Go to the OWD for external users to make the changes.

EDIT:

To make the field conditionally read only, in your markup, you should be able to use something like:

<apex:selectRadio value="{!Contact.LeadSource}" visible="{!disableRadio}">

In your controller, add the following:

public static Boolean disableRadio { ;private set}

In the getTypes() method, change the following:

       List<selectOption> options = new List<selectOption>();

       for (Schema.PicklistEntry a : pick_list_values) {
              options.add(new selectOption(a.getLabel(), a.getValue()));
          }
       if(Contact.LeadSource = 'Web'){
           disableRadio = true;
       }else{
           disableRadio = false;
       }

       set disableradio;

       return options;

   }
  • Thanks for reply @crmprogdev This field should be Visible and Editable by Community user. But on only one condition it should be disabled/read only i.e for records who's Lead Source is selected as "Web", for the rest of the conditions it should be editable. Like your answer is there any way i can do Conditional Read-Only? – Salesforce Developer Mar 9 '17 at 13:33
  • see my edited answer – crmprogdev Mar 9 '17 at 14:02

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.