You need to sign in to do that
Don't have an account?
Koti P 6
Getting Execute Anonymous Error Line: 2, Column: 2 Unexpected token 'public'.
Hi Friends, Iam new to Salesforce, when i am trying to execute the below code getting the error code as Line: 2, Column: 2 Unexpected token 'public'.
My Code:
public class MySOSLSOQLExaples
{
Integer i = [Select Count() from Lead where Name='Smith'];
System.debug('The Value of i is' + i);
}
My Code:
public class MySOSLSOQLExaples
{
Integer i = [Select Count() from Lead where Name='Smith'];
System.debug('The Value of i is' + i);
}
The reason you are getting this error and your apex class is not saved as the business logic is defined directly in the class.
As per the object-oriented language principles, the method should be defined in the class and the logic needs to be defined within the method as below. As you have defined the logic directly within the class you are not able to save it and getting the above error.
But when you execute in the anonymous window directly you can take the piece of code which you have written for the business logic and can execute it in the anonymous window.
Hope this will be clear for you.
Please let us know if you have any further issues.
Kindly mark this as solved if the reply was helpful so that it gets removed from the unanswered queue which results in helping others who are facing a similar issue.
Thanks,
Nagendra
All Answers
You Can use public Class in Execute Anonymous Window.
You can use this:
if you found this answer helpful then please mark it as best answer so it can help others.
Thanks
Akshay
public class MySOSLSOQLExaples
{
public static void isTest(){
Integer i = [Select Count() from Lead where Name='Smith'];
System.debug('The Value of i is' + i);
}
}
The reason you are getting this error and your apex class is not saved as the business logic is defined directly in the class.
As per the object-oriented language principles, the method should be defined in the class and the logic needs to be defined within the method as below. As you have defined the logic directly within the class you are not able to save it and getting the above error.
But when you execute in the anonymous window directly you can take the piece of code which you have written for the business logic and can execute it in the anonymous window.
Hope this will be clear for you.
Please let us know if you have any further issues.
Kindly mark this as solved if the reply was helpful so that it gets removed from the unanswered queue which results in helping others who are facing a similar issue.
Thanks,
Nagendra
I tried your modifed code and it's working fine, so we cannot write any business logic directly in the class, always write in a method.
Regards,
Koti
thank you so much for details explanation , i will keep these tips in mind while writing the business logic.
Regards,
Koti