You need to sign in to do that
Don't have an account?
AYUSH JAIN 29
Getting Error in Database.query()
I want to design a VF Page. In which after selecting any object(lead/contact) all its fields will be displayed and based on operator and input value we can search data. But Database.query() in search is not working and also <apex:column value="{!a['name']}" Here is my code :
<apex:page controller="searchLeadorContact_task6_cntlr">
<apex:form >
<apex:pageBlock id="pbs1" title="Search Lead or Contact">
<apex:pageMessages ></apex:pageMessages>
<apex:pageBlockSection title="Select Object">
<apex:outputLabel value="Select Object :"></apex:outputLabel>
<apex:selectList value="{!sobj}" size="1" onchange="call_field()" >
<apex:selectOptions value="{!listObj}"></apex:selectOptions>
</apex:selectList>
<apex:actionFunction name="call_field" action="{!FieldSelect}" rerender="pbs2"/>
</apex:pageBlockSection>
<apex:pageBlockSection id="pbs2" title="Fields" rendered="true" columns="1" >
<apex:pageBlockSectionItem >
<apex:outputLabel value="Select Fields:"></apex:outputLabel>
<apex:selectList value="{!sfield}" size="1">
<apex:selectOptions value="{!listFields}"></apex:selectOptions>
</apex:selectList>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel value=" Select Operator"></apex:outputLabel>
<apex:selectList value="{!soperator}" size="1">
<apex:selectOptions value="{!operators}"></apex:selectOptions>
</apex:selectList>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel value="Enter data"></apex:outputLabel>
<apex:inputText value="{!data}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel value=""></apex:outputLabel>
<apex:commandButton value="Search" action="{!Search}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockTable value="{!tableData}" var="a">
<apex:column headerValue="ID" value="{!a}"/>
<apex:column headerValue="Name" value={!a['Name']}/>
<apex:column headerValue="{!sfield}"/>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
******************
public class searchLeadorContact_task6_cntlr {
public String data { get; set; }
public String soperator { get; set; }
public Boolean showPBS2 { get; set; }
public String sfield { get; set;}
public String sobj { get; set; }
public Set<String> setFields {get;set;}
public Schema.SObjectType sobject1 {get;set;}
public list<SelectOption> listObj { get; set; }
public List<SelectOption> listFields { get; set; }
public list<Schema.SObjectField> listSfields{get;set;}
public Map<String,Schema.SObjectField> mobjField {get;set;}
public list<sobject> tableData {get;set;}
public searchLeadorContact_task6_cntlr ()
{ showPBS2= false;
listObj = new List<SelectOption>();
listObj.add(new SelectOption('None','None'));
listObj.add(new SelectOption('Lead','Lead'));
listObj.add(new SelectOption('Contact','Contact'));
sobject1 = null;
setFields = new Set<String>();
listSfields = new List<Schema.SObjectField>();
mobjField = new Map<String,Schema.SObjectField>();
listFields = new List<SelectOption>();
tableData = new List<sobject>();
}
public void FieldSelect()
{ System.debug('Selected Sobject is'+sobj);
if(sobj=='None')
{ ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Please Select an Object'));
}
else{
showPBS2= true;
System.debug('Field Select Function call');
sobject1 = Schema.getGlobalDescribe().get(sobj);
mobjField = sobject1.getDescribe().fields.getMap();
setFields.addAll(mobjField.keySet());
System.debug('Set Field values are'+setFields);
listFields.add(new SelectOption('None','--None--'));
for(String s:setFields)
{
listFields.add(new SelectOption(s,s));
}
}
}
public List<SelectOption> getoperators() {
List<SelectOption> option=new List<SelectOption>();
option.add(new SelectOption('none', '--None--'));
option.add(new SelectOption('=','equals'));
option.add(new SelectOption('!=','not equal to'));
option.add(new SelectOption('<','less than'));
option.add(new SelectOption('>','greater than'));
option.add(new SelectOption('<=','less or equal'));
option.add(new SelectOption('>=','greater or equal'));
option.add(new SelectOption('Like','contains'));
option.add(new SelectOption('NotLike','does not contain'));
option.add(new SelectOption('Likestart','starts with'));
return option;
}
public void Search() {
String query = 'SELECT id,name,' +sfield+ ' from '+sobj+' WHERE '+sfield +' '+soperator+':' +data ;
// String company = 'American Banking Corp.';
// String query = 'select id , name from Lead where company = :company';
System.debug('query is '+query);
System.debug(Database.query(query));
tableData = Database.query(query);
System.debug('table data is '+tableData);
}
}
<apex:page controller="searchLeadorContact_task6_cntlr">
<apex:form >
<apex:pageBlock id="pbs1" title="Search Lead or Contact">
<apex:pageMessages ></apex:pageMessages>
<apex:pageBlockSection title="Select Object">
<apex:outputLabel value="Select Object :"></apex:outputLabel>
<apex:selectList value="{!sobj}" size="1" onchange="call_field()" >
<apex:selectOptions value="{!listObj}"></apex:selectOptions>
</apex:selectList>
<apex:actionFunction name="call_field" action="{!FieldSelect}" rerender="pbs2"/>
</apex:pageBlockSection>
<apex:pageBlockSection id="pbs2" title="Fields" rendered="true" columns="1" >
<apex:pageBlockSectionItem >
<apex:outputLabel value="Select Fields:"></apex:outputLabel>
<apex:selectList value="{!sfield}" size="1">
<apex:selectOptions value="{!listFields}"></apex:selectOptions>
</apex:selectList>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel value=" Select Operator"></apex:outputLabel>
<apex:selectList value="{!soperator}" size="1">
<apex:selectOptions value="{!operators}"></apex:selectOptions>
</apex:selectList>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel value="Enter data"></apex:outputLabel>
<apex:inputText value="{!data}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel value=""></apex:outputLabel>
<apex:commandButton value="Search" action="{!Search}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockTable value="{!tableData}" var="a">
<apex:column headerValue="ID" value="{!a}"/>
<apex:column headerValue="Name" value={!a['Name']}/>
<apex:column headerValue="{!sfield}"/>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
******************
public class searchLeadorContact_task6_cntlr {
public String data { get; set; }
public String soperator { get; set; }
public Boolean showPBS2 { get; set; }
public String sfield { get; set;}
public String sobj { get; set; }
public Set<String> setFields {get;set;}
public Schema.SObjectType sobject1 {get;set;}
public list<SelectOption> listObj { get; set; }
public List<SelectOption> listFields { get; set; }
public list<Schema.SObjectField> listSfields{get;set;}
public Map<String,Schema.SObjectField> mobjField {get;set;}
public list<sobject> tableData {get;set;}
public searchLeadorContact_task6_cntlr ()
{ showPBS2= false;
listObj = new List<SelectOption>();
listObj.add(new SelectOption('None','None'));
listObj.add(new SelectOption('Lead','Lead'));
listObj.add(new SelectOption('Contact','Contact'));
sobject1 = null;
setFields = new Set<String>();
listSfields = new List<Schema.SObjectField>();
mobjField = new Map<String,Schema.SObjectField>();
listFields = new List<SelectOption>();
tableData = new List<sobject>();
}
public void FieldSelect()
{ System.debug('Selected Sobject is'+sobj);
if(sobj=='None')
{ ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Please Select an Object'));
}
else{
showPBS2= true;
System.debug('Field Select Function call');
sobject1 = Schema.getGlobalDescribe().get(sobj);
mobjField = sobject1.getDescribe().fields.getMap();
setFields.addAll(mobjField.keySet());
System.debug('Set Field values are'+setFields);
listFields.add(new SelectOption('None','--None--'));
for(String s:setFields)
{
listFields.add(new SelectOption(s,s));
}
}
}
public List<SelectOption> getoperators() {
List<SelectOption> option=new List<SelectOption>();
option.add(new SelectOption('none', '--None--'));
option.add(new SelectOption('=','equals'));
option.add(new SelectOption('!=','not equal to'));
option.add(new SelectOption('<','less than'));
option.add(new SelectOption('>','greater than'));
option.add(new SelectOption('<=','less or equal'));
option.add(new SelectOption('>=','greater or equal'));
option.add(new SelectOption('Like','contains'));
option.add(new SelectOption('NotLike','does not contain'));
option.add(new SelectOption('Likestart','starts with'));
return option;
}
public void Search() {
String query = 'SELECT id,name,' +sfield+ ' from '+sobj+' WHERE '+sfield +' '+soperator+':' +data ;
// String company = 'American Banking Corp.';
// String query = 'select id , name from Lead where company = :company';
System.debug('query is '+query);
System.debug(Database.query(query));
tableData = Database.query(query);
System.debug('table data is '+tableData);
}
}
1)This kind of {!a['Name']} is used for SOSL Queries, not for SOQL. Use normal data binding for displaying on UI.
2)Why did you comment this String query = 'select id , name from Lead where company = :company';
---- If it is commented in the code then Database.Query is running on blank data and your query will not run.
3) And also uncomment String company = 'American Banking Corp.' (If it is commented)
Please put {!a.Name} instead of {!a['Name']}.