How to check in Apex if a Text field is blank

1.7K    Asked by Ankityadav in Web-development , Asked on Aug 1, 2021

 I'm unsure what's the shortest and most robust way to check if string is null, whether a Text field is blank / empty?

/*1*/ Boolean isBlank = record.txt_Field__c == ''; /*2*/ Boolean isBlank = record.txt_Field__c == null; /*3*/ Boolean isBlank = record.txt_Field__c.trim() == ''; /*4*/ Boolean isBlank = record.txt_Field__c.size() == 0;


Answered by Victoria Martin

 I use the isBlank(String) method on the string class in order to check if string is null

, white space or empty.

  String.isBlank(record.txt_Field__c);

The documentation says:

Returns true if the specified String is white space, empty (''), or null; otherwise, returns false.



Your Answer

Interviews

Parent Categories