Find your content:

Search form

You are here

Checking to see if a value is present in a list - Apex


As a part of my first trigger, which is running on Opportunities, I have a list I created that contains the result of a SOQL query. Here is the code.

List<Custom_Object__c> mylist = [SELECT Id, Custom_Field_1__c FROM Custom_Object__c WHERE Custom_Field_2 = 'Value' AND Custom_Field__c > 0];

I have a custom field on Opportunities and I want to see if it's value matches a value of Custom_Field_1__c from this list. I've spent a fair amount of time looking around but am still stumped. How do I do this?

Attribution to: dannymorty

Possible Suggestion/Solution #1

Instead of using a list you could put the values of Custom_Field_1__c into a set.

then when you do a compare you can use a method called contains.

set<string> myset = new set<string>(); 
For(Custom_Object__c m :mylist){ 

for (Opportunity o: {
    myset.contains(o.Fieldvalue__c); //returns true/false

Attribution to: Salesforce Wizard
This content is remixed from stackoverflow or stackexchange. Please visit

My Block Status

My Block Content