sObject Empty Strings and Trailing Whitespace

Today’s PD1 study involved Apex data types. While I’ve been writing code for a while and have a good grasp on general constructs, the completionist in me hates skipping topics (it seriously gives me anxiety).

While reviewing subjects that have been a part of your daily life – for like ever – can be a bit dull, it’s a good way to fill in any knowledge gaps on things that you don’t usually come across.

Todays gap filler was on sObject strings and their contempt for whitespace.

Empty Strings and Trailing Whitespace: sObject String field values follow the same rules as in the SOAP API: they can never be empty (only null), and they can never include leading and trailing whitespace. These conventions are necessary for database storage.

From https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_primitives.htm

For giggles I did some research to see if there was a way to sneak in whitespace, and apparently there is. You can use a unicode non-breakingspace character (\u00A0) like so:

Contact c = new Contact(FirstName = '\u00A0\u00A0Leroy\u00A0\u00A0', LastName='\u00A0\u00A0Jenkins\u00A0\u00A0'); 
insert c;
Contact record with whitespace before and after the first name and last name.

Not exactly sure what use case this would make sense for, but now we know it can be done =).

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.