function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
pcavepcave 

Are blank sObject fields an empty string or NULL

I'm writing some apex code that needs to check to see if a contact has an account assigned via the AccountId field. Do I need to check for an empty or null value? as in?

 

 if (c.AccountId != NULL)

 

or

 

if (c.Account != '')

 

Thanks,

Phillip

jeffdonthemic2jeffdonthemic2

Use NULL.

 

Jeff Douglas
Appirio, Inc.
http://blog.jeffdouglas.com 

Cool_DevloperCool_Devloper

Hi There,

 

Just FYI .... for String fields, you should use '' check and for number/Id fields, you should use NULL check.

 

In some cases, you might face some weird issues with such comparisons, the workaround then is to check for both :D

 

hope this helps!!

Cool_D

Amit Singh 1Amit Singh 1

Hi There,

Use NULL check for Account like 

Contact.Account !=null

If you will use Contact.AccountId!='' you will get an error saying Invalid Id.

hope this helps :)

Thanks,
Amit Singh.
Shiva RajendranShiva Rajendran
Hi pcave,
Use Null if it is sobject like Account or customObject
if it is Contact.AccountId then it is ID not Sobject reference , in that case you can use String.isBlank(String.valueOf(Contact.Account)) but not generally advicable
c.Account is an sObject then if (c.Account != '') will always return false only
see this reference
http://salesforce.stackexchange.com/questions/110986/check-lookup-field-is-null-in-apex

Thanks and Regards,
Shiva RV