6

What is the best approach to check if a lookup field is null in apex?

rec.lookupField__c == null;         // OR
rec.lookupField__c == '';           // OR 
String.isBlank(rec.lookupField__c ) // ?

I apologize if this is the same as How to check in Apex if a Text field is blank. I posted this anyway because I need confirmation on the approach specifically for a lookup field. The linked question talks only about text field. I am not sure if it is completely safe to consider a lookup field as a text field in this scenario.

5
  • 2
    I always use the first approach.
    – Keith C
    Feb 19, 2016 at 11:09
  • 2
    Yeah if it is a lookup field then use null if it is a text field then use string.isblank.. But I prefer not to use lookupField__c == ''
    – Ratan Paul
    Feb 19, 2016 at 11:13
  • I always prefer to use String.isBlank(rec.lookupField__c). Reason being as isBlank method checks for both blank and null values. Feb 19, 2016 at 11:14
  • Thanks everyone. @tandonprateek It seems ideal, a minor concern is that - isnt that method optimised to be working with strings, will we be facing an issue due to using it on an ID variable.
    – Prajith
    Feb 19, 2016 at 11:16
  • Thanks @Stygon. I think with ID variable also it should work fine as I said it checks for null as well. Lemme know if I have understood ur problem correctly. Feb 19, 2016 at 11:18

3 Answers 3

8

Just compare against null directly (your first example).

Your second approach will always yield false.

Your third approach is about twice as slow as the first.

1
  • But when checking for null you will get unwanted behaviour, when the Id is empty. A way to produce an empty Id is with sObject.put(String fieldname, String value) method. Try the following in exec anonymous: Account s = new Account(); System.debug(s.Id); if(s.Id == null) System.debug('check for null ok.'); s.put('Id',''); if(s.Id == null) System.debug('check for null ok.'); if(String.isBlank(s.Id)) System.debug('check for isBlank ok.'); System.debug(s.Id);
    – Schnaps
    Nov 20, 2018 at 13:47
1

This is also a safer method -

    static public String validateId(String Idparam) {
            String id = String.escapeSingleQuotes(Idparam);
            if((id.length() == 15 || id.length() == 18) && Pattern.matches('^[a-zA-Z0-9]*$', id))
              {
                return id;
              }
            return null;
    }
3
  • This will validate the ID legitimacy. A very nice way to prevent the exception before the system catches it on its own when attempting DML. But how would you proceed when you just want to check if the lookup field in populated or not?
    – Prajith
    Feb 19, 2016 at 11:14
  • 4
    If you use of Apex's Id type in your code the syntax checking is built in with an exception thrown where the syntax is invalid.
    – Keith C
    Feb 19, 2016 at 11:19
  • @Stygon Agree, I would go with lookupField__c == null
    – Jarvis
    Feb 19, 2016 at 11:22
0

As everyone suggest you should go with lookupField__c == null. This will always give you correct result.

Or if you want to check if this is correct id and your value store in string then use

If(IdParam InstanceOf Id)
    return true;

Or a better approach what @Keith c Suggest store them in id field and then SF will handle them and will throw exception if you assign wrong value.

Id idparam;

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.