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
Aron Schor [Dev]Aron Schor [Dev] 

Class involving a query and a WHERE clause

I am not sure if my subject is clear, but hopefully this helps.

I am working on a Site with a Product search feature for different divisions (division__C)

I want each division search page to only search for products of that division.

I have a search feature working, just need help tweaking it.  I have tried some things on my own with no luck, so I am hoping I can get some help.

1. If I change the class to say
WHERE Division__C = Office/School AND Area__C...

it says Authorization Required on search (Guest has field access)
I have a search feature working, just need help tweaking it.

2. If I change class to say
...OR Case_Quantity__c like \'%'+searchstring+'%\' AND Division__c like \'%'+Office/School+'%\''

it says Error    Error: Compile Error: Variable does not exist: Office at line 15 column 521

_____________

Working code:

public with sharing class prodsearchschool {

    private String sortOrder = 'Name';
    public list <Product2> Prod {get;set;} 
    public String searchstring {get;set;}
    public String searchquery {get;set;}
    public String sortField {get;set;}
    public String lastClicked {get;set;}
    public String sortDir {get;set;}

    public prodsearchschool() {}
     
         
    public PageReference search(){
        searchquery='select Area__C, ProductCode, Name, Color__C, Overall_Length__C, H__C, W__C, L__C, Case_Quantity__c from Product2 WHERE Area__C like \'%'+searchstring+'%\' OR ProductCode like \'%'+searchstring+'%\' OR Name like \'%'+searchstring+'%\' OR Color__C like \'%'+searchstring+'%\' OR Overall_Length__C like \'%'+searchstring+'%\' OR H__C like \'%'+searchstring+'%\' OR L__C like \'%'+searchstring+'%\' OR W__C like \'%'+searchstring+'%\' OR Case_Quantity__c like \'%'+searchstring+'%\'';  
        Prod = database.query(searchquery);
        return null;
    } 

    public void clear(){ 
        prod.clear(); 
    }

    public PageReference sortThis() {  
        if(lastClicked == null){
            sortDir = 'ASC';
        }
        else{
            if(lastClicked == sortField){
                sortDir = 'DESC';
            }
            else{
                sortDir = 'ASC';
            }
        }
        lastClicked = sortField;
       prod = Database.query(searchquery + ' order by ' + sortField + ' ' + sortDir);
       return null;
    } 
}
yvk431yvk431
Try using the searchstring like this

Color__C like ' + '\'%'+searchstring+'%\''


--yvk
Aron Schor [Dev]Aron Schor [Dev]
The search string works but I need it to only search the industrial Division.
sandeep sankhlasandeep sankhla
Hi Aron,

Did you get the solutioj or still you are facing the same issue ? Please let me knwo so we can help..

Thanks,
Sandeep
Aron Schor [Dev]Aron Schor [Dev]
No answer yet Sandeep, let me know, thanks!
sandeep sankhlasandeep sankhla
Okay so can you put a debug statment and show me what you are getting in query...after framing the query ho wit is looking like..

 
Aron Schor [Dev]Aron Schor [Dev]
For the second example I get an error so I can't save.

The second seems to run, its an issue displaying on the site.  Maybe I have some odd setting?

Is this the debug info you need?

33.0 APEX_CODE,FINEST;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
05:55:38.031 (31242883)|EXECUTION_STARTED
05:55:38.031 (31280653)|CODE_UNIT_STARTED|[EXTERNAL]|VisualForce View State
05:55:38.037 (37578689)|CODE_UNIT_FINISHED|VisualForce View State
05:55:38.038 (38893283)|EXECUTION_FINISHED
05:55:38.957 (301051463)|CUMULATIVE_LIMIT_USAGE
05:55:38.957|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

05:55:38.957|CUMULATIVE_LIMIT_USAGE_END
sandeep sankhlasandeep sankhla
can you paste your entire code using <> and also share the line no where you are getting the error..
Aron Schor [Dev]Aron Schor [Dev]
Does this help?

This saves (I add WHERE Division__C = Office/School)

