+ Start a Discussion
Anshita JainAnshita Jain 

Method does not exist or incorrect signature: void insertNewAccount(String) from the type anon

This is what my code is suppose to be doing: 
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.


public class AccountHandler {

    public static Account insertNewAccount(String accName){
        if(accName!=''){    
            try{
                Account a = new Account(Name=accName);
                insert a;    
                return a;
            } catch(Exception e){
                
                return null;
            }
        } else {
        return null;
        }
    }
}

When I test it inside of the execute anonymous window, I input "insertNewAccount('abcd');". I get an error saying "Method does not exist or incorrect signature: void insertNewAccount(String) from the type anon"
 
Best Answer chosen by Anshita Jain
Abdul KhatriAbdul Khatri
Please try creating a brand new playground and try there.

All Answers

Abdul KhatriAbdul Khatri
You need to use it like 
 
Account acct = AccountHandler.insertNewAccount('abcd');

 
Anshita JainAnshita Jain
That solved it!

This was a problem for a trailhead module. I am pretty sure the above code is fine, but my trailhead says "There was an unexpected error in your org which is preventing this assessment check from completing: System.NullPointerException: Attempt to de-reference a null object"
Amit Chaudhary 8Amit Chaudhary 8
you need to try like below
Account acct = AccountHandler.insertNewAccount('Test Record');

As you insertNewAccount method is static so you need to call like below
CLASSNAME.MethodName('Data');

Can you please let me for which module you are talking about
Anshita JainAnshita Jain
https://trailhead.salesforce.com/modules/apex_database/units/apex_database_dml

This is module
Abdul KhatriAbdul Khatri
Please make all triggers related to the Account Inactive and then try trailhead.
Amit Chaudhary 8Amit Chaudhary 8
Can you please post your account trigger code so that we can help you
Abdul KhatriAbdul Khatri
I did the same trailhead, with your code exactly pasted, without any trigger as it doesn't require trigger. My trailhead passed without any issues. I feel like your some Account Trigger is failing when you run this class, therefore I am recommeding to turn off all Account Triggers (make them Inactive) and try running trailhead. I am sure it will pass.
Abdul KhatriAbdul Khatri
Here it is for your reference

User-added image
Ajay K DubediAjay K Dubedi
Hi Anshita,

The program is showing error while executing as you are declaring your insertNewAccount method
as static. Static methods cannot be called directly by their name they need to be called through class name.
All you need to do is:

AccountHandler.insertNewAccount('accountName');

Use this and your program will surely run.
Hope you find this solution helpful, please mark it as best answer if you get help from it.

Thanks.
Ajay Dubedi
Anshita JainAnshita Jain
I do not have any triggers on my Account! what should i do?
Anshita JainAnshita Jain
User-added image
Abdul KhatriAbdul Khatri
Please try creating a brand new playground and try there.
This was selected as the best answer
Hanusha GanjiHanusha Ganji
Have your issue been solved? @Anshita Jain