2

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?

enter image description here

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.

  1. The Apex class must be called AccountHandler and be in the public scope.
  2. The Apex class must have a public static method called insertNewAccount.
  3. The method must accept an incoming string as a parameter, which will be used to create the Account name.
  4. Insert the account into the system and then return the record.
  5. 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;
            }
        }
    }

1 Answer 1

-1

You need to return the inserted account record not database.SaveResult[].

Your apex class should be like:

public class AccountHandler {
    public static Account insertNewAccount(String val){
        Account acc = new Account(Name = val);
        try{
            INSERT acc;
        }
        catch(Exception e){
            return null;
        }
        return acc;
    }
}

You must log in to answer this question.

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