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.