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
gvgv 

Visualforce SOQl

Hi

 

I am constructing a search page with where condition and order by condition.  I have 5 conditions which are selected from the page and for the 6 condition, I always select 'Active' as the value.

 

For example :

 

if queryStr contains the query to be executed, this is how its going to be

 

queryStr= 'Select id, name from products where'

If(other == true)

queryStr+= 'or product_type <> null' etc with other conditions

 

but the last condition I want is status needs to be active(which is a text data type)

 

so I tried,

 

queryStr+=  'and status = 'Active''';

 

obviously this is not working and I can't figure out a way to do this. I tried assigning Active to a variable and use that in the string but that does not work either.

 

Pls. help . this is a production issue and I would have to add the active condition so search returns correct value.

 

I have very limited experience in VF, so this very basic question

 

Thanks 

 

Best Answer chosen by Admin (Salesforce Developers) 
SuperfellSuperfell

because the generated soql needs to have the active quoted, e.g.

 

queryStr += "and status=\'Active\''; 

All Answers

SuperfellSuperfell
Judging by the schema, you want 'and isActive=true'
gvgv
Thanks Simon, but the Status field is string with 2 possible values 'Active' and 'Not Active'
SuperfellSuperfell
Then what you have should be fine, perhaps you can explain how its not working.
gvgv

The Query already has 4/5 conditions and I want to add a final and to the condition (status = Active) condition

 

if queryStr contains the query to be executed, this is how its going to be

 

queryStr= 'Select id, name from products where'

If(other == true)

queryStr+= 'where product_type <> null' 

 

so my querystr will be here

Select id,name from products where  product_type_c <> null and also other conditons

 

now I want the query to look like  

Select id,name from products where  product_type_c <> null and subtype  = '123' and status__c = 'Active'

My querystr has till subtype = 123, but I am struggling to add status__c = 'Active' towards the end.

I tired doing this

 

QUeryStr +=  ' and Status__c =' +  'Active'  ;

This does not work. I get the following error. 

 

 

Error: Error occurred while loading a Visualforce page. 
Error: unexpected token: 'Active' 

Hopefully I am clear otherwise pls. ask me I can explain

 

SuperfellSuperfell

because the generated soql needs to have the active quoted, e.g.

 

queryStr += "and status=\'Active\''; 

This was selected as the best answer
gvgv
Thanks that helped