Try going here and searching for ruler
http://acmesalesdev-developer-edition.na24.force.com/officeschool_search
Guest has access

public with sharing class prodsearchschool {

    private String sortOrder = 'Name';
    public list <Product2> Prod {get;set;} 
    public String searchstring {get;set;}
    public String searchquery {get;set;}
    public String sortField {get;set;}
    public String lastClicked {get;set;}
    public String sortDir {get;set;}

    public prodsearchschool() {}
     
         
    public PageReference search(){
        searchquery='select Area__C, ProductCode, Name, Color__C, Overall_Length__C, H__C, W__C, L__C, Case_Quantity__c from Product2 WHERE Division__C = Office/School AND Area__C like \'%'+searchstring+'%\' OR ProductCode like \'%'+searchstring+'%\' OR Name like \'%'+searchstring+'%\' OR Color__C like \'%'+searchstring+'%\' OR Overall_Length__C like \'%'+searchstring+'%\' OR H__C like \'%'+searchstring+'%\' OR L__C like \'%'+searchstring+'%\' OR W__C like \'%'+searchstring+'%\' OR Case_Quantity__c like \'%'+searchstring+'%\'';
        Prod = database.query(searchquery);
        return null;
    } 

    public void clear(){ 
        prod.clear(); 
    }

    public PageReference sortThis() {  
        if(lastClicked == null){
            sortDir = 'ASC';
        }
        else{
            if(lastClicked == sortField){
                sortDir = 'DESC';
            }
            else{
                sortDir = 'ASC';
            }
        }
        lastClicked = sortField;
       prod = Database.query(searchquery + ' order by ' + sortField + ' ' + sortDir);
       return null;
    } 
}




__________


If I add this: AND Division__c like \'%'+Office/School+'%\'' (See the end)

searchquery='select Area__C, ProductCode, Name, Color__C, Overall_Length__C, H__C, W__C, L__C, Case_Quantity__c from Product2 WHERE Area__C like \'%'+searchstring+'%\' OR ProductCode like \'%'+searchstring+'%\' OR Name like \'%'+searchstring+'%\' OR Color__C like \'%'+searchstring+'%\' OR Overall_Length__C like \'%'+searchstring+'%\' OR H__C like \'%'+searchstring+'%\' OR L__C like \'%'+searchstring+'%\' OR W__C like \'%'+searchstring+'%\' ...OR Case_Quantity__c like \'%'+searchstring+'%\' AND Division__c like \'%'+Office/School+'%\''

it says Error    Error: Compile Error: Variable does not exist: Office at line 15 column 521
sandeep sankhlasandeep sankhla
use below and check
 
searchquery='select Area__C, ProductCode, Name, Color__C, Overall_Length__C, H__C, W__C, L__C, Case_Quantity__c from Product2 WHERE Area__C like \'%'+searchstring+'%\' OR ProductCode like \'%'+searchstring+'%\' OR Name like \'%'+searchstring+'%\' OR Color__C like \'%'+searchstring+'%\' OR Overall_Length__C like \'%'+searchstring+'%\' OR H__C like \'%'+searchstring+'%\' OR L__C like \'%'+searchstring+'%\' OR W__C like \'%'+searchstring+'%\' ...OR Case_Quantity__c like \'%'+searchstring+'%\' AND Division__c like \'%'+'Office/School'+'%\''

please provide that as string insetad of direct name..then check if it works...because everywhere you ahev used string type variable but here it is strin not variable..so quotes you should give

please check and let me know
Aron Schor [Dev]Aron Schor [Dev]
It saves but it says 
Authorization Required 

You must first log in or register before accessing this page. 
If you have forgotten your password, click Forgot Password to reset it. 

on search.

If I go to Public Access Settings in Site and then Product Field-Level Security for profile, all are visible.
sandeep sankhlasandeep sankhla
make sure your page and class are added to site...

 
Aron Schor [Dev]Aron Schor [Dev]
Yes, you can see the page so its public.  The class is also enabled.

User-added image