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
SDFC FirstLevelerSDFC FirstLeveler 

I am trying to create a Button on VF Page dynamic search, which will use to search dynamically the records in account but i am an error at line no. 12 Expression cannot be assigned.

The button will search account records if I dont put any values for any
VF page

<apex:page controller="DynaSearch" >
<apex:form >
    <apex:pageBlock title="Search">
        <apex:pageBlockSection >
            <apex:pageBlockSectionItem >
                <apex:outputLabel Value="Name"/>
                <apex:inputText value="{!aName}"/>  
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                <apex:outputLabel Value="Type"/>
                <apex:inputText value="{!aType}"/>  
            </apex:pageBlockSectionItem>
        </apex:pageBlockSection> 
    
       <apex:pageBlockButtons location="Bottom">
       <apex:commandButton value="Search" Action="{!search}"/>
       <apex:commandButton value="Desired Search" Action="{!desiredSearch}"/>
       </apex:pageBlockButtons>
       </apex:pageBlock>
       
       <apex:pageBlock title="Search Result" rendered="{! !ISNull(a)}">
    <apex:pageBlockTable value="{!a}" var="b">
    <apex:column value="{!b.Name}"/>
     <apex:column Value="{!b.Type}"/>
    </apex:pageBlockTable>    
    </apex:pageBlock>
     
</apex:form>
</apex:page>


Controller Class:



public class DynaSearch {
    public list <Account> a {set;get;}
    public string aName {set;get;}
    public string aType {set;get;}
    
    public void search(){
        a=[SELECT Name, Type from Account where name=:aName and Type=:aType];
        
    }
    public void desiredSearch(){
          string Dsearch;
        if((aName= Null && aName= '') && (aType= Null && aType='')){
                Dsearch ='SELECT Name, Type from Account';
        } 
        
       else if ((aName!=Null && aName!= '') && (aType!=Null && aType!='')){
             Dsearch= 'SELECT name, Type from Account where name=\' '+aName+' \' and type=\' '+aType+' \''; 
         }
   
        else {
            	if(aName!=Null && aName!= ''){
             		Dsearch= 'SELECT name, Type from Account where name=\' '+aName+' \'';  
                } else{
                    	if(aType!=Null && aType!=''){
            				Dsearch= ' SELECT name, Type from Account where type=\' '+aType+' \'';
                        	}
                		}
         }
           
         		a= Database.query(Dsearch);
    } 

}

input test fields.
SDFC FirstLevelerSDFC FirstLeveler
Please correct I am getting error at line no. 49
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Abhishek,

 May I suggest you please refer the below link for reference for code on Dynamic Search Functionality. Hope it will be helpful.

please mark it as the best answer if the information is informative.

Best Regards
Rahul Kumar
v varaprasadv varaprasad
Hi Abhishek,

please change below line: 

if((aName == Null && aName== '') && (aType == Null && aType !=''))

Please check once and let me know.

Thanks
Varaprasad
v varaprasadv varaprasad
if((aName == Null && aName== '') && (aType == Null && aType == ''))
 
SDFC FirstLevelerSDFC FirstLeveler
Pleaes review my code and help me
Controller Code:

public class HappySearch {
    public list <Account> a {set;get;}
    public string aName {set;get;}
    public string aType {set;get;}
    
    
    public void desiredSearch(){
                        string Dsearch;
                        if ((aName== Null && aName== '') && (aType==Null && aType=='')){
                            Dsearch= 'SELECT name, Type from Account';}
                            
                           else{        
                                 if ((aName!=Null && aName!= '') && (aType!=Null && aType!='')){
                                    Dsearch= 'SELECT name, Type from Account where name=\' '+aName+' \' and type=\' '+aType+' \'';}
                                 else {
                                        if(aName!=Null && aName!= ''){
                                            Dsearch= 'SELECT name, Type from Account where name=\' '+aName+' \'';} 
                                        else{
                                             if(aType!=Null && aType!=''){
                                                  Dsearch= ' SELECT name, Type from Account where type=\' '+aType+' \'';}
                                            }
                                        } 
                                    }           
                    a= Database.query(Dsearch);
    } 
}



VFPage Code:


<apex:page controller="HappySearch" >
<apex:form >
    <apex:pageBlock title="Search">
        <apex:pageBlockSection >
            <apex:pageBlockSectionItem >
                <apex:outputLabel Value="Name"/>
                <apex:inputText value="{!aName}"/>  
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                <apex:outputLabel Value="Type"/>
                <apex:inputText value="{!aType}"/>  
            </apex:pageBlockSectionItem>
        </apex:pageBlockSection> 
    
       <apex:pageBlockButtons location="Bottom">
       <apex:commandButton value="DSearch" Action="{!desiredSearch}"/>
       </apex:pageBlockButtons>
       </apex:pageBlock>
       
       <apex:pageBlock title="Search Result" >
            <apex:pageBlockTable value="{!a}" var="b">
            <apex:column value="{!b.Name}"/>
            <apex:column Value="{!b.Type}"/>
            </apex:pageBlockTable>    
       </apex:pageBlock>
     
</apex:form>
</apex:page>
v varaprasadv varaprasad
Hi ,

please check once below code..
 
public class DynaSearch {
    public list<Account> a {set;get;}
    public string aName {set;get;}
    public string aType {set;get;}
    public boolean resultMsg;
    
    public DynaSearch() {
        a = New list<Account>();
        resultMsg = false;
    }
    
    public void search(){
        a=[SELECT Name, Type from Account where name=:aName and Type=:aType];
        
    }
    public void desiredSearch(){
        //String.isNotEmpty(accountName)
        string Dsearch,accountName,accType;
        Dsearch ='SELECT Name, Type from Account ';
        if(String.isEmpty(aName) && String.isEmpty(aType)){
            Dsearch ='SELECT Name, Type from Account ';
            system.debug('==Dsearch=='+Dsearch);
        } else if (String.isNotEmpty(aName) && String.isEmpty(aType)){
            accountName = '%'+aName+'%';           
            Dsearch= Dsearch + 'where name like: accountName';
        } else {
             accType = '%'+aType+'%';           
            Dsearch= Dsearch + 'where type like: accType';
        }  
        
        a= Database.query(Dsearch);        
    } 
    
}

Hope this helps!!!

Let me know if you need more assistance on this. 

Thanks
Varaprasad