2

I have the following test class:

@isTest(SeeAllData=false)
private with sharing class classNameTest {

     @isTest
     static testMethod void methodName(){

        // declare a constant
        private static final String PROFILE_NAME = 'System Administrator';

        try{
            // use a constant in SOQL
            Profile p = [SELECT Id FROM Profile WHERE Name=PROFILE_NAME limit 1];
        }

         // remaining code

}

when I try to save the file, it gives me an error:

unexpected token 'PROFILE_NAME'.

How can I use the constant in an SOQL query?

2 Answers 2

5

It is a bind variable, so it needs to be used with colon. Also it should be declared outside the test method. Moreover, when you use @IsTest annotation on the method, you do not need to add testMethod modifier (which is deprecated). Finally, SeeAllData is false by default for test class, so it is not necessary to add this annotation to the class:

@IsTest
private with sharing class classNameTest {
    // declare constant
    private static final String PROFILE_NAME = 'System Administrator';

    @IsTest
    static void methodName(){        
        try {
            // use constant in soql
            Profile p = [SELECT Id FROM Profile WHERE Name = :PROFILE_NAME limit 1];
        } catch (Exception e){}
         
        // remaining code
    }
}
2
  • 2
    Nice answer that goes into some "why" not just "what" or "how" - I love those answers. :) 1) You might want to make your sentence read as, "Moreover, when you use @IsTest annotation on a method, you do not need..." (I added "on a method".) Just to clarify any possible ambiguity, since @IsTest is also used on the class. 2) You might also want to point out that testmethod is deprecated and @IsTest is preferred/suggested.
    – Moonpie
    Commented Sep 14, 2021 at 13:30
  • @Moonpie Thank you for your comment, I have updated the answer, also, feel free to edit it, if you want) Commented Sep 14, 2021 at 13:34
0

You are missing a colon in the SOQL query. It should be

Profile p = [SELECT Id FROM Profile WHERE Name=:PROFILE_NAME limit 1];

The colon is mandatory as it will let Salesforce know that you are trying to bind an Apex code variable in the SOQL query. For more details, please refer the official Salesforce documentation on this link https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SOQL_variables.htm

Also, the constant should be declared outside the method at the class level.

2
  • Welcome to Salesforce Stack Exchange (SFSE). Thank you for contributing.
    – Moonpie
    Commented Sep 14, 2021 at 13:32
  • What is the difference? Why should there be a colon in that location? Can you elaborate? Please respond by editing (changing) your answer, not here in comments (without "Edit:", "Update:", or similar - the answer should appear as if it was written right now). Commented Sep 14, 2021 at 19:12

You must log in to answer this question.

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