0

I noticed when you force an existing text field value with an empty String, instead of it's becoming an empty string it's becoming a null value.

For example, try below in Execute Anonymous.

Savepoint sp = Database.setSavepoint();
Contact c = new Contact(lastname = 'test', firstname='ttt');
insert c;
c.firstname = '';
update c;
System.debug('Forced empty string value saved as: ' + [select firstname from Contact where Id = :c.Id].firstname);
Database.rollback(sp);

Is this the normal behaviour?

Also noticed it does not clear email if you assign '', and did not get any errors either.

E.g.

Savepoint sp = Database.setSavepoint();
Contact c = new Contact(lastname = 'test', firstname='ttt', Email = 'test@test.com');
insert c;
c.firstname = '';
c.Email = '';
update c;
c = [select firstname, Email from Contact where Id = :c.Id];
System.debug('Forced empty string value saved as: ' + c.firstname +' and email: '+c.email);
Database.rollback(sp);
1

If I run the above code, the debug output I receive is:

08:09:30.96 (4920148972)|USER_DEBUG|[8]|DEBUG|Forced empty string value saved as: null and email: null

I.e. both values get applied but both become null. For you, I wonder if there is some automation that prevents the Email being cleared?

To check this further, if I create a test object, Test__c, using the default settings during creation of the object type and with two custom fields, FirstName__c a string of 255 and default settings plus EMail an email with default settings, and I try this code:

Savepoint sp = Database.setSavepoint();
Test__c c = new Test__c(Name = 'test', FirstName__c='ttt', Email__c = 'test@test.com');
insert c;
c.FirstName__c = '';
c.Email__c = '';
update c;
c = [SELECT FirstName__c, Email__c FROM Test__c WHERE Id = :c.Id];
System.debug('Forced empty string value saved as: ' + c.FirstName__c +' and email: '+c.Email__c);
Database.rollback(sp);

I get the following debug output:

08:18:43.109 (293581789)|USER_DEBUG|[8]|DEBUG|Forced empty string value saved as: null and email: null

Interestingly, updating the change of the FirstName__c field to use a string of whitespace, I find the insert appears to equally collapse to a null value when looking at the debug.

I then shifted the code into a unit test, like:

@IsTest
private class Test_Test {
    @IsTest
    static void testBehavior() {
        Test__c c = new Test__c(Name = 'test', FirstName__c='ttt', Email__c = 'test@test.com');
        insert c;
        c.FirstName__c = ' ';
        c.Email__c = '';
        update c;
        c = [SELECT FirstName__c, Email__c FROM Test__c WHERE Id = :c.Id];
        System.debug('Forced empty string value saved as: ' + c.FirstName__c +' and email: '+c.Email__c);
    }
}

In this context the debug still shows:

08:40:25.7 (150466565)|USER_DEBUG|[11]|DEBUG|Forced empty string value saved as: null and email: null

I then changed the update c; statement to be Database.update(c); which also results in nulls.

I could not find this behaviour explicitly documented anywhere so this is a really interesting observation. It seems that Apex, at least, treats any value that returns true from String.isBlank as null when updating fields via DML.

I ran these tests against Spring '21.

1
  • @Phik W Thanks, Yes. RE Email: My bad there seems to be an automation in the org which is prevents clearing email. – Pasan Eeriyagama Feb 3 at 23:38

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.