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. Commented 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
    Commented 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
    Commented 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
    Commented 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
    Commented Aug 8, 2016 at 3:53

2 Answers 2

5

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
    Commented 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
    Commented 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.. :-) Commented 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
    Commented 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
    Commented Aug 9, 2016 at 3:23
-1

In my case, all of my classes were static so when I removed the keyword static it worked.

Before public static void show_data(String start_date, String end_date) {...}

After public void show_data(String start_date, String end_date) {...}

2
  • You meant "methods" here instead of "classes" (because your examples are methods, and classes cannot be static). I've downvoted this because you haven't explained why this works (and under which circumstances an error will occur). This is mostly covered by Eric's answer from ~7 years ago. Inner classes cannot have static variables or methods. Additionally, anonymous apex is basically an "inner class" (not entirely accurate, but close enough), which is why anon apex cannot have static variables/methods.
    – Derek F
    Commented Jun 25, 2023 at 0:14
  • Hello @DerekF, thank you for your feedback. I post this answer to help others, if you or anyone want to know the details he can ask through a comment or can search dig down according to his need. From my point of view, I can downvote only if the solution doesn't work or it's not relevant at all. And I found Eric 's situation was different that's why I made this post. However, thank you.
    – sh6210
    Commented Jun 25, 2023 at 21:05

You must log in to answer this question.

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