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
mohan s 37mohan s 37 

how to search a specific record by enter a keyword in search dialog box

I want to search a specific account record when i enter a record name  and click search button?  i was written the following code. While compliling the code there is no errors but when i click search button in visualforce page the following query exception throws.
System.QueryException: unexpected token: 'Name'
Error is in expression '{!queryData}' in component <apex:commandButton> in page soqldynamic: Class.DynamicSOQL.queryData: line 24, column 1
Class.DynamicSOQL.queryData: line 24, column 1
my class:
public class DynamicSOQL {
    public string accName{set;get;}
    public string accIndustry{set;get;}
    public List<Account>a{set;get;}
    public void queryData(){
        if(a!=null)
            a.clear();
        string q='select name,Industry from Account WHERE Name=:accName';
        if(accName!=''&&accName!=null&&accIndustry!=''&&accIndustry!=null){
            q=q+'where Name=\''+accName+'\'and Industry=\''+accIndustry+'\'';
        }
        else {
            if(accName!=''&&accName!=null){
           q=q+'where Name=\''+accName+'\''; 
        }
            else{
                if(accIndustry!=''&&accIndustry!=null){
            q=q+'where industry=\''+accIndustry+'\'';
            
        }
            }
            
        }
        a=database.query(q);
    }
    
    }
visualforce page:
<apex:page controller="DynamicSOQL" showHeader="true" sidebar="false">
    <apex:form >
        <apex:pageBlock title="Account Data">
            <apex:pageBlockButtons location="top">
                <apex:commandButton value="submit" action="{!queryData}"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection columns="1">
                <apex:pageBlockSectionItem >
                   Enter Name: <apex:inputText title="Enter Name" value="{!accName}"/>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                Enter Industry:<apex:inputText title="Enter Industry" value="{!accIndustry}"/>
                    </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
        </apex:pageBlock>
        <apex:pageBlock title="Account Records" rendered="{! !ISNULL(a)}">
        <apex:pageBlockTable value="{!a}" var="b">
            <apex:column value="{!b.Name}"/>
            <apex:column value="{!b.industry}"/>
            </apex:pageBlockTable>
            </apex:pageBlock>
    </apex:form>
</apex:page>
can any one help me what is the mistake that i have made in the code
Best Answer chosen by mohan s 37
sfdcMonkey.comsfdcMonkey.com
hi Mohan
try this onces
public class DynamicSOQL {
    public string accName{set;get;}
    public string accIndustry{set;get;}
    public List<Account> a {set;get;}
    public void queryData(){
        if(a!=null)
            a.clear();
        string q = 'select name,Industry from Account where name like \'%'+accName+'%\'and Industry like \''+accIndustry+'\' ';
        if(accName!='' && accName!=null && accIndustry!='' && accIndustry!= null){
          
            a = database.query(q);
           
        }
        else {
            if(accName!=''&&accName!=null){
           q=q+'where Name=\''+accName+'\''; 
        }
            else{
                if(accIndustry!=''&&accIndustry!=null){
            q=q+'where industry=\''+accIndustry+'\'';
            
        }
            }
            
        }
     
    }
    
    }
i only corraction in first if block where both are not null
now it will work :)
Thanks
Mark it best answer if it helps you :)
 

All Answers

sfdcMonkey.comsfdcMonkey.com
hi Mohan
try this onces
public class DynamicSOQL {
    public string accName{set;get;}
    public string accIndustry{set;get;}
    public List<Account> a {set;get;}
    public void queryData(){
        if(a!=null)
            a.clear();
        string q = 'select name,Industry from Account where name like \'%'+accName+'%\'and Industry like \''+accIndustry+'\' ';
        if(accName!='' && accName!=null && accIndustry!='' && accIndustry!= null){
          
            a = database.query(q);
           
        }
        else {
            if(accName!=''&&accName!=null){
           q=q+'where Name=\''+accName+'\''; 
        }
            else{
                if(accIndustry!=''&&accIndustry!=null){
            q=q+'where industry=\''+accIndustry+'\'';
            
        }
            }
            
        }
     
    }
    
    }
i only corraction in first if block where both are not null
now it will work :)
Thanks
Mark it best answer if it helps you :)
 
This was selected as the best answer
sfdcMonkey.comsfdcMonkey.com
another modify version try this
public class DynamicSOQL {
    public string accName{set;get;}
    public string accIndustry{set;get;}
    public List<Account> a {set;get;}
    public void queryData(){
        if(a!=null)
            a.clear();
        string q = 'select name,Industry from Account where name like \'%'+accName+'%\'and Industry like \''+accIndustry+'\' ';
        if(accName!='' && accName!=null && accIndustry!='' && accIndustry!= null){
          
            a = database.query(q);
           
        }
        else {
            if(accName!=''&&accName!=null){
             string q1 = 'select name,Industry from Account where name like \'%'+accName+'%\' ';   
             a = database.query(q1); 
        }
            else{          
              string q2 = 'select name,Industry from Account where Industry like \'%'+accIndustry+'%\' ';   
               a = database.query(q2); 
            
            }
            
        }
     
    }
    
    }
Let me inform if it work for you
 
mohan s 37mohan s 37
Thanq piyush this is exactly what i am looking.