I just started learning in Salesforce, I'm getting basic knowledge via super helpful blog i.e trailhead. But on a challenge for Apex, I got stuck to complete it, I did run my code on developer Console and it is giving me the exact result, but when I check my challenge it's giving me the error. Is there any way to get the correct answer?
Question:
create an Apex class that inserts a new account named after an incoming parameter. If the account is successfully inserted, the method should return the account record. If a DML exception occurs, the method should return null.
- The Apex class must be called AccountHandler and be in the public scope.
- The Apex class must have a public static method called insertNewAccount.
- The method must accept an incoming string as a parameter, which will be used to create the Account name.
- Insert the account into the system and then return the record.
- The method must also accept an empty string, catch the failed DML and then return null.
Code :
public class AccountHandler {
public static database.SaveResult[] insertNewAccount(String val) {
try {
List<Account> acc = new List<Account> {
new Account (Name = val),
new Account()
};
database.SaveResult[] results = Database.insert(acc);
return results;
} catch (DmlException e) {
return null;
}
}
}