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
dedeepya chowdarydedeepya chowdary 

To pass this challenge, 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.

SandhyaSandhya (Salesforce Developers) 
Hi Dedeepya,

Please see below 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;
    }
}

Please accept my solution as Best Answer if my reply was helpful. It will make it available for other as the proper solution. If you felt I went above and beyond, you can give me kudos.
 
Thanks and Regards
Sandhya


 
Suraj Tripathi 47Suraj Tripathi 47
Hi Dedeepya,
"Try this code,it will complete your challenge."
public class AccountHandler {
    public static Account insertNewAccount(String s)
    {
        Account accountObj=new Account(name=s);
        try
        {
            insert accountObj;
            return accountObj;
        }catch(DMLException e)
        {
            System.debug('Exception due to-->: '+e.getMessage());
            return null;
        }
    }

}
If you find your Solution then mark this as the best answer. 

Thank you!

Regards 
Suraj Tripathi
Shital Patel 17Shital Patel 17
public class AccountHandler {

    
    public static Account insertNewAccount(String Aname)
    {
        Account acct = new Account();
        System.debug(Aname);
        acct.Name=Aname;
        
        try{
            insert acct;
            
        } catch (DmlException e) {
                    System.debug('A DML exception has occurred: ' +
                                   e.getMessage());
            return null;
        }
        return acct;
    }
}