1

I’ve written the code for the below use case and it is partially working. Though the checkbox behavior is working as expected, the value returned by the dependent checkbox tag is false even if it is checked (maybe because I’ve assigned false in the constructor). I’ve lost the track when using disabled, selected, and value attributes in the inputcheckbox tag. Any idea what’s causing this? Am I making it complicated? Is there a simple way to workaround? Any help is appreciated.

Use Case:

enter image description here "Send Spec" checkbox is dependent on "Interested" checkbox
When Furniture field is null:
“Interested” is enabled and “Send Spec” is disabled and the Note text appears
When Furniture field is not-null:
“Send Spec” is disabled until “Interested” is checked.
When “Interested” is selected, “Send Spec” has to be automatically enabled and selected, then the user can unselect/select the “Send Spec”

Page:

 <apex:page standardController="Product__c" extensions="VisitorFormExtension" standardStylesheets="true" >
      <h1>Thanks for visiting our Brochure</h1>
      <apex:form >
           <apex:pageBlock title="Guest Form" >
                <apex:pageblockSection columns="1" id="theDependentSection" >
                    <apex:inputText value="{!visitorEmail}" label="Visitor Email" />
                    <apex:inputcheckbox value="{!blnInterested}" label="Interested?" >
                    <apex:actionSupport event="onclick" rerender="theDependentSection" />
                    </apex:inputCheckbox>
                    <apex:pageBlockSection >
                    <apex:inputcheckbox value="{!blnSendSpec}" label="Send Specifications" disabled="{!blnDisablebox}" selected="{!blnSelected}" />
                    </apex:pageBlockSection>
                    <apex:outputText rendered="{!blnNoFurniture}" label="Note:">You haven't chosen any furniture</apex:outputText>
                    <apex:inputTextarea value="{!anyComments}" label="What do you think about our products?" />
                </apex:pageblockSection>
                <apex:pageblockButtons >
                     <apex:commandButton value="Submit" action="{!SubmitForm}" />
                </apex:pageblockButtons>
            </apex:pageBlock>
      </apex:form>
 </apex:page>

Extension:

public class VisitorFormExtension {

    public Id ProductId { get; set; }
    public Product__c product { get; set; }
    public Furniture__c furniture { get; set; }
    public boolean blnNoFurniture { get; set; }
    public boolean blnInterested { get; set; }
    public boolean blnSendSpec { get; set; }
    public string visitorEmail { get; set; }
    public string anyComments { get; set; }

    public boolean blnDisablebox { 
        get {
            if(!blnNoFurniture) {
                if(blnInterested) {
                    return false;
                } else {
                    return true;
                }
            } else {
                return true;
            }
        } set; 
    }

    public boolean blnSelected {
        get {
            if(!blnNoFurniture) {
                if(blnInterested) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } set;
    }

    public VisitorFormExtension(ApexPages.StandardController controller) {
        ProductId = ApexPages.currentPage().getParameters().get('id');
        product = [SELECT Id, Name, Furniture__c FROM Product__c WHERE Id =: ProductId LIMIT 1];
        if(product.Furniture__c != null) {
            furniture = [SELECT Id, Name FROM Furniture__c WHERE Id =: product.Furniture__c LIMIT 1];
            blnNoFurniture = false;
        } else {
            blnNoFurniture = true;
        }
            blnInterested = false;
            blnSendSpec = false; //I think when value is not assigned, then it's taken as null and throws error while submission
    }

    public PageReference SubmitForm() {
        Visitor__c vis = new Visitor__c(Product__c=ProductId);
        vis.VisitorEmail__c = VisitorEmail;
        vis.Interested__c = blnInterested;
        vis.SendSpecifications__c = blnSendSpec;
        vis.AnyComments__c = anyComments;
        insert vis;

        PageReference pageref = new PageReference('/'+ProductId);
        pageref.setRedirect(true);
        return pageref; 
    }
}
0

Dependent checkbox tag is false even if it is checked because you have not assigned the value as blnSendSpec = true when it is showing selected and vice versa.

Correct code will be:

public boolean blnSelected {
    get {
        if(!blnNoFurniture) {
            if(blnInterested) {
                blnSendSpec = true; //add this line explicitly
                return true;
            } else {
                blnSendSpec = false; //add this line explicitly
                return false;
            }
        } else {
            blnSendSpec = false; //add this line explicitly
            return false;
        }
    } set;
}

or,

I would also suggest to get rid of blnSendSpec variable and use only blnSelected instead of blnSendSpec. In that case whenever it is selected it will be shown as selected and checkbox value will be true. Your existing blnSelected getter method is good for that.

In that case, checkbox code will be:

<apex:inputcheckbox value="{!blnSelected}" label="Send Specifications" disabled="{!blnDisablebox}" selected="{!blnSelected}" />

Hope, it makes sense.

  • Thanks @Santanu. I have experimented by replacing blnSendSpec with blnSelected. But, tried adding the value explicitly after seeing your post. In both these attempts, though the dependent checkbox is unchecked, it is returning true. This could be because of the condition in getter method to return true when blnInterested checkbox is true. Do I have to rework on the conditioning part? Just an idea, is there a better way if I use javascript? – Seesh Oct 30 '16 at 20:34
  • You can use option 1 which I have suggested – Santanu Boral Oct 30 '16 at 22:38
  • Yes, I'm working on the option 1 with the help of debug logs. Figured out that there is no operation happening when checkbox2 is clicked. There is a rerender only for checkbox1, I think adding one more condition and rerendering for checkbox2 might work out. – Seesh Oct 30 '16 at 22:45
  • Is that your issue got resolved? – Santanu Boral Nov 1 '16 at 3:50
  • Thanks for your help. Initially, tried to add the line explicitly to the getter method, but it finally worked when a method was created to assign those values and made the ActionSupport call it whenever the checkbox is clicked. – Seesh Nov 2 '16 at 2:39

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.