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
Dhairya Shah 15Dhairya Shah 15 

Having this error:- "Only top-level class methods can be declared static"

<code>

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;
   }
}
}    

</code>    

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.
sfdcMonkey.comsfdcMonkey.com
try this Onces
public class AccountHandler{
    public static Account insertNewAccount(String accountName){  
        Account acctToBeInserted = new Account(Name=accountName);
      
        try{
            insert acctToBeInserted;
        }catch(DMLException e){
            System.debug('Inside DMLException catch ,error is ' + e.getMessage());
            acctToBeInserted = NULL;   
            }
       return acctToBeInserted;                 
       }
}

Thanks Let me inform if it work :)
Dilip_VDilip_V
Shah,

Code looks like ok.Make sure that trigger and validation rules are in-active on account.

Give it a try once
Public class AccountHandler
{
    public static Account insertNewAccount(String acc)
    {
        Account acct = new Account(name=Acc);
        //Database.insert(acct, false);
        try{
            insert Acct;
        }
        Catch(Exception e){
            return null;
        }
        return Acct;
    }
}

Let me know if you have any issues.

Mark it as best answer if it works.

Thanks.
Preya VaishnaviPreya Vaishnavi
Hi,

I think you have created AccountHandler as a subclass of another class.Please create it as a seperate class to make it working.

Also use the below steps to create this class

setup->Develop->Apex Classes, Click New 

Please refer to the below link for the same:
http://salesforce.stackexchange.com/questions/135865/only-top-level-class-methods-can-be-declared-static

Hope it helps!!
Dhairya Shah 15Dhairya Shah 15
Hi,
Yes correct it was just an execution problem as i have created static class so have to execute with the class name.

Thanks a lot!!
Rob Easton 1Rob Easton 1
Thanks Preya! I struggled to get the snippet to executve from dev console, but no problem inside the Salesforce UI itself.