1

I'm really hoping someone can help me here. I have tried numerous times with this code but still get the same error. I have even used other code I have found on-line that apparently 'works', but with the same error.

Below is an example of someone else's code (not mine) that they say works, but unfortunately not for me. Why is it that I am still getting the same error even with this?

public class AccountHandler {
    public static Account insertNewAccount(string AcctName)        
    {

        try{
        Account acct=new Account(Name=AcctName);
        insert acct;
        return acct;
         }
        catch (DMLException e)
        {
            System.debug('A DML exception has occurred: ' +
                         e.getMessage());
          return (NULL);
        }

    }
}
7
  • 2
    seems u hv created AccountHandler as a sub class of another class. if u move that into a separate class it shd start working. Aug 8, 2016 at 3:09
  • i'd be surprised if you got that error for that situation, but you are return twice, which you can't do in apex. I should be making this an answer but I don't know if this solution, I just know it would cause a problem by having two returns
    – EricSSH
    Aug 8, 2016 at 3:45
  • 1
    @EricSSH - you can have multiple returns depending on the circumstances. The first return is if everything goes well, the second is if there is an error. In fact, if you do not add the return in the catch block or at the end of the method the compiler will throw the non void method must return xxx error
    – Eric
    Aug 8, 2016 at 3:49
  • I just tried to save it with 2 returns, and it failed can you pseudo something for me to compile?
    – EricSSH
    Aug 8, 2016 at 3:50
  • 2
    Sure: public class myExample{ public static string doIt(Boolean optionOne){ if(optionOne) return 'First'; else return 'Second'; } }
    – Eric
    Aug 8, 2016 at 3:53

1 Answer 1

4

Credit goes to @Vamsi and if he posts I will delete (If I miss it someone please comment on this answer @me)....

An Inner class cannot have a static method. You must refactor the inner class to its own class.

This fails to compile throwing the error the OP is experiencing:

public class myExample{

    public static string doIt(Boolean optionOne){

        if(optionOne)
            return 'First';
        else
            return 'Second';

    }

    public class exampleTwo{

        public static string testIt(){
            return 'A';
        }

    }


}

However if you move the inner class exampleTwo to is own main class it will compile just fine

public class myExample{

    public static string doIt(Boolean optionOne){

        if(optionOne)
            return 'First';
        else
            return 'Second';

    }
}

Own class

    public class exampleTwo{

        public static string testIt(){
            return 'A';
        }

    }
6
  • Hi Vamsi, thanks for the response and I'm sure you are correct. However, I have tried to use your exampleTwo as-is (simply copied and pasted) and still get exactly the same error. Any ideas on why this could be?
    – Darren
    Aug 8, 2016 at 7:22
  • 1
    @Darren - Then you are doing something wrong because a copy and paste of exampleTwo directly from above to a new Apex Class saves just fine.
    – Eric
    Aug 8, 2016 at 13:47
  • Thanks @Eric .. I can post it as an answer but can't detail it like what you have done.. so happy to leave this with you.. :-) Aug 9, 2016 at 1:19
  • Thanks for the confirmation @Eric, I suspected as much because nearly everything I do doesn't seem to work. Not just on this challenge either, it is really frustrating doing this remotely. If it is not the code, I wonder what is the issue. Is it a browser issue, some setting hidden away on the Developer Console... ?
    – Darren
    Aug 9, 2016 at 3:20
  • Hard to say. In SF, goto setup->Develop->Apex Classes, Click New, paste the exampleTwo class code and click save. You should have no issues.
    – Eric
    Aug 9, 2016 at 3:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .