You need to sign in to do that
Don't have an account?

Dynamic query is not working of multiple Fields its only work on single field
string queryStr1 = 'SELECT ID, NAME, createdBy.Name, createdDate, lastModifiedBy.Name, lastModifiedDate, '+fldAPINameMap.get(selectedField)+' FROM '+selectedObject+' WHERE '+fldAPINameMap.get(selectedField)+' like \'%'+criteria+'%\'';
the above Dynamic query is working perfectly on single select feild but now i am having multiple fields to select then after run query according to selected fields so it is not working
the above Dynamic query is working perfectly on single select feild but now i am having multiple fields to select then after run query according to selected fields so it is not working
VF Page ---------
<apex:outputPanel id="rPanel">
<apex:pageBlockSection title="Results" id="results" rendered="{!queried}">
<apex:pageBlockTable value="{!results}" var="r" id="Opp">
<apex:column value="{!r.id}"/>
<apex:column value="{!r['createdBy.name']}"/>
<apex:column value="{!r['createdDate']}"/>
<apex:column value="{!r['lastModifiedBy.name']}"/>
<apex:column value="{!r['lastModifiedDate']}"/>
<apex:column value="{!r[selectedFieldAPI]}"/>
<apex:column value="{!r[selectedFieldAPI1]}"/>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:outputPanel>
=============Apex Class=======================
Add 1 public variable in class
public String selectedFieldAPI1 {get;set;}
public void btnPerformQuery(){
results=new List<sObject>();
string queryFields = ''+fldAPINameMap.get(selectedField)+','+fldAPINameMap.get(selectedField1)+'';
string queryStr1 = 'SELECT ID, NAME, createdBy.Name, createdDate, lastModifiedBy.Name, lastModifiedDate,'+queryFields+' FROM '+selectedObject+' WHERE '+fldAPINameMap.get(selectedField)+' IN (\'' + String.join( criteria.split( ',' ), '\',\'' ) +'\') limit 999 ';
system.debug('queryStr1-='+queryStr1);
selectedFieldAPI = fldAPINameMap.get(selectedField);
selectedFieldAPI1 = fldAPINameMap.get(selectedField1);
results = database.query(queryStr1);
System.Debug('results-='+results);
queried = true;
system.debug('Query performed, Queried is: '+queried);
}
All Answers
fldAPINameMap.get(selectedField) --> Only One field so you no need to use comma .. its wokring
fldAPINameMap.get(selectedField) --> Mutliple field , you need to comman after each field select ... so its not working
GIve me complete code so that i will look into it
thanks for the quick reply here is code
vf page :-
<apex:page controller="ObjectQueryController2" showHeader="true" tabStyle="Opportunity_Prod_Batch_Record__c" >
<apex:form >
<apex:pageBlock title="Query Objects">
<apex:pageBlockSection columns="1" title="Object Selection">
<apex:selectList title="" size="1" value="{!selectedObject}" >
<apex:selectOptions value="{!objectNames}"/>
<apex:actionSupport event="onchange" reRender="fieldSelection"/>
<apex:outputLabel >Select the object you would like to query</apex:outputLabel>
</apex:selectList>
</apex:pageBlockSection>
<apex:pageBlockSection title="Select fields to query" id="fieldSelection">
<apex:outputPanel >
<table>
<thead></thead>
<tbody>
<tr>
<td>
Select Edition Name:
</td>
<td>
<apex:selectList size="1" value="{!selectedField}">
<apex:selectOptions value="{!objFields}"/>
</apex:selectList>
</td>
</tr>
<tr>
<td>
Select Opportuntiy Id:
</td>
<td>
<apex:selectList size="1" value="{!selectedField}">
<apex:selectOptions value="{!objFields}"/>
</apex:selectList>
</td>
</tr>
<tr>
<td>
Select Opportuntiy Name:
</td>
<td>
<apex:selectList size="1" value="{!selectedField}">
<apex:selectOptions value="{!objFields}"/>
</apex:selectList>
</td>
</tr>
<tr>
<td>
Select Opportuntiy Stage:
</td>
<td>
<apex:selectList size="1" value="{!selectedField}">
<apex:selectOptions value="{!objFields}"/>
</apex:selectList>
</td>
</tr>
<tr>
<td>
Select Banner Name:
</td>
<td>
<apex:selectList size="1" value="{!selectedField}">
<apex:selectOptions value="{!objFields}"/>
</apex:selectList>
</td>
</tr>
<tr>
<td>
Select Stand No:
</td>
<td>
<apex:selectList size="1" value="{!selectedField}">
<apex:selectOptions value="{!objFields}"/>
</apex:selectList>
</td>
</tr>
<!--Repeat for the others-->
</tbody>
</table>
</apex:outputpanel>
</apex:pageBlockSection>
<!-- Button performs a query we have the results and we have results Id. -->
<apex:pageBlockSection title="Search Field to Query">
<apex:inputText value="{!criteria}">
<apex:outputLabel value="Search Edition Name"/>
</apex:inputText>
<apex:commandButton action="{!btnPerformQuery}" reRender="rPanel" value="Query!"/>
</apex:pageBlockSection>
<apex:outputPanel id="rPanel">
<apex:pageBlockSection title="Results" id="results" rendered="{!queried}">
<apex:pageBlockTable value="{!results}" var="r" id="Opp">
<apex:column value="{!r.id}"/>
<apex:column value="{!r['createdBy.name']}"/>
<apex:column value="{!r['createdDate']}"/>
<apex:column value="{!r['lastModifiedBy.name']}"/>
<apex:column value="{!r['lastModifiedDate']}"/>
<apex:column value="{!r[selectedFieldAPI]}"/>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:outputPanel>
</apex:pageBlock>
</apex:form>
</apex:page>
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
controller :-
public with sharing class ObjectQueryController2 {
public List<Contact> conts{get;set;}
public Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
private Map<String, String> fldAPINameMap = new Map<String,String>();
//Private set on SelectOption is so that values can be set from the controller.
public List<SelectOption> objectNames{public get; private set;}
public String selectedObject {get; set;}
public String selectedField {get;set;}
public String selectedFieldAPI {get;set;}
private List<SelectOption> fields;
Public string criteria{get;set;}
public List<sObject> results{get;set;}
public boolean queried {get;set;}
// TO DO:
// - Error handling when user selects ID or Name fields to query (Any Duplicate Fields)
// - Handle queries of fields that are different than text
// - Allow the selection of multiple fields
// - Allow Mass Update of fields on queried records
// - Insert new records
public List<SelectOption> getObjFields() {
fields.clear();
if(queried == null){queried = false;}
system.debug('Page loading, Queried is: '+queried);
// If an object was not selected yet, just populate the text 'Select object first'
if(selectedObject == NULL){
fields.add(new selectOption('Select Object First', 'Select Object First'));
}
// if object was selected, create a list of the object's fields
else {
system.debug('$$$$$' + selectedObject);
//We get a string of the sOjbects using the schemaMap.
Map <String, Schema.SObjectField> fieldMap = schemaMap.get(selectedObject).getDescribe().fields.getMap();
for(Schema.SObjectField sfield : fieldMap.Values())
{
schema.describefieldresult dfield = sfield.getDescribe();
String fieldName = dfield.getLabel();
fields.add(new SelectOption(fieldName, fieldName));
fldAPINameMap.put(fieldName, dfield.getname());
}
}
return fields;
}
// Constructor - this method will run when the controller is instantiated
public ObjectQueryController2(){
objectNames = initObjNames();
fields = new List<SelectOption>();
List<sObject> results = new List<sObject>();
String Criteria;
}
// Populate SelectOption list -
// find all sObjects available in the organization
private List<SelectOption> initObjNames() {
List<SelectOption> objNames = new List<SelectOption>();
List<String> entities = new List<String>(schemaMap.keySet());
entities.sort();
for(String name : entities)
objNames.add(new SelectOption(name,name));
return objNames;
}
//Instantiate the list cast records. We pull id, Name and other needed fields for our pageBlockTable in our VF page
public void btnPerformQuery(){
string queryStr1 = 'SELECT ID, NAME, createdBy.Name, createdDate, lastModifiedBy.Name, lastModifiedDate, '+fldAPINameMap.get(selectedField)+' FROM '+selectedObject+' WHERE '+fldAPINameMap.get(selectedField)+' like \'%'+criteria+'%\'';
selectedFieldAPI = fldAPINameMap.get(selectedField);
system.Debug(queryStr1);
results = database.query(queryStr1);
System.Debug(results.size());
queried = true;
system.debug('Query performed, Queried is: '+queried);
}
}
please chehk this and add your logic as u saying...........
can you please add in my code i am unble to get it pleaase
public String selectedField1 {get;set;}
public String selectedField2 {get;set;}
public String selectedField3 {get;set;}
public String selectedField4 {get;set;}
public String selectedField5 {get;set;}
In Method btnPerformQuery() you have to modify your query
string queryFields = fldAPINameMap.get(selectedField)+','+fldAPINameMap.get(selectedField1)+','fldAPINameMap.get(selectedField2)+','fldAPINameMap.get(selectedField3)+','fldAPINameMap.get(selectedField4)+','fldAPINameMap.get(selectedField5);
string queryStr1 = 'SELECT ID, NAME, createdBy.Name, createdDate, lastModifiedBy.Name, lastModifiedDate, '+queryFields+' FROM '+selectedObject+' WHERE '+fldAPINameMap.get(selectedField)+' like \'%'+criteria+'%\'';
Note ** -- Make sure user will select field other then ID, because LIKE operators can not be used on Id fields
==============================================================================================
<table>
<thead></thead>
<tbody>
<tr>
<td>
Select Edition Name:
</td>
<td>
<apex:selectList size="1" value="{!selectedField}">
<apex:selectOptions value="{!objFields}"/>
</apex:selectList>
</td>
</tr>
<tr>
<td>
Select Opportuntiy Id:
</td>
<td>
<apex:selectList size="1" value="{!selectedField1}">
<apex:selectOptions value="{!objFields}"/>
</apex:selectList>
</td>
</tr>
<tr>
<td>
Select Opportuntiy Name:
</td>
<td>
<apex:selectList size="1" value="{!selectedField2}">
<apex:selectOptions value="{!objFields}"/>
</apex:selectList>
</td>
</tr>
<tr>
<td>
Select Opportuntiy Stage:
</td>
<td>
<apex:selectList size="1" value="{!selectedField3}">
<apex:selectOptions value="{!objFields}"/>
</apex:selectList>
</td>
</tr>
<tr>
<td>
Select Banner Name:
</td>
<td>
<apex:selectList size="1" value="{!selectedField4}">
<apex:selectOptions value="{!objFields}"/>
</apex:selectList>
</td>
</tr>
<tr>
<td>
Select Stand No:
</td>
<td>
<apex:selectList size="1" value="{!selectedField5}">
<apex:selectOptions value="{!objFields}"/>
</apex:selectList>
</td>
</tr>
<!--Repeat for the others-->
</tbody>
</table>
I added your logic in my code but still facing an error while running the dynamic query
vf page:-
<apex:page controller="ObjectQueryController5" showHeader="true" tabStyle="Opportunity_Prod_Batch_Record__c" >
<apex:form >
<apex:pageBlock title="Query Objects">
<apex:pageBlockSection columns="1" title="Object Selection">
<apex:selectList title="" size="1" value="{!selectedObject}" >
<apex:selectOptions value="{!objectNames}"/>
<apex:actionSupport event="onchange" reRender="fieldSelection"/>
<apex:outputLabel >Select the object you would like to query</apex:outputLabel>
</apex:selectList>
</apex:pageBlockSection>
<apex:pageBlockSection title="Select fields to query" id="fieldSelection">
<apex:outputPanel >
<table>
<thead></thead>
<tbody>
<tr>
<td>
Select Edition Name:
</td>
<td>
<apex:selectList size="1" value="{!selectedField}">
<apex:selectOptions value="{!objFields}"/>
</apex:selectList>
</td>
</tr>
<tr>
<td>
Select Opportuntiy Id:
</td>
<td>
<apex:selectList size="1" value="{!selectedField1}">
<apex:selectOptions value="{!objFields}"/>
</apex:selectList>
</td>
</tr>
</tbody>
</table>
</apex:outputpanel>
</apex:pageBlockSection>
<!-- Button performs a query we have the results and we have results Id. -->
<apex:pageBlockSection title="Search Field to Query">
<apex:inputText value="{!criteria}">
<apex:outputLabel value="Search Edition Name"/>
</apex:inputText>
<apex:commandButton action="{!btnPerformQuery}" reRender="rPanel" value="Query!"/>
</apex:pageBlockSection>
<apex:outputPanel id="rPanel">
<apex:pageBlockSection title="Results" id="results" rendered="{!queried}">
<apex:pageBlockTable value="{!results}" var="r" id="Opp">
<apex:column value="{!r.id}"/>
<apex:column value="{!r['createdBy.name']}"/>
<apex:column value="{!r['createdDate']}"/>
<apex:column value="{!r['lastModifiedBy.name']}"/>
<apex:column value="{!r['lastModifiedDate']}"/>
<apex:column value="{!r[selectedFieldAPI]}"/>
<apex:column value="{!r[selectedField1]}"/>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:outputPanel>
</apex:pageBlock>
</apex:form>
</apex:page>
COntrolller :-
public with sharing class ObjectQueryController5 {
public List<Contact> conts{get;set;}
public Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
private Map<String, String> fldAPINameMap = new Map<String,String>();
// private Map<String, String> fldAPINameMap1 = new Map<String,String>();
//Private set on SelectOption is so that values can be set from the controller.
public List<SelectOption> objectNames{public get; private set;}
public String selectedObject {get; set;}
public String selectedField {get;set;}
public String selectedField1 {get;set;}
public String selectedFieldAPI {get;set;}
private List<SelectOption> fields;
Public string criteria{get;set;}
public List<sObject> results{get;set;}
public boolean queried {get;set;}
// TO DO:
// - Error handling when user selects ID or Name fields to query (Any Duplicate Fields)
// - Handle queries of fields that are different than text
// - Allow the selection of multiple fields
// - Allow Mass Update of fields on queried records
// - Insert new records
public List<SelectOption> getObjFields() {
fields.clear();
if(queried == null){queried = false;}
system.debug('Page loading, Queried is: '+queried);
// If an object was not selected yet, just populate the text 'Select object first'
if(selectedObject == NULL){
fields.add(new selectOption('Select Object First', 'Select Object First'));
}
// if object was selected, create a list of the object's fields
else {
system.debug('$$$$$' + selectedObject);
//We get a string of the sOjbects using the schemaMap.
Map <String, Schema.SObjectField> fieldMap = schemaMap.get(selectedObject).getDescribe().fields.getMap();
for(Schema.SObjectField sfield : fieldMap.Values())
{
schema.describefieldresult dfield = sfield.getDescribe();
String fieldName = dfield.getLabel();
fields.add(new SelectOption(fieldName, fieldName));
fldAPINameMap.put(fieldName, dfield.getname());
}
}
return fields;
}
// Constructor - this method will run when the controller is instantiated
public ObjectQueryController5(){
objectNames = initObjNames();
fields = new List<SelectOption>();
List<sObject> results = new List<sObject>();
String Criteria;
}
// Populate SelectOption list -
// find all sObjects available in the organization
private List<SelectOption> initObjNames() {
List<SelectOption> objNames = new List<SelectOption>();
List<String> entities = new List<String>(schemaMap.keySet());
entities.sort();
for(String name : entities)
objNames.add(new SelectOption(name,name));
return objNames;
}
//Instantiate the list cast records. We pull id, Name and other needed fields for our pageBlockTable in our VF page
public void btnPerformQuery(){
string queryFields = ''+fldAPINameMap.get(selectedField)+','+fldAPINameMap.get(selectedField1)+'';
string queryStr1 = 'SELECT ID, NAME, createdBy.Name, createdDate, lastModifiedBy.Name, lastModifiedDate, '+queryFields+' FROM '+selectedObject+' WHERE '+fldAPINameMap.get(selectedField)+' IN (\'' + String.join( criteria.split( ',' ), '\',\'' ) +'\') limit 999 ';
selectedFieldAPI = fldAPINameMap.get(selectedField);
system.Debug(queryStr1);
results = database.query(queryStr1);
System.Debug(results.size());
queried = true;
system.debug('Query performed, Queried is: '+queried);
}
}
please see this where did i did wrong......please see this
string queryStr1 = 'SELECT ID, NAME, createdBy.Name, createdDate, lastModifiedBy.Name, lastModifiedDate, '+queryFields+' FROM '+selectedObject+' WHERE '+fldAPINameMap.get(selectedField)+' IN (\'' + String.join( criteria.split( ',' ), '\',\'' ) +'\') limit 999 ';
Since you are already paasing Name and ID field in your dynamic query , you can not select Name or ID field again from PickList. If you want to give User's option to select Name field from Picklist , you have to remove Name from your dynamic query .
Make sure user select fields other then ID, because LIKE operators can not be used on Id fields
i removed id and name and search found this errror now
from this type i searched but found above error
13:39:59:060 USER_DEBUG [80]|DEBUG|queryStr1-=SELECT ID, NAME, createdBy.Name, createdDate, lastModifiedBy.Name, lastModifiedDate, Industry,BillingCity FROM account WHERE Industry IN ('Oil & Gas') limit 999
========================================Apex class ---================
public with sharing class ObjectQueryController2 {
public List<Contact> conts{get;set;}
public Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
private Map<String, String> fldAPINameMap = new Map<String,String>();
// private Map<String, String> fldAPINameMap1 = new Map<String,String>();
//Private set on SelectOption is so that values can be set from the controller.
public List<SelectOption> objectNames{public get; private set;}
public String selectedObject {get; set;}
public String selectedField {get;set;}
public String selectedField1 {get;set;}
public String selectedFieldAPI {get;set;}
private List<SelectOption> fields;
Public string criteria{get;set;}
public List<sObject> results{get;set;}
public boolean queried {get;set;}
// TO DO:
// - Error handling when user selects ID or Name fields to query (Any Duplicate Fields)
// - Handle queries of fields that are different than text
// - Allow the selection of multiple fields
// - Allow Mass Update of fields on queried records
// - Insert new records
public List<SelectOption> getObjFields() {
fields.clear();
if(queried == null){queried = false;}
system.debug('Page loading, Queried is: '+queried);
// If an object was not selected yet, just populate the text 'Select object first'
if(selectedObject == NULL){
fields.add(new selectOption('Select Object First', 'Select Object First'));
}
// if object was selected, create a list of the object's fields
else {
system.debug('$$$$$' + selectedObject);
//We get a string of the sOjbects using the schemaMap.
Map <String, Schema.SObjectField> fieldMap = schemaMap.get(selectedObject).getDescribe().fields.getMap();
for(Schema.SObjectField sfield : fieldMap.Values())
{
schema.describefieldresult dfield = sfield.getDescribe();
String fieldName = dfield.getLabel();
fields.add(new SelectOption(fieldName, fieldName));
fldAPINameMap.put(fieldName, dfield.getname());
}
}
return fields;
}
// Constructor - this method will run when the controller is instantiated
public ObjectQueryController2(){
objectNames = initObjNames();
fields = new List<SelectOption>();
List<sObject> results = new List<sObject>();
String Criteria;
}
// Populate SelectOption list -
// find all sObjects available in the organization
private List<SelectOption> initObjNames() {
List<SelectOption> objNames = new List<SelectOption>();
List<String> entities = new List<String>(schemaMap.keySet());
entities.sort();
for(String name : entities)
objNames.add(new SelectOption(name,name));
return objNames;
}
//Instantiate the list cast records. We pull id, Name and other needed fields for our pageBlockTable in our VF page
public void btnPerformQuery(){
string queryFields = ''+fldAPINameMap.get(selectedField)+','+fldAPINameMap.get(selectedField1)+'';
string queryStr1 = 'SELECT ID, NAME, createdBy.Name, createdDate, lastModifiedBy.Name, lastModifiedDate, '+queryFields+' FROM '+selectedObject+' WHERE '+fldAPINameMap.get(selectedField)+' IN (\'' + String.join( criteria.split( ',' ), '\',\'' ) +'\') limit 999 ';
system.debug('queryStr1-='+queryStr1);
selectedFieldAPI = fldAPINameMap.get(selectedField);
system.Debug(queryStr1);
results = database.query(queryStr1);
System.Debug(results.size());
queried = true;
system.debug('Query performed, Queried is: '+queried);
}
}
==================VF Page --====================
<apex:page controller="ObjectQueryController2" showHeader="true">
<apex:form >
<apex:pageBlock title="Query Objects">
<apex:pageBlockSection columns="1" title="Object Selection">
<apex:selectList title="" size="1" value="{!selectedObject}" >
<apex:selectOptions value="{!objectNames}"/>
<apex:actionSupport event="onchange" reRender="fieldSelection"/>
<apex:outputLabel >Select the object you would like to query</apex:outputLabel>
</apex:selectList>
</apex:pageBlockSection>
<apex:pageBlockSection title="Select fields to query" id="fieldSelection">
<apex:outputPanel >
<table>
<thead></thead>
<tbody>
<tr>
<td>
Select Edition Name:
</td>
<td>
<apex:selectList size="1" value="{!selectedField}">
<apex:selectOptions value="{!objFields}"/>
</apex:selectList>
</td>
</tr>
<tr>
<td>
Select Opportuntiy Id:
</td>
<td>
<apex:selectList size="1" value="{!selectedField1}">
<apex:selectOptions value="{!objFields}"/>
</apex:selectList>
</td>
</tr>
</tbody>
</table>
</apex:outputpanel>
</apex:pageBlockSection>
<!-- Button performs a query we have the results and we have results Id. -->
<apex:pageBlockSection title="Search Field to Query">
<apex:inputText value="{!criteria}">
<apex:outputLabel value="Search Edition Name"/>
</apex:inputText>
<apex:commandButton action="{!btnPerformQuery}" reRender="rPanel" value="Query!"/>
</apex:pageBlockSection>
<apex:outputPanel id="rPanel">
<apex:pageBlockSection title="Results" id="results" rendered="{!queried}">
<apex:pageBlockTable value="{!results}" var="r" id="Opp">
<apex:column value="{!r.id}"/>
<apex:column value="{!r['createdBy.name']}"/>
<apex:column value="{!r['createdDate']}"/>
<apex:column value="{!r['lastModifiedBy.name']}"/>
<apex:column value="{!r['lastModifiedDate']}"/>
<apex:column value="{!r[selectedFieldAPI]}"/>
<apex:column value="{!r[selectedField1]}"/>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:outputPanel>
</apex:pageBlock>
</apex:form>
</apex:page>
Visualforce Error
Help for this Page
Exception: Invalid field P Stage for SObject OpportunityLineItem
like example i am having two select field
edition name :- P Edition
stage :- P stage
according to first select field i.e Edtion name i selected P edtion field in that having Adipec 2018 values i put the value Adipec 2018 in search box and click Query button then after it is showing an above error
Error :-
Visualforce Error
Help for this Page
Exception: Invalid field P Stage for SObject OpportunityLineItem
like example i selected Opportuntiy line item object having custom fields and i am having two select field
edition name :- P Edition(1st selectd field)
stage :- P stage(2nd selectd field)
according to first select field i.e Edtion name i selected P edtion field in that having Adipec 2018 values i put the value Adipec 2018 in search box and click Query button then after it is showing an above error
u der ???
i
Happy new year
sir i chked in debug log queery working perfectly accroding to selelected field but dont know why tthat error occurs
Can you send , query from debug log and error which you are getting . Code you have written is correct , their must be some other issue that causing this error .
|USER_DEBUG|[83]|DEBUG|queryStr1-=SELECT createdBy.Name, createdDate, lastModifiedBy.Name, lastModifiedDate, Industry,Rating FROM account WHERE Industry IN ('oil & gas') limit 999
12:13:58.0 (281410240)|VARIABLE_ASSIGNMENT|[81]|queryFields|"Industry,Rating"
<apex:pageBlockSection title="Results" id="results" rendered="{!queried}">
<apex:pageBlockTable value="{!results}" var="r" id="Opp">
<apex:column value="{!r.id}"/>
<apex:column value="{!r['createdBy.name']}"/>
<apex:column value="{!r['createdDate']}"/>
<apex:column value="{!r['lastModifiedBy.name']}"/>
<apex:column value="{!r['lastModifiedDate']}"/>
<apex:column value="{!r[selectedFieldAPI]}"/>
<apex:column value="{!r[selectedField1]}"/> <--------------------- on this field we call on vf page on that time it is shwing an error like
Visualforce Error
Help for this Page
Exception: Invalid field Account Rating for SObject Account
</apex:pageBlockTable>
results = database.query(queryStr1);
is not returning Rating field .
This field is present in Query string but not returning from Database.query() method .
when i run like this
i mean first field Banner name
2nd field Industry
and search according 1st field banner name it is working
but below scrren shot i swap the fields like
1st field Industry
2nd field Banner name
and searching according 1st field Industry first field like values oil & gas then it is not working same error occurs
see this scrren shot
Error while run according this
14:38:20.719 (3722415562)|VARIABLE_ASSIGNMENT|[18]|this.results|"List of size 999 too large to display"|0x1a231f13
did u think this will be imapcting on results ??( What ever line i bold see)
https://developer.salesforce.com/docs/atlas.en-us.salesforce_app_limits_cheatsheet.meta/salesforce_app_limits_cheatsheet/salesforce_app_limits_platform_vf.htm
string queryStr1 = 'SELECT createdBy.Name, createdDate, lastModifiedBy.Name, lastModifiedDate, '+queryFields+' FROM '+selectedObject+' WHERE '+fldAPINameMap.get(selectedField)+' IN (\'' + String.join( criteria.split( ',' ), '\',\'' ) +'\') limit 9';
system.debug('queryStr1-='+queryStr1);
VF Page ---------
<apex:outputPanel id="rPanel">
<apex:pageBlockSection title="Results" id="results" rendered="{!queried}">
<apex:pageBlockTable value="{!results}" var="r" id="Opp">
<apex:column value="{!r.id}"/>
<apex:column value="{!r['createdBy.name']}"/>
<apex:column value="{!r['createdDate']}"/>
<apex:column value="{!r['lastModifiedBy.name']}"/>
<apex:column value="{!r['lastModifiedDate']}"/>
<apex:column value="{!r[selectedFieldAPI]}"/>
<apex:column value="{!r[selectedFieldAPI1]}"/>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:outputPanel>
=============Apex Class=======================
Add 1 public variable in class
public String selectedFieldAPI1 {get;set;}
public void btnPerformQuery(){
results=new List<sObject>();
string queryFields = ''+fldAPINameMap.get(selectedField)+','+fldAPINameMap.get(selectedField1)+'';
string queryStr1 = 'SELECT ID, NAME, createdBy.Name, createdDate, lastModifiedBy.Name, lastModifiedDate,'+queryFields+' FROM '+selectedObject+' WHERE '+fldAPINameMap.get(selectedField)+' IN (\'' + String.join( criteria.split( ',' ), '\',\'' ) +'\') limit 999 ';
system.debug('queryStr1-='+queryStr1);
selectedFieldAPI = fldAPINameMap.get(selectedField);
selectedFieldAPI1 = fldAPINameMap.get(selectedField1);
results = database.query(queryStr1);
System.Debug('results-='+results);
queried = true;
system.debug('Query performed, Queried is: '+queried);
}
selectedFieldAPI = fldAPINameMap.get(selectedField);
selectedFieldAPI1 = fldAPINameMap.get(selectedField1);
selectedFieldAPI2 = fldAPINameMap.get(selectedField2);
selectedFieldAPI3 = fldAPINameMap.get(selectedField3);
selectedFieldAPI4 = fldAPINameMap.get(selectedField4);
selectedFieldAPI5 = fldAPINameMap.get(selectedField5);
and call in vf like this
<apex:pageBlockTable value="{!results}" var="r" >
<apex:column value="{!r.id}"/>
<apex:column value="{!r['createdBy.name']}"/>
<apex:column value="{!r['createdDate']}"/>
<apex:column value="{!r['lastModifiedBy.name']}"/>
<apex:column value="{!r['lastModifiedDate']}"/>
<apex:column value="{!r[selectedFieldAPI]}"/>
<apex:column value="{!r[selectedFieldAPI1]}"/>
<apex:column value="{!r[selectedFieldAPI2]}"/>
<apex:column value="{!r[selectedFieldAPI3]}"/>
<apex:column value="{!r[selectedFieldAPI4]}"/>
<apex:column value="{!r[selectedFieldAPI5]}"/>
</apex:pageBlockTable>
suparb it is working thanks a lot sir see this scrren shot.....
You can mark this is best answer , if its working fine .
you can reach out to me at "manav.063057@gmail.com"
Sir dynamic query displaying records according to all selected field see this
but now when i changed the object then selected field label shuld be dispaly label according to selected object can it is possible see this scrren shot on lead
i need to display selected field label according to selected object under that selected field
can it is possible ???
<table>
<thead></thead>
<tbody>
<tr>
<td>
{!objFieldLabelName}:
</td>
<td>
<apex:selectList size="1" value="{!selectedField}">
<apex:selectOptions value="{!objFields}"/>
</apex:selectList>
</td>
</tr>
<tr>
<td>
{!objFieldLabelType}:
</td>
<td>
<apex:selectList size="1" value="{!selectedField1}">
<apex:selectOptions value="{!objFields}"/>
</apex:selectList>
</td>
</tr>
</tbody>
</table>
================Apex Class ===========================
public with sharing class ObjectQueryController2 {
public List<Contact> conts{get;set;}
public Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
private Map<String, String> fldAPINameMap = new Map<String,String>();
// private Map<String, String> fldAPINameMap1 = new Map<String,String>();
//Private set on SelectOption is so that values can be set from the controller.
public List<SelectOption> objectNames{public get; private set;}
public String selectedObject {get; set;}
public String selectedField {get;set;}
public String selectedField1 {get;set;}
public String selectedFieldAPI {get;set;}
public String selectedFieldAPI1 {get;set;}
public String objFieldLabelName {get;set;}
public String objFieldLabelType {get;set;}
private List<SelectOption> fields;
Public string criteria{get;set;}
public List<sObject> results{get;set;}
public boolean queried {get;set;}
// TO DO:
// - Error handling when user selects ID or Name fields to query (Any Duplicate Fields)
// - Handle queries of fields that are different than text
// - Allow the selection of multiple fields
// - Allow Mass Update of fields on queried records
// - Insert new records
public List<SelectOption> getObjFields() {
fields.clear();
if(queried == null){queried = false;}
system.debug('Page loading, Queried is: '+queried);
// If an object was not selected yet, just populate the text 'Select object first'
if(selectedObject == NULL){
fields.add(new selectOption('Select Object First', 'Select Object First'));
}
// if object was selected, create a list of the object's fields
else {
system.debug('$$$$$' + selectedObject);
//We get a string of the sOjbects using the schemaMap.
Map <String, Schema.SObjectField> fieldMap = schemaMap.get(selectedObject).getDescribe().fields.getMap();
for(Schema.SObjectField sfield : fieldMap.Values())
{
schema.describefieldresult dfield = sfield.getDescribe();
String fieldName = dfield.getName();
if(selectedObject.endsWithIgnoreCase('Account')){
if(fieldName =='Industry'){
objFieldLabelName = fieldName ;
}
if(fieldName =='Type'){
objFieldLabelType = fieldName ;
}
}
if(selectedObject.endsWithIgnoreCase('Case')){
// add field for case object
}
system.debug('fieldName-='+fieldName );
fields.add(new SelectOption(fieldName, fieldName));
fldAPINameMap.put(fieldName, dfield.getname());
}
}
return fields;
}
// Constructor - this method will run when the controller is instantiated
public ObjectQueryController2(){
objectNames = initObjNames();
fields = new List<SelectOption>();
// List<sObject> results = new List<sObject>();
String Criteria;
}
// Populate SelectOption list -
// find all sObjects available in the organization
private List<SelectOption> initObjNames() {
List<SelectOption> objNames = new List<SelectOption>();
List<String> entities = new List<String>(schemaMap.keySet());
entities.sort();
for(String name : entities)
objNames.add(new SelectOption(name,name));
return objNames;
}
//Instantiate the list cast records. We pull id, Name and other needed fields for our pageBlockTable in our VF page
public void btnPerformQuery(){
results=new List<sObject>();
string queryFields = ''+fldAPINameMap.get(selectedField)+','+fldAPINameMap.get(selectedField1)+'';
string queryStr1 = 'SELECT ID, NAME, createdBy.Name, createdDate, lastModifiedBy.Name, lastModifiedDate,'+queryFields+' FROM '+selectedObject+' WHERE '+fldAPINameMap.get(selectedField)+' IN (\'' + String.join( criteria.split( ',' ), '\',\'' ) +'\') limit 999 ';
system.debug('queryStr1-='+queryStr1);
selectedFieldAPI = fldAPINameMap.get(selectedField);
selectedFieldAPI1 = fldAPINameMap.get(selectedField1);
results = database.query(queryStr1);
System.Debug('results-='+results);
queried = true;
system.debug('Query performed, Queried is: '+queried);
}
}
it is not working properly i am selected Lead object and in 1st picklist City but label showing Industry
and 2nd picklist Company but label showing Type
then fild label (SHow) :- 1st selected field (Show)
field lable (City) :- 2nd slected field (City)
field label (Agent) :- 3rd selectd field (Agent)
or 2nd example on i Sekected Account Object:-
then fild label (Industry) :- 1st selected field (Industry)
field lable (Account Rating) :- 2nd slected field (Account Rating)
field label (Company Type) :- 3rd selectd field (Company Type)
like this i need to show dynamically changed field label on selcted object on that sleected field
<apex:page controller="ObjectQueryController2" showHeader="true">
<apex:form >
<apex:pageBlock title="Query Objects">
<apex:pageBlockSection columns="1" title="Object Selection">
<apex:selectList title="" size="1" value="{!selectedObject}" >
<apex:selectOptions value="{!objectNames}"/>
<apex:actionSupport event="onchange" reRender="fieldSelection"/>
<apex:outputLabel >Select the object you would like to query</apex:outputLabel>
</apex:selectList>
</apex:pageBlockSection>
<apex:pageBlockSection title="Select fields to query" id="fieldSelection">
<apex:outputPanel >
<table>
<thead></thead>
<tbody>
<tr>
<td>
{!objFieldLabelName}:
</td>
<td>
<apex:selectList size="1" value="{!selectedField}">
<apex:actionSupport event="onchange" action="{!setFieldLabel}" reRender="fieldSelection"/>
<apex:selectOptions value="{!objFields}" />
</apex:selectList>
</td>
</tr>
<tr>
<td>
{!objFieldLabelType}:
</td>
<td>
<apex:selectList size="1" value="{!selectedField1}">
<apex:actionSupport event="onchange" action="{!setFieldLabel}" reRender="fieldSelection"/>
<apex:selectOptions value="{!objFields}"/>
</apex:selectList>
</td>
</tr>
</tbody>
</table>
</apex:outputpanel>
</apex:pageBlockSection>
<!-- Button performs a query we have the results and we have results Id. -->
<apex:pageBlockSection title="Search Field to Query">
<apex:inputText value="{!criteria}">
<apex:outputLabel value="Search Edition Name"/>
</apex:inputText>
<apex:commandButton action="{!btnPerformQuery}" reRender="rPanel" value="Query!"/>
</apex:pageBlockSection>
<apex:outputPanel id="rPanel">
<apex:pageBlockSection title="Results" id="results" rendered="{!queried}">
<apex:pageBlockTable value="{!results}" var="r" id="Opp">
<apex:column value="{!r.id}"/>
<apex:column value="{!r['createdBy.name']}"/>
<apex:column value="{!r['createdDate']}"/>
<apex:column value="{!r['lastModifiedBy.name']}"/>
<apex:column value="{!r['lastModifiedDate']}"/>
<apex:column value="{!r[selectedFieldAPI]}"/>
<apex:column value="{!r[selectedFieldAPI1]}"/>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:outputPanel>
</apex:pageBlock>
</apex:form>
</apex:page>
====================APex controller ===============
public with sharing class ObjectQueryController2 {
public List<Contact> conts{get;set;}
public Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
private Map<String, String> fldAPINameMap = new Map<String,String>();
// private Map<String, String> fldAPINameMap1 = new Map<String,String>();
//Private set on SelectOption is so that values can be set from the controller.
public List<SelectOption> objectNames{public get; private set;}
public String selectedObject {get; set;}
public String selectedField {get;set;}
public String selectedField1 {get;set;}
public String selectedFieldAPI {get;set;}
public String selectedFieldAPI1 {get;set;}
public String objFieldLabelName {get;set;}
public String objFieldLabelType {get;set;}
private List<SelectOption> fields;
Public string criteria{get;set;}
public List<sObject> results{get;set;}
public boolean queried {get;set;}
// TO DO:
// - Error handling when user selects ID or Name fields to query (Any Duplicate Fields)
// - Handle queries of fields that are different than text
// - Allow the selection of multiple fields
// - Allow Mass Update of fields on queried records
// - Insert new records
public List<SelectOption> getObjFields() {
fields.clear();
if(queried == null){queried = false;}
system.debug('Page loading, Queried is: '+queried);
// If an object was not selected yet, just populate the text 'Select object first'
if(selectedObject == NULL){
fields.add(new selectOption('Select Object First', 'Select Object First'));
}
// if object was selected, create a list of the object's fields
else {
system.debug('$$$$$' + selectedObject);
//We get a string of the sOjbects using the schemaMap.
Map <String, Schema.SObjectField> fieldMap = schemaMap.get(selectedObject).getDescribe().fields.getMap();
for(Schema.SObjectField sfield : fieldMap.Values())
{
schema.describefieldresult dfield = sfield.getDescribe();
String fieldName = dfield.getName();
fields.add(new SelectOption(fieldName, fieldName));
fldAPINameMap.put(fieldName, dfield.getname());
}
}
return fields;
}
// Constructor - this method will run when the controller is instantiated
public ObjectQueryController2(){
objectNames = initObjNames();
fields = new List<SelectOption>();
// List<sObject> results = new List<sObject>();
String Criteria;
}
// Populate SelectOption list -
// find all sObjects available in the organization
private List<SelectOption> initObjNames() {
List<SelectOption> objNames = new List<SelectOption>();
List<String> entities = new List<String>(schemaMap.keySet());
entities.sort();
for(String name : entities)
objNames.add(new SelectOption(name,name));
return objNames;
}
//Instantiate the list cast records. We pull id, Name and other needed fields for our pageBlockTable in our VF page
public void btnPerformQuery(){
results=new List<sObject>();
string queryFields = ''+fldAPINameMap.get(selectedField)+','+fldAPINameMap.get(selectedField1)+'';
string queryStr1 = 'SELECT ID, NAME, createdBy.Name, createdDate, lastModifiedBy.Name, lastModifiedDate,'+queryFields+' FROM '+selectedObject+' WHERE '+fldAPINameMap.get(selectedField)+' IN (\'' + String.join( criteria.split( ',' ), '\',\'' ) +'\') limit 999 ';
system.debug('queryStr1-='+queryStr1);
selectedFieldAPI = fldAPINameMap.get(selectedField);
selectedFieldAPI1 = fldAPINameMap.get(selectedField1);
results = database.query(queryStr1);
System.Debug('results-='+results);
queried = true;
system.debug('Query performed, Queried is: '+queried);
}
public void setFieldLabel(){
system.debug('selectedObject-='+selectedObject);
system.debug('selectedField-='+selectedField);
Map <String, Schema.SObjectField> fieldMap = schemaMap.get(selectedObject).getDescribe().fields.getMap();
for(Schema.SObjectField sfield : fieldMap.Values())
{
schema.describefieldresult dfield = sfield.getDescribe();
String fieldName = dfield.getName();
if(fieldName==selectedField){
objFieldLabelName = dfield.getLabel();
}
if(fieldName==selectedField1){
objFieldLabelType = dfield.getLabel();
}
}
}
}
sir it is working pefctly according to field select
but when we select fiild in that they are showing api name of the fields but i need labels of the fields
see this scrren shot showing API names of the fields
fields.clear();
if(queried == null){queried = false;}
system.debug('Page loading, Queried is: '+queried);
// If an object was not selected yet, just populate the text 'Select object first'
if(selectedObject == NULL){
fields.add(new selectOption('Select Object First', 'Select Object First'));
}
// if object was selected, create a list of the object's fields
else {
system.debug('$$$$$' + selectedObject);
//We get a string of the sOjbects using the schemaMap.
Map <String, Schema.SObjectField> fieldMap = schemaMap.get(selectedObject).getDescribe().fields.getMap();
for(Schema.SObjectField sfield : fieldMap.Values())
{
schema.describefieldresult dfield = sfield.getDescribe();
String fieldName = dfield.getName();
fields.add(new SelectOption(fieldName, dfield.getLabel()));
fldAPINameMap.put(fieldName, dfield.getname());
}
}
return fields;
}
@Manvendra Chaturvedi 26
Sir i have question can we do mapping with other object and past the data into other object whatever we records call on our vf page we need to store this records into the other object by simply cliking on button???
I have a reverse question where I have a map with Key as Name of properties of Object with values and I want to iterate over collection and assign value to relevant property i.e.
I have a custom object with 4 properties, First, Second, Third and Forth
Now I also have a Map<string, string> Values which has data as
<First, 10>
<Second,20>
<Third, 30>
<Forth, 40>
Now I want a way to iterate over all the Keys in MAP and assign the value of corrosponding property of the object from value from MAP.
for(string keyValue : Map.GetKeys())
{
Object.Proeprty_Name = Map.Get(keyValue);
}
I also have another post for same.