function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Koti P 6Koti 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);
}
Best Answer chosen by Koti P 6
NagendraNagendra (Salesforce Developers) 
Hi Koti,

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.
public class MySOSLSOQLExaples {
    public void sosl(){
 	Integer i = [Select Count() from Lead];
    System.debug('The Value of i is' + i);
			} 
}
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

Akshay_DhimanAkshay_Dhiman
Hi Koti,

You Can use public Class in Execute Anonymous Window.

You can use this:
 
Integer i = [Select Count() from Lead ];
    System.debug('The Value of i is' + i);

if you found this answer helpful then please mark it as best answer so it can help others.   
  
  Thanks 
  Akshay


 
Akshay_DhimanAkshay_Dhiman
You can not use public Class in Execute Anonymous Window.  (Can not use public Class in Execute Anonymous Window.)

public class MySOSLSOQLExaples
{
public static void isTest(){
    Integer i = [Select Count() from Lead where Name='Smith'];
    System.debug('The Value of i is' + i);
 }
}
NagendraNagendra (Salesforce Developers) 
Hi Koti,

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.
public class MySOSLSOQLExaples {
    public void sosl(){
 	Integer i = [Select Count() from Lead];
    System.debug('The Value of i is' + i);
			} 
}
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

 
This was selected as the best answer
Koti P 6Koti P 6
Hi Akshay,

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
Koti P 6Koti P 6
Hi Nagendra,

thank you so much for details explanation , i will keep these tips in mind while writing the business logic.

Regards,
Koti