+ Start a Discussion
Sandeep YadavSandeep Yadav 

reate apex method for inserting an account record

Why I am getting this error--
"Executing the 'insertNewAccount' method failed. Either the method does not exist, is not static, or does not insert the proper account."
my 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;
    }
}
Best Answer chosen by Sandeep Yadav
Amit Chaudhary 8Amit Chaudhary 8
Please updatr your code like below
public class AccountHandler{
    public static Account insertNewAccount(String AccName){
        try{
			Account acc = new Account(Name=AccName);
			insert acc;
			return acc;
        } catch(Exception e) {
            System.debug('Exception is:'+  e.getMessage());
            return null;
        }
    }
}

I found below issue in your code
1) a.Name = 'name';
it should be like below
a.Name = name;

Let us know if this will help you
 

All Answers

Alisha Mehta 9Alisha Mehta 9
Hi Sandeep,

How are you executing this code? 
Use this - 
AccountHandler.insertNewAccount('Account Test'); 
Also,
a.Name = 'name'; //Name is an input value for this method.
You should use it as - 
a.Name = name; //Inserts proper Account that you specify.

This works for me. Do let know if it still doesn't work.
 
NagendraNagendra (Salesforce Developers) 
Hi Sandeep,

May I suggest you please give a try with below piece of code which is working fine for me.
public class AccountHandler {

    public static Account insertNewAccount(String AccountName){

        Account acct = new Account(Name=AccountName);

        try {
            insert acct;

        } catch (DMLException e){
            return null;
        }

        return acct;
    }
}
Hope this will help you.

Kindly mark this as solved if the solution was helpful.

Thanks,
Nagendra
 
mukesh guptamukesh gupta
Hi Sandeep,

I have tested my code it's working fine Please apply your side and let me know if you have any issue.
 
public class AccountHandler{
   
    public static Account insertNewAccount(String name){
        try{
             Account acct = new Account(Name=accountName);
             insert acct;
            return acct;
           
        } catch(DmlException e) {
            System.debug('A DML exception has occurred:'+  e.getMessage());
            return null;
        }
    }
   
}

Please MARK AS BEST ANSWER!!!!

REgards
Mukesh

Amit Chaudhary 8Amit Chaudhary 8
Please updatr your code like below
public class AccountHandler{
    public static Account insertNewAccount(String AccName){
        try{
			Account acc = new Account(Name=AccName);
			insert acc;
			return acc;
        } catch(Exception e) {
            System.debug('Exception is:'+  e.getMessage());
            return null;
        }
    }
}

I found below issue in your code
1) a.Name = 'name';
it should be like below
a.Name = name;

Let us know if this will help you
 
This was selected as the best answer