0

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>
2
  • What exactly the issue is when you are selecting the lookup field? Apr 2 '17 at 20:24
  • The picklist field does not adjust according to what has been searched in the lookup field. There's not even a single change in the layout at all.
    – curdual
    Apr 2 '17 at 20:34
0

I can't see any actionSupport function in InputField selection.

You have to create an actionSupport function which will pass input selection to the controller.

In the controller method, fetch the Multi_Picklist_Field__c based on this input id. Finally assign the Multi_Picklist_Field__c to selected variable of <apex:selectList value="{!selected}" size="1">

Code will be like this:

<apex:inputField id="CustomObject1" value="{!c2.CustomObject1__c}"> 
     <apex:actionSupport event="onchange" reRender="page1" action="{!passValueToController}"/>
</apex:inputField>

Controller

public void passValueToController()   
{        
     String selectedCustomObjId = c2.CustomObject1__c; //retrieve the selected value
     if(selectedCustomObjId !=null)
     {
        selected = [SELECT Multi_Picklist_Field__c FROM CustomObject1__c WHERE Id =:selectedCustomObjId].Multi_Picklist_Field__c;
            getPicklistValues(); //refresh picklist
     }

}

Finally, reRender the section properly from actionSupport.

2
  • Thanks for the response. I've edited the code based from what you've suggested. But after some modifications, I am not getting the correct multi picklist value of the queried record into the picklist field. Instead of B, the option becomes A. Can you check my edited code from my post?
    – curdual
    Apr 2 '17 at 21:34
  • You may need to call getPicklistVales() method from controller method to refresh picklist value based on selection. Apr 2 '17 at 22:04

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.