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
Divya YadavDivya Yadav 

Create a method for inserting accounts.

Hi

I tried this 
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;
}
}

but it is giving error "Line: 2, Column: 23
Only top-level class methods can be declared static"

 
pconpcon
Is this is a Salesforce class called AccountHandler?  Or do you have that defined twice?

The entire file should look like
 
public class AccountHandler {
    public static Account insertNewAccount(String name) {
        Account account = new Account(Name = name);
        try {
            insert account;
        } catch (System.DmlException e) {
            return null;
        }
        return account;
    }
}

I suspect you have this
 
public class AccountHandler {
    public class AccountHandler {
        public static Account insertNewAccount(String name) {
            Account account = new Account(Name = name);
            try {
                insert account;
            } catch (System.DmlException e) {
                return null;
            }
            return account;
        }
    }
}