8

I'm looking for the logical syntax to determine if a sobject is empty.

Example:

MyObject obj = new MyObject();
system.debug(obj == null);

The debug returns "False" -- I want the syntax so it returns True when there's no values within the sobject.

1 Answer 1

23

The '==' operator will return true if the left hand and right hand sobjects have the same value in every field. Thus you will need to instantiate an empty version of the object and compare your candidate to that. E.g. assuming candObj is the instance of the sobject you want to check:

MyObject__c emptyObj=new MyObject__c();
System.debug(candObj==emptyObj);
2
  • Excellent! Thanks. I've gotten so use to just using ==null that when I finally have to instantiate an object first to prevent a VF Page error - I was at a loss how to figure out if it was empty. Awesome Sauce Oct 12, 2012 at 15:03
  • Woah, that's neat .equals override I never knew about. Thanks for posting this. Oct 12, 2012 at 17:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .