Salesforce Fact #6 | Id not needed

Considering the below query returns result, do you think is there any error:

Account a = [SELECT Name FROM Account LIMIT 1];
System.debug(a.Id);

You might think that a.Id will generate an error as Id field is not fetched in the SOQL query. But the thing is, ID field is implicitly returned in case of SOQL query result. So the above query is interpreted as:

Account a = [SELECT Id, Name FROM Account LIMIT 1];

Hence, we need not specify Id field explicitly in the SOQL query unless we are doing some kind of existence check like:

List<Account> accList = [SELECT Id FROM Account WHERE Name=<inputname>];

Comments

Popular posts from this blog

Salesforce Fact #218 | Blank picklist value is actually NULL

Salesforce Fact #947 | Resolving dashboard logged-in user limit error

Salesforce Fact #989 | New delete files permission