I have created a page that is supposed to display a specific layout assigned to a picklist value. This is based on what has been searched on a lookup field.
Objects:
CustomObject1__c (Id, Name, Multi_Picklist_Field__c(A,B) )
CustomObject2__c (Id, Name, CustomObject1__c, Picklist_Field__c)
Ex: Let's assume there's a record named Record1 in CustomObject1__c
. This record has a multipicklist value of B.
Now, when creating a new CustomObject2__c
record and I selected Record 1 from the CustomObject1__c
lookup field, the only option in the picklist must only be B. Vice versa when searched record contains multipicklist value of A.
On the other hand, when both multipicklist values are selected, both must display as options for picklist values when creating new CustomObject2__c
.
I created both apex class and visualforce page, but it's not working when I search through the lookup field.
Apex Class:
public class TheController {
ApexPages.StandardController con;
public CustomObject2__c c2 {get;set;}
public List<CustomObject1__c> c1List = new List<CustomObject1__c>();
public List<SelectOption> options {get;set;}
public Boolean page1 {get;set;}
public Boolean page2 {get;set;}
public String selected {get;set;}
public TheController(ApexPages.StandardController controller) {
con = controller;
c2 = new CustomObject2__c();
page1 = true;
page2 = false;
}
public void passValueToController() {
String selectedCustomObjId = c2.CustomObject1__c;
if(selectedCustomObjId !=null)
{
selected = [SELECT Multi_Picklist_Field__c FROM CustomObject1__c WHERE Id =: selectedCustomObjId].Multi_Picklist_Field__c;
}
}
public List<SelectOption> getPicklistValues() {
List<SelectOption> options = new List<SelectOption>();
Schema.DescribeFieldResult fieldResult = CustomObject2__c.Picklist_Field__c.getDescribe();
List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
if(c2.CustomObject1__c != null) {
options.add(new SelectOption(ple[0].getLabel(),ple[0].getValue()));
return options;
}
if(c2.CustomObject1__c != null) {
if(selected == 'A') {
options.add(new SelectOption(ple[0].getLabel(),ple[0].getValue()));
return options;
}
if(selected == 'B') {
options.add(new SelectOption(ple[1].getLabel(),ple[1].getValue()));
return options;
}
if(selected == 'A' && selected == 'B') {
for(Schema.PicklistEntry f : ple) {
options.add(new SelectOption(f.getLabel(),f.getValue()));
return options;
}
}
}
return options;
}
public void onChangeVal() {
if(selected == 'A') {
page1 = true;
page2 = false;
}
else {
page1 = false;
page2 = true;
}
}
}
Visualforce Page:
<apex:page standardController="CustomObject2__c" extensions="TheController">
<apex:form >
<apex:outputPanel id="page1" rendered="{!page1}">
<apex:pageBlock title="This is PAGE 1">
<apex:pageBlockSection columns="2">
<apex:inputField value="{!c2.CustomObject1__c}">
<apex:actionSupport event="onchange" action="{!passValueToController}"/>
</apex:inputField>
<apex:selectList value="{!selected}" size="1">
<apex:selectOptions value="{!PicklistValues}"/>
<apex:actionSupport event="onchange" action="{!onChangeVal}"/>
</apex:selectList>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:outputPanel>
<apex:outputPanel id="page2" rendered="{!page2}">
<apex:pageBlock title="This is PAGE 2">
<apex:pageBlockSection columns="2">
<apex:inputField value="{!c2.CustomObject1__c}">
<apex:actionSupport event="onchange" action="{!passValueToController}"/>
</apex:inputField>
<apex:selectList value="{!selected}" size="1">
<apex:selectOptions value="{!PicklistValues}"/>
<apex:actionSupport event="onchange" action="{!onChangeVal}"/>
</apex:selectList>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:outputPanel>
</apex:form>
</apex:page>