0

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 'insertNewAccount' method must accept an incoming string as a parameter, name the account after the parameter, insert it into the system and then return the account record. I have tried this

public class AccountHandler {

    public static Account insertNewAccount(String Test) {
            Account acct = new Account(Name='Acme', Phone='(415)555-1212', NumberOfEmployees=100);
            insert acct;
       } 
        return acct;
    }
2

Looking at what the requirements are I would suggest you trying this code:

public class AccountHandler {
    public static Account insertNewAccount(String name) {
        Account a = new Account();
        a.Name = name;
        try {
            insert a;
        } catch (Exception e) {
            return null;
        }
        return a;
    }
}

This method takes name string parameter and makes a new Account. Then it tries to insert it, but if insert fails then it returns null. If insert succeeds, it will return that account object.

2

I agree almost totally with Novarg code, but I think you should also manage the case in which you insert an empty string (as suggested by the exercise).

public class AccountHandler {

   public static Account insertNewAccount (String accName){ 

   if(accName!=''){    
       try{
           Account a = new Account(Name=accName);
           insert a;
           System.debug('Bravo User5633! Account created');
           return a;
       } catch(Exception e){
           System.Debug('Account not created');
           return null;
       }
   } else {
       return null;
   }

}    

}

0

You can rewrite the account creation statement as below

Account acct = new Account(Name=Test+ 'Acme', Phone='(415)555-1212', NumberOfEmployees=100);
  • The 'insertNewAccount' method must also accept an empty string, catch the failed DML and return null. – user5633 Nov 25 '14 at 10:36
  • how to execute this – user5633 Nov 25 '14 at 10:36
  • You mean another argument to the method? – Vignex Nov 25 '14 at 10:41
  • DML errors will occur only if the record getting inserted contains any errors/missing mandatory details. For your case you have hardcoded it as 'Acme'. So this would not throw error, considering you're passing ''(blank string). But you will get error only if you're passing null. In this case you need not pass it to method, You can do validation even before calling the method. – Vignex Nov 25 '14 at 10:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.