0

I'm trying to compare the picklist (multi-select) values to a List

below is my code:

List<selectOption> options = new List<selectOption>(); 
Schema.sObjectType sobject_type = Account.getSObjectType(); 
Schema.DescribeSObjectResult sobject_describe = sobject_type.getDescribe(); 
Map<String, Schema.SObjectField> field_map = sobject_describe.fields.getMap(); 
List<Schema.PicklistEntry> pick_list_values = field_map.get('Language__c').getDescribe().getPickListValues(); 

List<string> picklistValues = new List<string>();

for (Schema.PicklistEntry a : pick_list_values) { //for all values in the picklist list 
    system.debug(a.getLabel());
    picklistValues.add(a.getValue()); //getValue
}

//picklistValues.sort();

list<string> lang = new list<string>();
lang.add('English');
lang.add('French'); 

for(integer i = 0; i<picklistValues.size(); i++)
{
   if(picklistValues.contains(string.valueof(lang[i])))
   {
      system.debug('//matched:');
   }
}

getting the following error:

Method does not exist or incorrect signature: [List].contains(String)

1
  • 1
    Changing to Set<string> picklistValues = new Set<string>(); would give you acontains method but other changes such as i<lang.size() also needed.
    – Keith C
    Aug 26, 2017 at 0:37

2 Answers 2

3

You cannot do a contains on a List of strings, only a set.

You could do

Set<string> lang = new Set<string>();
lang.add('English');
lang.add('French'); 

lang.retainAll(picklistValues);

system.debug('The matching values are: ' + lang);

The above removes all the values from the lang set that are NOT in the pick list values. You can change things around as you need depending on what you are comparing

To modify your code to just compare like you are trying to do (changes commented with //*******) :

List<selectOption> options = new List<selectOption>(); 
Schema.sObjectType sobject_type = Account.getSObjectType(); 
Schema.DescribeSObjectResult sobject_describe = sobject_type.getDescribe(); 
Map<String, Schema.SObjectField> field_map = sobject_describe.fields.getMap(); 
List<Schema.PicklistEntry> pick_list_values = field_map.get('Language__c').getDescribe().getPickListValues(); 

List<string> picklistValues = new List<string>();

for (Schema.PicklistEntry a : pick_list_values) { //for all values in the picklist list 
    system.debug(a.getLabel());
    picklistValues.add(a.getValue()); //getValue
}

//picklistValues.sort();

Set<string> lang = new Set<string>(); //********Change to set
lang.add('English');
lang.add('French'); 

for(integer i = 0; i<picklistValues.size(); i++)
{
   if(lang.contains(picklistvalues[i]) //*******see if value is in both places
   {
      system.debug('//matched:');
   }
}
4
  • how do i compare values in picklistValues and lang? (set), I just need a way to make sure it exists exact name of the lang in both
    – Nick
    Aug 26, 2017 at 0:46
  • After the above code the lang will contain only the matching values. If you need other ideas you will have to provide more information about what it is you are trying to accomplish. I'll update the answer to do exactly what you are asking.
    – Eric
    Aug 26, 2017 at 0:48
  • I already gave you that in the first code. lang will end up with only the values that are in both. The you can do with it what you need to knowing that only matching values are left in lang. I updated your code to work using the flow you previously had without regard to modifying anything else in the code
    – Eric
    Aug 26, 2017 at 0:50
  • Thank @sfdcfox too as he got me started on actually using retainAll.
    – Eric
    Aug 26, 2017 at 1:10
2

I just wanted to provide an updated answer to this for anybody who visits this question in 2018 & beyond and mention that the upcoming version of Salesforce (Spring '18) does add the contains() method to List: Spring '18 Release Notes: New and Changed Classes

contains(listElement)

Returns true if the list contains the specified element.

0

You must log in to answer this question.

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