4

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:

enter image description here

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(';');

  • 2
    Is there any chance that the multi-select picklist value for 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 entries – cropredy Sep 9 '14 at 19:23
  • I don't believe so. RT CN01 CN, is a regular configured Salesforce picklist option of the multi-select field. – rcraven Sep 9 '14 at 20:48
  • Have you tried endsWith on some of the other values? eg FTI – Caspar Harmer Sep 10 '14 at 1:15
  • 2
    Try copy-pasting the picklist value RT 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 the if (n.toUpperCase().endsWith(' CN')) statement. A second thing to try would be create a new picklist value RT 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 – cropredy Sep 11 '14 at 0:16
  • Using the field metadata value and [www.edithex.com][1] hex editor, reported the following, 00000000: 52 54 20 43 4E 30 31 20 43 4E. I'm assuming that's 10 characters, which is expected for "RT CN01 CN". – rcraven Sep 11 '14 at 18:24
1

It ended up being an issue with the picklist value after all. As crop1645 mentioned, it could have been a copy & paste issue from a web page. I wasn't able to verify that from the metadata value - it looked fine and seemed to evaluate to 10 characters. However, re-entering the picklist option has resolved our evaluation issue.

Thanks!

  • glad you found it --- this kind of problem can consume hours! – cropredy Sep 11 '14 at 19:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.