Skip to main content The Trailblazer Community will be unavailable from 2/1/2025 to 2/2/2025. Please plan your activities accordingly.
Error: Compile Error: Found punctuation symbol or operator '%' that isn't valid in Apex. at line 6 column 87

Apex Code

public class searchBox {

    public list <Account> acc {get;set;}

    public String searchKey {get;set;}

    public searchBox( ) {} 

    public void search(){

        string searchquery='select Name,id from account where name like \ +searchKey+'%\'';

        acc= Database.query(searchquery);

    }

}

This My VF CODE

<apex:page Controller="searchBox">

    <apex:form >

        <apex:inputText value="{!searchKey}" label="Input"/><br/>

        <apex:commandButton value="Enter" action="{!search}"/>

        <apex:pageBlock title="Searched Accounts are:-">

            <apex:pageBlockTable value="{!acc}" var="a">

                <apex:column value="{!a.name}"/>

                <apex:column value="{!a.id}"/>

            </apex:pageBlockTable>

        </apex:pageBlock>

    </apex:form>

</apex:page>
2 answers
  1. May 19, 2021, 2:17 PM
    Hii Yash

    Try Below Code

    public class searchBox {

    public list <Account> acc {get;set;}

    public String searchKey {get;set;}

    public searchBox( ) {}

    public void search(){

    string searchquery='select Name,id from account where name like '+searchKey+'%' ;

    acc.add(Database.query(searchquery));

    }

    }

    Please Mark It As Best Answer If It Helps

    Thank You!

     
Loading
0/9000