[SalesForce] Method does not exist or incorrect signature: void contains(String) from the type List

I cannot figure out why the following code (which is within a Test Data Factory class) would cause this error when validating in Production for deploy:

Method does not exist or incorrect signature: void contains(String) from the type List

The class compiles just fine in the Sandbox, and all tests referencing it pass. Here's the code:

private static String setSomeValues(sObject sobj, Map<String,String> myMap) {

    List<String> stringsToExclude = new List<String>{'String1','String2','String3'};

    //The following line is indicated by the compile error
    if(stringsToExclude.contains(sobj.field_X)) {
        sobj.field_Y = myMap.values()[0];
    }
    else {
        sobj.field_Y = map.get(sobj.field_Z);
    }

    return sobj.field_Y;
}

For the sake of testing, I also tried storing the List.contains() result to a Boolean variable in a previous line, then doing if(booleanVar)…but the same error occurred, this time indicating the Boolean variable declaration line.

Edit: Looking at Salesforce's List Class documentation, the contains(listElement) method gives the following explicit example:

List<String> myStrings = new List<String>{'a', 'b'};
Boolean result = myStrings.contains('z');
System.assertEquals(false, result);

So…it would appear their documentation is just incorrect since executing that anonymously produces the same error.

Best Answer

You are deploying to production. list.contains was added in spring 2018 and i dont think its deployed to production instances yet. Its only in preview instances(sandboxes).

Related Topic