0

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

ACCOUNT oRecord = new ACCOUNT();
ACCOUNT oRecord1 = new ACCOUNT();

Map<Id,ACCOUNT> mCommPref = new Map<Id,ACCOUNT>();
mCommPref.put('a2r1h000000hCPMAA2', oRecord);
oRecord1 = mCommPref.get('a2r1h000000hCPMAA2'); 

system.debug('oRecord111 -->' + oRecord1);
system.debug('oRecord1 -->' + oRecord1 == null);

but the oRecord1 == null return False, Need to Return as true when there is no value in the sobject. Can you help me on this

Tried by adding Empty Object as below

ACCOUNT empty = new ACCOUNT();
system.debug('oRecord1 -->' + oRecord1 == empty);

the above code getting this error

Comparison arguments must be compatible types: String, Account

2
  • the accounts don't have ids?
    – Nick C
    Commented Sep 10, 2021 at 2:34
  • @NickCook Yes account having Id Commented Sep 10, 2021 at 2:40

2 Answers 2

1

oRecord1 == null returns false because there is an instantiated Account.

Seeing as the Accounts have Ids, you should be able to use:

oRecord1?.Id == null

1

You're confusing "null" with "empty." An empty object is an object with no values populated, while a null object is one that doesn't exist. If you want to check if it's an "empty" account, you can:

system.debug('oRecord1 -->' + (oRecord1 == new Account()));

The == operator checks all the populated values in two sobjects to see if they're equal.

2
  • I think you'll need to enclose the comparison in parenthesis to avoid the "comparison arguments must be compatible types" error (that, or remove the 'oRecrod1 -->' part) to do this in a debug.
    – Derek F
    Commented Sep 10, 2021 at 2:59
  • @DerekF Yeah, I don't write debug statements this way anyways, so I don't think about that too much. Edited.
    – sfdcfox
    Commented Sep 10, 2021 at 3:02

You must log in to answer this question.

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