function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Tarun MukhiaTarun Mukhia 

Stuck at Trailhead Hands On - Need Help

Hi,

I am suck at this. Can someone help?

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.
The 'insertNewAccount' method must also accept an empty string, catch the failed DML and return null.
Check challenge


Thanks
Tarun
NagendraNagendra (Salesforce Developers) 
Hi Tarun,

May I suggest you please give a try with below piece of code:
public class AccountHandler {

    public static Account insertNewAccount (String accName){
        
        
    if(accName!=''){    
        try{
            Account a = new Account(Name=accName);
            insert a;
            System.debug('Bravo Andrè! Account created');
            return a;
        } catch(Exception e){
            System.Debug('Account not created');
            return null;
        }
    } else {
        return null;
    }
     
        
    }    
}
Hope this helps.

Kindly mark this as solved if it's resolved.

Thanks,
Nagendra