Skip to content

Instantly share code, notes, and snippets.

@justindonnaruma
Last active February 26, 2020 14:07
Show Gist options
  • Save justindonnaruma/a0b5c245451fe62af54d6b74b76ef5c1 to your computer and use it in GitHub Desktop.
Save justindonnaruma/a0b5c245451fe62af54d6b74b76ef5c1 to your computer and use it in GitHub Desktop.
Salesforce String match check
List <sObject> records = new List<sObject>();
records = [SELECT ID, Name FROM sObject WHERE ID IN ('List', 'Of', 'Ids')];
String Id0 = records[0].Id;
String Id1 = records[1].Id;
String Name0 = records[0].Name;
String Name1 = records[1].Name;
System.debug('Name Check: '+ Name0 == Name1);
System.debug('Name: '+Name0);
System.debug('Name: '+Name1);
System.debug('Difference Name: '+Name0.difference(Name1));
System.debug('Contains Whitespace Name0: '+Name0.containsWhitespace());
System.debug('Contains Whitespace Name1: '+Name1.containsWhitespace());
System.debug('CompareTo Name: '+Name0.compareTo(Name1));
System.debug('Name0 Id: '+Id0);
System.debug('Name1 Id: '+Id1);
for(Integer i=0; i< Name0.length(); i++) {
System.debug('Name0 = ' + Name0.codePointAt(i));
System.debug('Name1 = ' + Name1.codePointAt(i));
}
@justindonnaruma
Copy link
Author

Background

Had a client tell me that a system that was not supposed to contain duplicates, had a duplicate. Sent me an example, and, unlike other times where the font type made it appear there was a duplicate (.com vs .corn, for example), these two had all of the same characters.

I copied the characters out of the system, into an ASCII checker, and found that they were identical strings.

I attempted to find duplicates using Salesforce's reporting tools, no duplicates found.

I attempted to find both records in SOQL using a 'WHERE" check, only pulled back one of the two records.

I then started writing the above code to identify what was really going on with the strings, first by doing the basic string comparison which failed, moving to difference which said that the entire string was different, and then compareTo, at which point I lived a negative integer in the 6000s. I then decided to check character by character the unicode values. This is what led me to finally find the issue, a Zero Width, Non-breaking space character at the beginning of one of the Names.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment