User Story
As a Salesforce developer,
I want to create a program that accepts an Account ID as input and retrieves the corresponding Account record, returning the fields: Id, Name, Industry, and Phone,
So that I can access the details of an Account or return null
if the Account is not found or the ID is invalid.
Acceptance Criteria:
- The program accepts a valid Account ID as input.
- It retrieves the Account record and returns the following fields: Id, Name, Industry, and Phone.
- If no Account is found for the provided ID, the program returns
null
.
- If the Account ID is invalid or null, the program returns
null
.
Codes
public static Account getAccountById(Id accountId) {
if (accountId == null) {
return null;
}
List<Account> accounts = [SELECT Id, Name, Industry, Phone FROM Account WHERE Id = :accountId LIMIT 1];
return !accounts.isEmpty() ? accounts[0] : null;
}