I have to get subset of users who do not have records in custom table last modified in last 15 minutes. Here is my code:

string query = 'Select Id FROM MyCustomObject__c where LastModifiedDate > ' + Datetime.now().addMinutes(15);

List<MyCustomObject__c> sobjList = Database.query(query);

List<String> userIdsToRemove = new List<String>();
for (MyCustomObject__c dataGrid :sobjList)

List<User> allUsers = [select Id FROM User WHERE Profile.UserLicense.Name IN ('Salesforce','Salesforce Platform') AND 
for (User u :allUsers)
    if (!userIdsToRemove.contains(u.Id))

I get the following error: Error: Compile Error: Method does not exist or incorrect signature: [List].contains(Id) Why is my code complaining about contains()? Is there a way to optimize this code

4 Answers 4


The List class does not have a contains method. Use the Set class instead:

Set<Id> userIdsToRemove = new Set<Id>();
if (!userIdsToRemove.contains(u.Id)) { /* do stuff */ }
  • right on. @user569925 - This is why I always suffix the name of my collection variables as either ....List, ...Set, or ....Map so when I look at / edit the code I know which methods are available to use (given the absence of code complete in my version of the Force.com Eclipse IDE)
    – cropredy
    Oct 21, 2015 at 23:35
  • I tend to find the use of suffixes distasteful and use other conventions for that (e.g. Map is usually something like keyToValue, parentToChildren, etc.). However it is a good strategy if confusion is creeping in about which data type is in use.
    – Adrian Larson
    Oct 22, 2015 at 15:17
  • 3
    Does it not seem utterly ludicrous that such an obvious method is missing?
    – NickJ
    Nov 11, 2016 at 16:25
  • 1
    @NickJ Yes, it does.
    – Adrian Larson
    Nov 11, 2016 at 16:30

Since so many people have viewed this question, I wanted to provide an updated answer to this for anybody who visits this question in 2018 & beyond: The upcoming version of Salesforce (Spring '18) does add the contains() method to List: Spring '18 Release Notes: New and Changed Classes


Returns true if the list contains the specified element.

  • 6
    Caution:- This method only works in debug mode, for e.g if you setup debug log for current user it will work, but it will stop working as soon as you remove debug logs for current user or close developer console. This is strange but true. I am testing in sandbox with API version 42.0 Feb 23, 2018 at 12:42
  • @SFIntegrator are you sure about that? When you say it stops working do you mean it returns false? Or throws an exception?
    – NSjonas
    Feb 24, 2018 at 21:02
  • @NSjonas Yes it returns false. Feb 26, 2018 at 6:50
  • @SFIntegrator , Do you know if this issue is fixed or is it still happening.
    – Nihar
    Jun 29, 2018 at 19:49
  • 1
    To future viewers, see this question for an explanation of why List.contains() can fail depending on what your debug logs are set to. Jul 11, 2018 at 13:46

Contains is the Set Class method not the list class.. as the query return the list . You need to convert list to set and then You can use the contains method..You use other comparison operator with the list..




You can do something like below in your code. This is just for reference.

List<String> city = new List<String>();

Set<String> setCity = new Set<String>(city);
System.debug('Set City : '+setCity);

You must log in to answer this question.

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