You need to sign in to do that
Don't have an account?
Prince_sfdc
Aepx Static method execution
when I run the block of ‘static’ code in execute anonymous, it throws an error “only top-level methods in the class are allowed to be declared as static”. I tried my code also: there also same issue.
public StringArrayTest { public static void generateStringArray() {} }
Can anyone help on this?
thank you.
public StringArrayTest { public static void generateStringArray() {} }
Can anyone help on this?
thank you.
When you define a class in an anonymous block, the class is considered virtual by default when the anonymous block executes. Save your class in Salesforce to avoid this from happening.
All Answers
Below code I am not able to understand .Could you please explain .
public StringArrayTest { public static void generateStringArray() {} }
You cannot use static methods in your wrapper/inner class. In your case, create another class and copy the wrapper/innser class code to the new class.
I've not created any wrapper/inner class. I just created a static method in a class and excuted in the anonymous and it threw error as given in my question..
Thanks for reply..!
My bad, missed the class keyword in the question...
its like below:
public class StringArrayTest{
public static void generateStringArray()
{
system.debug('array to be printed..');
}
}
created a static method in a class and excuted in the anonymous and it threw error as given in my question..
When you define a class in an anonymous block, the class is considered virtual by default when the anonymous block executes. Save your class in Salesforce to avoid this from happening.
Yes, you're absolutely right. Now I've gotten solution for this problem.
And eventually, my observation is that we can run any snippet in the anonymous window, but if we are keeping class in the snippet then it should not contain a static method. In this particular code given in my question I've included a static method in a class that's not allowed.
Had it been a case where I've used a code w/o static method like below it would have run:
e.g. code I tried w/o static method in class it runs successfully.
Thanks
Thiyagarjan and Manoj
@Thiyagarajan: And to add to your point that we can save the class and make instance and run in the excute anonymous.
But if we don't want to save the class we should not include static method in that.