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
Rajnarayan DeenadayalanRajnarayan Deenadayalan 

Stuck with working on Challenge for Manipulating Records with DML section




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.
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.


Getting following Error message :

Challenge not yet complete... here's what's wrong: 
Executing the 'insertNewAccount' method failed. Either the method does not exist, is not static, or does not insert the proper account.



Following is the Code written for this ,any help is appreciated to complete the challenge
//

Public class AccountHandler{


public static List<Account> insertNewAccount(String firstname){

Account a=new Account();
a.Name=firstname;   
insert a;


List <Account> queriedaccoutn = [SELECT Name 
                          FROM Account 
                          WHERE Name =:firstname 
                          LIMIT 1];
return queriedaccoutn;
}

}
//
Best Answer chosen by Rajnarayan Deenadayalan
Amit Chaudhary 8Amit Chaudhary 8
Please try below code. I hope that will help you.
public class AccountHandler
{
    public static Account insertNewAccount(String sName)
    {
        Account acc= new Account();
        acc.Name = sName;
        try
        {
            insert acc;
            return acc;
        }
        catch(Exception ee)
        {
            return null;
        }
    }
}
Please let us know if this will help you

Thanks
Amit Chaudhary

 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please try below code. I hope that will help you.
public class AccountHandler
{
    public static Account insertNewAccount(String sName)
    {
        Account acc= new Account();
        acc.Name = sName;
        try
        {
            insert acc;
            return acc;
        }
        catch(Exception ee)
        {
            return null;
        }
    }
}
Please let us know if this will help you

Thanks
Amit Chaudhary

 
This was selected as the best answer
Rajnarayan DeenadayalanRajnarayan Deenadayalan
Thanks Amit,this worked,Code is simple and addressed all the points raised in the Challenge
Shivam Mishra 4Shivam Mishra 4
This worked for me
 
public class AccountHandler {
    public static Account insertNewAccount(String accName){
        Account account = new Account();
        
        if(accName != null){
        	account.Name = accName;
             try{
                 upsert account;
                 return account;
        }
        catch(DmlException e){
            return null;
        }
        }
        else
            return null;
     }
}

 
Henry Burns 2Henry Burns 2
I have tried all the solutions so far and it still fails. This is my code

public with sharing class AccountHandler {
    public static Account insertNewAccount (Name strName){
             Account acct = new Account();
        if(strName != null){
             acct.Name = strName;   
             try {
                    upsert acct;
                    return acct;
                } 
        catch (DmlException e) {
        System.debug('A DML exception has occurred: ' +
                    e.getMessage());
        return(null);}
                    
        }
        else
            return null;}
}