1

I need to write a SOQL query that retrieves all accounts with the character '\u0000', i.e. the null character, in their name. I wrote the following code:

final string nullChar = '\u0000';
final string nullCharSearch = '%' + nullChar + '%';
Database.QueryLocator = Database.getQueryLocator(
    'select name from Account where name like :nullCharSearch'
);

However, this does not return any results. Our Account table is very large so it is difficult for me to confirm whether there are indeed no results, or if my query is not behaving correctly. So, the question is, will this code produce the correct results, or do I need to escape or hardcode the unicode?

2
  • What is your load process like where an Account can get saved with the null character but not ultimately be null itself? I'm struggling to figure out how/why you're allowing "Account\u0000Name" to be stored in the first place. Dec 12, 2016 at 16:30
  • @caleb.breckon The input process tends to get spammed with some frequency, so I'd assume the null characters are either caused by the spammers having bad input generators, or a peculiarity in the way Salesforce parses badly encoded input (the names tend to look something like \u0000\uFFFD\uFFFD). As for why it's allowed, our requirement is such that we have to keep the data even if it's bad data. I suppose our developers could have used URL encoding, but hindsight is 20/20. Dec 12, 2016 at 16:41

1 Answer 1

3

For some reason, it didn't dawn on me to use an Apex test method to test this:

public static testMethod void nullTest()
{
    final String nullchar = '\u0000';
    final String nullCharSearch = '%' + nullChar + '%';
    Account test = new Account(Name = 'gg\u0000gg');
    insert test;
    List<Account> accnList = Database.query('select Name from Account where Name like :nullCharSearch');
    System.assert(!accnList.isEmpty());
    System.debug(accnList);
}

This test passes and displays the inserted record, so it appears my query was correct.

1
  • 1
    +1 for this; the Apex doc maddeningly never mentions the ability to use \uXXXX strings
    – cropredy
    Dec 13, 2016 at 0:17

You must log in to answer this question.

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