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
SaakuSaaku 

Fetch the record from custom object Customer__c by name and it should display relevant fields

Hi folks!
My requirement
my apec class is
public class fetch {
   public String accountname{set;get;}
    public string destquery{set;get;}
    public List<customer__c> cust{set;get;}
    public fetch(){
       string accountname;
        cust=new List<customer__c>();
    }
    public void show(){   
        destquery='select Balance__c,City__c,State__c from Customer__c where name=\'' +accountname+'\'';       
    }
}

My vf page is
<apex:page controller="fetch">
    <apex:form >
    <apex:pageBlock >
        <apex:outputLabel value="Name" /><apex:inputText >{!accountname}</apex:inputText>
        <apex:commandButton value="Ok" action="{!show}"/>
    </apex:pageBlock>
        <apex:pageBlock >
      <!--    <apex:pageBlockTable value="{!destquery}" var="a">
               <apex:column value="{!a.Balance__c}" /> 
                 <apex:column value="{!a.City__c}" />
                 <apex:column value="{!a.State__c}" />          
            </apex:pageBlockTable> -->
           {!accountname}
        </apex:pageBlock>
    </apex:form>
</apex:page>

butI'm not getting the fetching records pls help where I done wrong.

Thanks in Advance
Best Answer chosen by Saaku
Bhanu MaheshBhanu Mahesh
Hi,

You are preparing the query string but you are not querying the records.
you have to use database.query() method and pass the query string as a parameter for this method. And then siplay the records in pageBlock Table

And also in page for pageBlock table you have defined value as destquery which is a string. Instead you need to pass List<customer__c> ie..cust

Try below code

Page
<apex:page controller="fetch">
    <apex:form >
    <apex:pageBlock >
        <apex:outputLabel value="Name" /><apex:inputText value="{!accountname}"/>
        <apex:commandButton value="Ok" action="{!show}" reRender="table"/>
    </apex:pageBlock>
        <apex:pageBlock  id="table">
        <apex:pageBlockTable value="{!cust}" var="a">
               <apex:column value="{!a.Balance__c}" /> 
                 <apex:column value="{!a.City__c}" />
                 <apex:column value="{!a.State__c}" />          
            </apex:pageBlockTable> 
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller
public class fetch{
   public String accountname{set;get;}
    public string destquery{set;get;}
    public List<customer__c> cust{set;get;}
    public fetch(){
        cust=new List<customer__c>();
    }
    public PageReference show(){   
        destquery='select Balance__c,City__c,State__c from Customer__c where name=:accountname'; //if you want the customer with exact name which is entered for search
		//if you want all the customers with the searchstring as a part of the name use below query. COmment the above line and uncomment the below
		//destquery='select Balance__c,City__c,State__c from Customer__c where name like \'%'+accountname+'%\''; 
		cust = database.query(destquery);
		return null;
    }
}

Mark this as "SOLVED" if your query is Answered

Regards,
Bhanu Mahesh

All Answers

Bhanu MaheshBhanu Mahesh
Hi,

You are preparing the query string but you are not querying the records.
you have to use database.query() method and pass the query string as a parameter for this method. And then siplay the records in pageBlock Table

And also in page for pageBlock table you have defined value as destquery which is a string. Instead you need to pass List<customer__c> ie..cust

Try below code

Page
<apex:page controller="fetch">
    <apex:form >
    <apex:pageBlock >
        <apex:outputLabel value="Name" /><apex:inputText value="{!accountname}"/>
        <apex:commandButton value="Ok" action="{!show}" reRender="table"/>
    </apex:pageBlock>
        <apex:pageBlock  id="table">
        <apex:pageBlockTable value="{!cust}" var="a">
               <apex:column value="{!a.Balance__c}" /> 
                 <apex:column value="{!a.City__c}" />
                 <apex:column value="{!a.State__c}" />          
            </apex:pageBlockTable> 
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller
public class fetch{
   public String accountname{set;get;}
    public string destquery{set;get;}
    public List<customer__c> cust{set;get;}
    public fetch(){
        cust=new List<customer__c>();
    }
    public PageReference show(){   
        destquery='select Balance__c,City__c,State__c from Customer__c where name=:accountname'; //if you want the customer with exact name which is entered for search
		//if you want all the customers with the searchstring as a part of the name use below query. COmment the above line and uncomment the below
		//destquery='select Balance__c,City__c,State__c from Customer__c where name like \'%'+accountname+'%\''; 
		cust = database.query(destquery);
		return null;
    }
}

Mark this as "SOLVED" if your query is Answered

Regards,
Bhanu Mahesh
This was selected as the best answer
Rk NRk N

my apec class is
my apec class is
public class fetch {
   public String accountname{set;get;}
    public string destquery{set;get;}
    public List<customer__c> cust{set;get;}                 //You don't even need this variable
    public fetch(){
       string accountname;
        cust=new List<customer__c>();                  //Also the constructor part is not required
    }
    public void show(){   
        destquery='select Balance__c,City__c,State__c from Customer__c where name=\'' +accountname+'\'';       //Query should always be written in Square brackets [  ]
    }
}
 
// VF part done by you is almost  okay.

So, here is the working code for this. You can replace the object and fields according to your requirement.
VF Page
Controller Class
public class fetch1 {
   public String accountname{set;get;}
    public list<merchandise__c> destquery{set;get;}
    
    public void show(){  
        destquery=new list<merchandise__c>(); 
        destquery=[select quantity__c, price__c from merchandise__c where name=:accountname];       
    }
}
VF Page
<apex:page controller="fetch1">
    <apex:form >
    <apex:pageBlock >
        Name<apex:inputText value="{!accountname}"/>
        <apex:commandButton value="Ok" action="{!show}"/>
    </apex:pageBlock>
    
    <apex:pageblock >
        <apex:outputpanel >
            <apex:pageBlockTable value="{!destquery}" var="a">
                <apex:column value="{!a.Quantity__c}" />
                <apex:column value="{!a.Price__c}" />          
            </apex:pageBlockTable> 
        </apex:outputpanel>
    </apex:pageblock>
    </apex:form>
</apex:page>
SaakuSaaku
Hi Rk, Thanks for your reply and I want to use dynamic soql instead of static that's why only I used ( these brackets).And now I solved my doubt
SaakuSaaku
Hi mahesh, Thank you so much now my code is working without errors