0

I have a scenario where I'm using the custom dependent picklist and I don't want to bind all data of VF page to the controller so I'm using immediate on action function. My requirement is that I have to reset the field options and selected field every time when the object is changed. So When I select any value in the field and change the object it resets as I want (Like "Website" is selected in field and I change object "Account" to "Contact" it will reset to "--None--") but when there is any value selected which is common ("Name") in both field options it will not change in select-List but the changed value is reflected on VF page ( in selected field). If I remove the immediate it will work fine but with immediate I'm facing this issue. Here is my sample code: Controller:

    public with sharing class PicklistController {
        public List<SelectOption> objects { get; set; }
        public List<SelectOption> fields { get; set; }
        public String selectedObject { get; set; }
        public String selectedField { get; set; }

        public PicklistController() {
            objects = new List<SelectOption>();
            fields = new List<SelectOption>();
            objects.add(new SelectOption('Account', 'Account'));
            objects.add(new SelectOption('Contact', 'Contact'));
            selectedObject = 'Account';
            changeObject();
        }

        public void changeField() {
            system.debug('*****' + selectedField );
        }

        public void changeObject() {
            fields = new List<SelectOption>();
            fields.add(new SelectOption('', '--None--'));
            selectedField = '';

            if (selectedObject == 'Account') {
                fields.add(new SelectOption('Name', 'Name'));
                fields.add(new SelectOption('Website', 'Website'));
                fields.add(new SelectOption('Type', 'Type'));
            } 
            if (selectedObject == 'Contact') {

                fields.add(new SelectOption('Email', 'Email'));
                fields.add(new SelectOption('Name', 'Name'));
                fields.add(new SelectOption('Phone', 'Phone'));
            }
        }
    }   

VF page:

    <apex:page controller="PicklistController" cache="false">
        <apex:pagemessages id="pgmsgId"></apex:pagemessages>
        <apex:form>
            <apex:actionFunction action="{!changeObject}" immediate="true" name="changeObject"  rerender="pbId, pgmsgId">
                <apex:param value="" assignTo="{!selectedObject}" name="prm"/>
            </apex:actionFunction>
            <apex:actionFunction action="{!changeField}" name="changeField" rerender="pbId, pgmsgId"/>
            <apex:pageBlock id="pbId">
                <apex:pageBlockSection>
                    <apex:pageBlockSectionItem>
                        <apex:outputLabel>Object </apex:outputLabel>
                        <apex:selectList value="{!selectedObject}" size="1" onchange="changeObject(this.value);return false;">
                            <apex:selectOptions value="{!objects}"></apex:selectOptions>
                        </apex:selectList>
                    </apex:pageBlockSectionItem>

                    <apex:pageBlockSectionItem >

                        <apex:outputLabel>Fields</apex:outputLabel>

                        <apex:selectList value="{!selectedField}" size="1"  onchange="changeField();return false;">
                            <apex:selectOptions value="{!fields}"></apex:selectOptions>
                        </apex:selectList>
                    </apex:pageBlockSectionItem>
                    <apex:pageBlockSectionItem >

                        <apex:outputLabel>Selected Field</apex:outputLabel>
                        <apex:outputText>{!selectedField}</apex:outputText>
                    </apex:pageBlockSectionItem>
                </apex:pageBlockSection>
            </apex:pageBlock>
        </apex:form>
    </apex:page>

Can anyone tell me the reason why this is happening. Thanks.

4
  • try binding the <apex:selectList> inside <apex:outputpanel> Commented Sep 7, 2017 at 7:39
  • I have tried this also but that's not working too. Commented Sep 7, 2017 at 7:59
  • then try <apex:actionregion>. It works for me! Commented Sep 7, 2017 at 8:04
  • your saying that if field is already selected , And if try to change the object in that case : if same name present in selected object then shouldn't be reset otherwise its should reset ?
    – NITHESH K
    Commented Sep 7, 2017 at 8:42

1 Answer 1

0

If your saying that if the field is already selected , And if try to change the object in that case, if same name present in selected object then field shouldn't be reset otherwise its should reset .

Then just preserve your selectedField value in different variable called selectedValue and compare selectedValue with your fields has same value present. if its has same value then set selectedField with same old value ie.. selectedValue. otherwise selectedField just make its blank.

Just replace below function with code .

public void changeObject() {
        fields = new List<SelectOption>();
        fields.add(new SelectOption('', '--None--'));

         system.debug('*****' + selectedField );
        if (selectedObject == 'Account') {
            fields.add(new SelectOption('Name', 'Name'));
            fields.add(new SelectOption('Website', 'Website'));
            fields.add(new SelectOption('Type', 'Type'));
        } 
        if (selectedObject == 'Contact') {

            fields.add(new SelectOption('Email', 'Email'));
            fields.add(new SelectOption('Name', 'Name'));
            fields.add(new SelectOption('Phone', 'Phone'));
        }

        String selectedValue=selectedField;
        selectedField = '';
        for(integer i=0;i<fields.Size();i++)
                   if(fields[i].getValue() == selectedValue) { 
                         selectedField=selectedValue;
                         break;
                   }    
 }
2
  • I think you are taking it wrong, I'm not sure. But my case is I want to reset it everytime but when the field value is same in different objects and this same field value is selected and if I change the object that the field will not be changed but it should be changed. Commented Sep 8, 2017 at 6:27
  • @RekhaJangir Exactly. I did in above code .
    – NITHESH K
    Commented Sep 8, 2017 at 6:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .