We've started encountering evaluation issues with the endsWith() method. We are using a multi-select field called, "Product_Notification__c", which we pull back and split into a string array for evaluation (typical). In this case, I'm looking for a value of " CN" as a suffix for China.
To replicate the issue, I'm evaluating both the Project_Notification__c value from the Contact record, as well as, a String variable holding the same value.
Sample Code:
Contact myContact = [SELECT Id, Product_Notification__c from Contact where Id = '003500000265gH8'];
System.debug('### Contact Product Notications : ' + myContact.Product_Notification__c);
String[] lstValuesFromContactRecord = myContact.Product_Notification__c.split(';');
String notifications = 'RT AUS Maint/Outage - FTI; RT CN01 CN; RT SaaS P01797; TOF NM Maint/Outage; RT EU Maint/Outage - FTI; RT US Maint/Outage - FTI; Maint DEV FTI';
System.debug('### String Product Notications : ' + notifications );
String[] lstValuesFromString = notifications.split(';');
for(String n : lstValuesFromContactRecord)
{
n = n.trim();
System.debug('### Contact value : *' + n.toUpperCase() + '*');
if (n.toUpperCase().endsWith(' CN'))
{
System.debug('### CN Type found');
}
}
for(String n : lstValuesFromString)
{
n = n.trim();
System.debug('### String value : *' + n.toUpperCase() + '*');
if (n.toUpperCase().endsWith(' CN'))
{
System.debug('### CN Type found');
}
}
Debug Log Results:
Notice the string value for "RT CN01 CN" evaluates as "true" but, the Contact multi-select field evaluates "RT CN01 CN" as "false"? Have I missed something trivial? We've had similar code in place for months, and this evaluation recently started failing.
I've also tried the contains() method with similar results. I've also tried explicit casting during assignment i.e. String[] lstValuesFromContactRecord = (String[])myContact.Product_Notification__c.split(';');
Edit 9/9 - I've tried the below assignment with same results. String contactNoficiations = myContact.Product_Notification__c; String[] lstValuesFromContactRecord = (String[])contactNoficiations.split(';');
RT CN01 CN
is using one of the other UTF-8 chars that look like a blank but is not (like a nbsp)? This can happen when copy-pasting from web pages to create picklist entriesRT CN01 CN
from SFDC field metadata UI into a hex editor and see how many characters are really there and that they are the expected UTF-8 characters you are testing against in theif (n.toUpperCase().endsWith(' CN'))
statement. A second thing to try would be create a new picklist valueRT XCN01 CN
on the off (weird) chance that endsWith() was picking up the first ` CN` and failing (I know, unlikely). You could also play with the version of the class if you can't reproduce in anonymous apex