0

user.City__c.contains(Sales.Contact__r.City__c)

City is a field on user Object which is a multipicklist values with ; separated values, I want to compare city with city value from contact.

The problem with "contains" is if user has cities like "South San Francisco"; "San Francisco" & if city on the contact South San Francisco, it matches on both. I would like to do an exact match on South San Francisco instead.

2 Answers 2

2

Split the string into parts, then you can find the exact values:

 if(user.City__c.split(';').indexOf('San Francisco') != -1) {
   // Contains an exact match for San Francisco //

Note that you obviously need to check if the field/string is null before doing this, or you'll get a NullPointerException. In Winter '21 Apex, you can now write:

 Integer index = user.City__c?.split(';').indexOf('San Francisco');
 if(index != null && index != -1) {
   // Contains an exact match for San Francisco //

See the Release Notes for more information on this feature.

0

If you need exact match the you have to use = operator

  1. user.City__c.equals(Sales.Contact__r.City__c);
  2. Or if your condition is opposite , Sales.Contact__r.City__c.contains(user.City__c)
1
  • Equals will not work since the user multipicklist will have more than one values stored in it. Sep 21, 2020 at 19:40

You must log in to answer this question.

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