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.

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.

| improve this answer | |
  • 1
    I don't think that first approach will work. – Adrian Larson Feb 11 '17 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 '17 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 '17 at 23:10
  • From the code it looks like a string variable, getProgramsFromListId(String listId) – Raul Feb 12 '17 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 '17 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);
| improve this answer | |
  • getProgramsFromListId(String listId) to getProgramsFromListId(List<String> listId), better would be to use a list – Raul Feb 12 '17 at 23:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.