1

I am trying test a piece of code by changing a Status__c field on Save_Attempt__c Object. I am explicitly setting the value of Status picklist to an empty string and not null but when I query, it's coming out as null.

Here's the code:

Save_Attempts__c aSaveAttempt = new Save_Attempts__c();
aSaveAttempt.Status__c = 'In Progress';
insert aSaveAttempt;

Save_Attempts__c parentResult = [SELECT Id, Status__c from 
Save_Attempts__c where Id = :aSaveAttempt.Id];

System.debug('parentResult.Status__c: '+parentResult.Status__c);
// Above line getting printed as "parentResult.Status__c: In Progress"

aSaveAttempt.Status__c = '';
update aSaveAttempt;

parentResult = [SELECT Id, Status__c from Save_Attempts__c where Id = 
:aSaveAttempt.Id];

System.debug('parentResult.Status__c: '+parentResult.Status__c);
// Above line getting printed as "aSaveAttempt.Status__c: null"

The Status__c field has "Restrict picklist to the values defined in the value set" checkbox unchecked.

am I missing something?

2

The Apex primitive type underlying Picklist fields is a String, which has a unique property derived from the underlying data storage:

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.

Conversely, Strings in Apex can be null or empty and can include leading and trailing whitespace, which can be used to construct a message.

(Emphasis is mine). So you cannot in a field whose underlying data storage is a String store a literal empty string value; it's stored as a null.

As a further illustration, if you perform a describe of a picklist field, using Workbench for example, you find on a picklist field these properties:

soapType: xsd:string
type: picklist

Just showing that those types under the hood are the same.

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.