+ Start a Discussion
Vinnie BVinnie B 

Anyone have trouble with the containsNone() function?

I'm trying to use the containsNone function to rule out certain values.  It seems to not be working as designed.

 

Looking at the documentation, I'm assuming that the function will make sure that a certain substring is not present in the full string being tested.  What I think I'm seeing is that if ANY of the string, including a single letter, is present, the function will consider that string to be 'contained'.

 

For example, in the following snippet I want to rule out strings that have either MEM, XB or FTB in them.

 

 else if(newOpportunity.Amount > 250 &&
          (
            newOpportunity.Donation_Code__c.containsNone('MEM') &&
            newOpportunity.Donation_Code__c.containsNone('XB') &&
            newOpportunity.Donation_Code__c.containsNone('FTB')
          ) && ...
   

I did some testing and found that the following strings all got caught in this. (I expected only the second one to.)

 

  B, X, PPXBASSD, F T B (with spaces), F, MME

 

The strings that didn't get caught are:

 

  CCC, ZZZ, AAA, Y

 

I was going off of a workflow rule that used - does not contain (MEM, XB, FTB).  That worked fine.

 

Anyone else have similar problems with this?

         

Best Answer chosen by Admin (Salesforce Developers) 
wt35wt35
String a = 'abc';
String b = 'def';
System.debug(a.containsNone(b));
String c = 'abc';
String d = 'adef';
System.debug(c.containsNone(d));
String e = 'abc';
String f = 'abcdef';
System.debug(e.containsNone(f));

 

When trying the here-above code from Developer Console I get:

true, false, false

 

I expected the 2nd result to return True.

To my humble opinion too , this method is not working well, the best would be to contact SF Support to see what they say.

All Answers

AmulAmul
Use this indexOf Method for your logic.

String myString1 = 'abcd';
String myString2 = 'bc';
Integer result =
myString1.indexOf(myString2, 0);
wt35wt35
String a = 'abc';
String b = 'def';
System.debug(a.containsNone(b));
String c = 'abc';
String d = 'adef';
System.debug(c.containsNone(d));
String e = 'abc';
String f = 'abcdef';
System.debug(e.containsNone(f));

 

When trying the here-above code from Developer Console I get:

true, false, false

 

I expected the 2nd result to return True.

To my humble opinion too , this method is not working well, the best would be to contact SF Support to see what they say.

This was selected as the best answer
Vinnie BVinnie B

Thanks!  Glad to know that something seems to not be working right.  Being new to Apex it's usually user error when something is not working as I think it should.  :)

 

For my part, the business has told me that StartsWith will be adequate.  I tested that out and it seems to work ok.