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
Jose María Nicolás ArfelisJose María Nicolás Arfelis 

expecting colon, found ERROR MESSAGE after running test class

Hi all,

I am trying to cover some lines in my controller apex class, but since hours dealing with the error message:
 
Expecting colon, found 'TestPostgrado1'.
The method I am trying to cover through my controller test class is this (method in my controller):
 
public list<Product2> getProgramsFromListId(String listId){
    list<Product2> listProgramas = new list<Product2>();
    String query = 'Select Id, Name from Product2 where Id In ' + listId ; 
    listProgramas = Database.query(query);
    return listProgramas;
}
In order to cover that part I have written these lines in my code on the controller test class:
 
Product2 postgrado1 =  new Product2(Name = 'TestPostgrad1');
insert postgrado1;

Product2 postgrado2 =  new Product2(Name = 'TestPostgrad2');
insert postgrado2;

List<Product2> postgrados = new List<Product2> ();
postgrados.add(postgrado1);
postgrados.add(postgrado2);
        
        //List<Product2> listProgramas = new list<Product2>();
        //listProgramas.add(postgrado.Name);
  
controller.getProgramsFromListName(postgrado1.Name);
ok, I know postgrado1.Name is not correct, I was trying with several variants, but nothing happens, the errors is still being displayed.
I tend to think that it has to do with the "IN" in the SELECT statement in the controller method,

Does anybody know, what I am doing wrong or what is missing in the lines in my test class?. The errors are pointing to the line
 
controller.getProgramsFromListName(postgrado1.Name);
in my controller test class.

and to the line
 
listProgramas = Database.query(query);
I would appreciate any help.





 
Alain CabonAlain Cabon
Where is the method getProgramsFromListName ? (there is just getProgramsFromListId above)
Jose María Nicolás ArfelisJose María Nicolás Arfelis
Thanks for responding. Sorry the method to invoke above in the test class, should be
 
controller.getProgramsFromListId(Id);
if you would like to know what the getProgramsFromList Name has:
 
public list<Product2> getProgramsFromListName(String listName){
    list<Product2> listProgramas = new list<Product2>();
    String query = 'Select Id, Name from Product2 where Name In ' + listName ; 
    listProgramas = Database.query(query);
    return listProgramas;
}


 
Jose María Nicolás ArfelisJose María Nicolás Arfelis
Still hoping to get responses.
As addition to the description above, there are the following lines in my controller class:
 
public String array_values{get;set;} 
List<String> listStrings = array_values.split(',');
        String query_str = '(\'';                 
        for (String programa : listStrings){

            query_str += programa + '\',\'';
        }
        query_str = query_str.substring(0,query_str.length()-2);
        system.debug('@@@ query_str:' +query_str);
        if(listStrings.size() == 0) query_str += '(\'\'';
        query_str += ')';
   
		
        List<Product2> listProgramas = getProgramsFromListId(query_str);
I am trying to amend the issue while running the test, but still can not solve it.
 
Alain CabonAlain Cabon
Hi
public list<Product2> getProgramsFromListName(String listName){
    list<Product2> listProgramas = new list<Product2>();
    String query = 'Select Id, Name from Product2 where Name In (\'' + listName + '\')';
    listProgramas = Database.query(query);
    return listProgramas;
}

Regards
Alain
Jose María Nicolás ArfelisJose María Nicolás Arfelis
Thanks AlaIn  for responding. I solved the problem already. The same content is to find in Salesforce Success.