function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
bob4bob4 

Check if selected picklist value is one among other picklist

Consider 2 picklists

<apex:inputfield value="controller.picklist1__c"/>
and the second picklist, 'picklist2__c' has some values in it, Is there any way to check if selected value in picklist1__c is in picklist2__c. Thanks.

Like, IF(Selectedvalue(picklist1) is one of the field values of (picklist2))
digamber.prasaddigamber.prasad
Are you using vf page/apex controller for this? If yes, then in controller you can do this.

Let me know if you have any specific question.

Regards,
Digamber Prasad
bob4bob4
Yes, Digamber. I'm using VF page and apex controller
ZedroidZedroid
Hi bob,

You can call onchange JavaScript method to invoke action where you can check whether the selected picklist1__c is in picklist2__c.

Regards,
Zedroid.
bob4bob4
Zedroid, I'm new to salesforce I'd really appreciate if you can show with some sample code. Thanks.
GlynAGlynA
Bob,

The code in the following post is a property you can add to your controller.  The set will contain all of the picklist values that are in common between the two picklists.  You can check to see if the chosen value is a shared value with:

set_PicklistValuesInCommon.contains( chosenValue )

In the code, replace "myObject__c" with whatever your object name is.

Let me know if you have any questions.

Glyn Anderson
Sr Developer | System Analyst | ClosedWon | closedwon.com
Certified Developer | Certified Advanced Administrator
GlynAGlynA
<pre>
public Set<String> set_PicklistValuesInCommon
    {
        get
        {
            if ( set_PicklistValuesInCommon == null )
            {
                Set<String> set_Picklist1Values = new Set<String>();
                for ( Schema.PicklistEntry entry : MyObject__c.Picklist1__c.getDescribe().getPicklistValues() )
                {
                    set_Picklist1Values.add( entry.getValue() );
                }

                Set<String> set_Picklist2Values = new Set<String>();
                for ( Schema.PicklistEntry entry : MyObject__c.Picklist1__c.getDescribe().getPicklistValues() )
                {
                    set_Picklist2Values.add( entry.getValue() );
                }

                set_PicklistValuesInCommon = set_Picklist1Values;
                set_PicklistValuesInCommon.retainAll( set_Picklist2Values );
            }
            return set_PicklistValuesInCommon;
        }
        private set;
    }
</pre>
GlynAGlynA
Whoops, I see a typo on line 14:  it should reference "Picklist2__c", not "Picklist1__c" again.

-Glyn