1

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.

0

2 Answers 2

2

You are on right track. IN operator is usually used for filtering over collections like Set or List in SOQL.

In your example, you can try any of the following:

// Enclosing in single quotes
String query = 'SELECT Id, Name FROM Product2 WHERE Id = \'' + listId + '\'';

OR

// Cleaner one, using a colon
String query = 'SELECT Id, Name FROM Product2 WHERE Id = :listId';

Refer Dynamic SOQL document for more details.

6
  • 1
    I don't think that first approach will work.
    – Adrian Larson
    Feb 11, 2017 at 22:01
  • Ah you're right, need to enclose it in single quotes. I had not used that one in quite sometime, thanks.
    – Raul
    Feb 11, 2017 at 22:11
  • 1
    My understanding was it is a List variable. I think you need String.join and some parens here.
    – Adrian Larson
    Feb 11, 2017 at 23:10
  • From the code it looks like a string variable, getProgramsFromListId(String listId)
    – Raul
    Feb 12, 2017 at 11:58
  • Thanks Rahul, I tried it with getProgramsFromListName instead, because while using getProgramsFromListId instead, I get the error "Invalid Id field'. What worked for me was:
    – José
    Feb 12, 2017 at 22:55
1

What worked for me was:

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

    String query_str = '(\'';  
    query_str +=  + postgrado1 + '\',\'' + postgrado2 + '\'';
    query_str += ')';

    controller.getProgramsFromListName(query_str);
1
  • getProgramsFromListId(String listId) to getProgramsFromListId(List<String> listId), better would be to use a list
    – Raul
    Feb 12, 2017 at 23:10

You must log in to answer this question.

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