Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I have a SOQL query below that works in the Force.com Explorer but when I implement it into my dev org Visualforce page, I get the following result: Error: Line Breaks not allowed in string literals at line 89 column -1. The link below is column 89

   // We dynamically build a query string and exclude items already in the shopping cart
    String qString = 'select Id, Pricebook2Id, IsActive, Product2.Name, Product2.Family, Product2.IsActive, Product2.Description, UnitPrice from PricebookEntry where Product2.Family = 'Service Contract' and Isactive=true;

Originally, the code looked like the statement below, but I need to filter out the Product Family so the products that have service contract for a product family are the only ones showing in the search.

   // We dynamically build a query string and exclude items already in the shopping cart
    String qString = 'select Id, Pricebook2Id, IsActive, Product2.Name, Product2.Family, Product2.IsActive, Product2.Description, ProductCode, UnitPrice from PricebookEntry where IsActive=true and Pricebook2Id = \'' + theBook.Id + '\'';

I got the code from this website: http://www.michaelforce.org/recipeView?id=a0G30000006eVxVEAU and the code works, but when I try to configure it to filter out certain products it gives me the error above. Any help would be appreciated. Thank you!

share|improve this question
    
Was just adding that to my answer. –  Daniel Hoechst Mar 19 at 15:09
    
You need to have dynamic query? Can't you retrieve the data into a list directly? –  highfive Mar 19 at 15:25
add comment

2 Answers 2

up vote 4 down vote accepted

Looks like you need to escape some single quotes and add another one:

String qString = 'select Id, Pricebook2Id, IsActive, Product2.Name, Product2.Family, Product2.IsActive, Product2.Description, UnitPrice from PricebookEntry where Product2.Family = \'Service Contract\' and Isactive=true';

But in this case, I wouldn't construct a string for the query, you can just assign the query to a list:

PricebookEntry[] pbe = [SELECT Id, Pricebook2Id, IsActive, Product2.Name,
    Product2.Family, Product2.IsActive, Product2.Description, UnitPrice from
    PricebookEntry where Product2.Family = 'Service Contract' and Isactive=true 
    and Pricebook2Id = :theBook.Id];
share|improve this answer
    
Thank you so much! –  NewbieDeveloper27 Mar 19 at 15:51
add comment

The force.com explorer is probably complaining (if it's not because of the missing/unescaped single quotes as given by Daniel) because you have "broken" your line up in the IDE.

Though in your question it is all one line, if you have done this in your code:

Line breaks

You will get the complaint

Line Breaks Not Allowed in String Literals

..You must END your string delimiter on each line, and then concatenate the string with a plus sign on the next line like this:

proper lines

Incidentally, the comments that have been made about not building this string up with the ID field injected in like that are all true. Use the prepared statement/bind variable style available in SOQL instead.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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