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
kullai king 1kullai king 1 

Error Apex Dynamical Query


System.QueryException: unexpected token: !=
Error is in expression '{!sear}' in component <apex:commandButton> in page listobjects: Class.listobjects.sear: line 57, column 1
Class.listobjects.sear: line 57, column 1

Please help me


public with sharing class listobjects {
    public String lisobj{get;set;}
    public String lisfields{get;set;}
    public string opira{get;set;}
    public String envalu{get;set;}
    public list<Sobject> accList{get;set;}
    public set<String> lisfiels{get;set;}
    public string query{get;set;}
    
    Public list<SelectOption> getobj(){
        List<SelectOption> opty=new List<SelectOption>();
        opty.add(new SelectOption('None','--none---'));
        opty.add(new selectoption('Account','Account'));
        opty.add(new selectoption('Lead','Lead'));
        opty.add(new selectoption('Contact','Contact'));
        opty.add(new selectoption('Opportunity','Opportunity'));
        opty.add(new selectoption('Reports','Reports'));
        opty.add(new selectoption('testA__c','testA__c'));
        return opty;
    }
    
    public listobjects(){
    
        lisfiels=new set<string>();
    }
    
    public list<SelectOption> getcall(){
    
        list<selectoption> losfils=new list<selectoption>();
        try{
            losfils.add(new SelectOption('None','--none---'));
            map<String,Schema.SObjectType> schemap = Schema.getGlobalDescribe();
            Schema.SObjectType ts=schemap.get(lisobj);
            Schema.DescribeSobjectResult dsobjresul=ts.getDescribe();
            Map<String,Schema.SObjectField> fildres=dsobjresul.Fields.getmap();
            for(Schema.SobjectField sts:fildres.values()){
                Schema.DescribeFieldResult dfc=sts.getDescribe();
                losfils.add(new SelectOption(dfc.getName(),dfc.getLabel()));
            }
        }catch(Exception e){}
        return losfils;
    }
    public list<Selectoption> getval(){
        list<Selectoption> opera=new list<Selectoption>();
        opera.add(new Selectoption('--None--','--None--'));
        opera.add(new Selectoption('%','Equal'));
        opera.add(new Selectoption('!=','NotEqual'));
        opera.add(new Selectoption('>:','Greaterthan'));
        return opera;
    }
    
    public void sear(){
        string query;
        Schema.SObjectType tt=Schema.getGlobalDescribe().get(lisobj);
                              
        query='Select id,Name from ' + tt   + 'where' + lisfields + '!=:' +envalu;
        list<Sobject> ss=database.query(query);
        system.debug('SSSSSSSSSSSSS'+query);
        
    }
    
}
Best Answer chosen by kullai king 1
sandeep@Salesforcesandeep@Salesforce
I tried to debug your query in my org using Account object instead. Here I found below points
1. You are are not puttin any spaces between words like From , Querying Object, where and where clause. 
2. envalu is a variable so it should come like below 
lisfields + '!=: envalu' ;

so that what when it gets executed it behace like a variable.

Thanks
Sandeep Singhal
http://www.codespokes.com/

All Answers

sandeep@Salesforcesandeep@Salesforce
I tried to debug your query in my org using Account object instead. Here I found below points
1. You are are not puttin any spaces between words like From , Querying Object, where and where clause. 
2. envalu is a variable so it should come like below 
lisfields + '!=: envalu' ;

so that what when it gets executed it behace like a variable.

Thanks
Sandeep Singhal
http://www.codespokes.com/
This was selected as the best answer
kullai king 1kullai king 1
Hi Sandeep,

Thanks For Giving Response. I got the Isseu.