You need to sign in to do that
Don't have an account?
PRIYAN NEERUDU
priya
i want to retrieve all the objects as dropdownlist and after selecting one object i want to display all the mandatory field of that object with its data type and search that particular object records based on mandatory field selected and display in my vf page
HI Priya,
Refer getDescribe calls in the following link:
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_dynamic_vf_globals_objecttype.htm?search_text=metadata
Thanks,
Hari
<apex:form > <apex:pageBlock >
<apex:pageBlockSection columns="4">
<apex:pageBlockSectionItem >
<apex:outputlabel value="Object Names :"/>
<apex:actionRegion > <apex:selectList value="{!selectedObject}" size="1">
<apex:selectOptions value="{!ObjectNames}"/>
<apex:actionSupport event="onchange" rerender="myFields"/>
</apex:selectList> </apex:actionRegion> </apex:pageBlockSectionItem>
<apex:pageBlockSectionItem > <apex:outputlabel value="Field Names :"/>
<apex:outputPanel id="myFields"> <apex:actionRegion >
<apex:selectList value="{!selectedField}" size="1">
<apex:selectOptions value="{!ObjectFields}"/>
</apex:selectList> </apex:actionRegion>
</apex:outputPanel> </apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<input id="name" placeholder="label" style="margin-left:10px;" />
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:commandButton onclick="callSearch();" value="Search" reRender="x" style="margin-left:10px;" />
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
<apex:pageblockTable value="{!lstQuery}" var="eachRecord">
<apex:column value="{!eachRecord.id}"/>
<apex:column value="{!eachRecord['Name']}"/>
</apex:pageblockTable>
</apex:pageBlock>
<apex:actionFunction name="callActFun" action="{!searchRecords}" reRender="op">
<apex:param name="a" value="" />
</apex:actionFunction>
</apex:form>
</apex:page>
----------------------------------------------------------------------------------------------------------
public class objectController1
{
String queryString;
public void searchrecords() {
String name= ApexPages.CurrentPage().getParameters().get('a');
//string staging = rfq.Name;
queryString = 'SELECT Id, LastName, FirstName';
if (selectedObject!= null && selectedObject!= '') {
queryString += ' from \' + selectedObject+ \'';
}
if (selectedField != null && selectedField != '') {
queryString += ' where \' + selectedField + \'';
system.debug('queryString -----'+queryString );
}
if (name != null && name!= '') {
queryString += ' = \'' + name+ '\'';
}
system.debug('queryString -----------'+queryString );
lstQuery= database.query(queryString);
}
public Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
public String selectedObject {get; set;}
public list<sobject >lstQuery{get; set;}
public String selectedField {get; set;}
Public objectController1()
{
selectedObject = 'account';
}
public List<SelectOption> getObjectNames()
{
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;
}
public List<SelectOption> getObjectFields()
{
Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
Schema.SObjectType ObjectSchema = schemaMap.get(selectedObject);
Map<String, Schema.SObjectField> fieldMap = ObjectSchema.getDescribe().fields.getMap();
List<SelectOption> fieldNames = new List<SelectOption>();
for (String fieldName: fieldMap.keySet())
{
fieldNames.add(new SelectOption(fieldName,fieldName));
//fieldMap.get(fieldName).getDescribe().getLabel();//It provides to get the object fields label.
}
return fieldNames;
}
}
i actually want to serach the object records based on field name provided given in input label as a serch functioanlity but m unable to do using above code can u plz rectify .
<apex:page controller="objectController1" setup="false" showHeader="false" sidebar="false">
<apex:form > <apex:pageBlock >
<apex:pageBlockSection columns="4">
<apex:pageBlockSectionItem >
<apex:outputlabel value="Object Names :"/>
<apex:actionRegion > <apex:selectList value="{!selectedObject}" size="1">
<apex:selectOptions value="{!ObjectNames}"/>
<apex:actionSupport event="onchange" rerender="myFields"/>
</apex:selectList> </apex:actionRegion> </apex:pageBlockSectionItem>
<apex:pageBlockSectionItem > <apex:outputlabel value="Field Names :"/>
<apex:outputPanel id="myFields"> <apex:actionRegion >
<apex:selectList value="{!selectedField}" size="1">
<apex:selectOptions value="{!ObjectFields}"/>
</apex:selectList> </apex:actionRegion>
</apex:outputPanel> </apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<input id="name" placeholder="label" style="margin-left:10px;" />
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:commandButton onclick="callSearch();" value="Search" reRender="x" style="margin-left:10px;" />
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
<apex:pageblockTable value="{!lstQuery}" var="eachRecord" id="list">
<apex:column value="{!eachRecord.id}"/>
<apex:column value="{!eachRecord['Name']}"/>
</apex:pageblockTable>
</apex:pageBlock>
<apex:actionFunction name="callSearch" action="{!searchRecords}" reRender="list">
<apex:param name="a" value="" assignTo="{!searchTxt}"/>
</apex:actionFunction>
</apex:form>
</apex:page>
---controller---
public class objectController1
{
string searchTxt {get; set;}
String queryString;
public void searchrecords() {
system.debug('selectedObject ---selectedField--------'+selectedObject+'---'+selectedField );
//string staging = rfq.Name;
queryString = 'SELECT Id, name';
if (selectedObject!= null && selectedObject!= '') {
queryString += ' from '+ selectedObject+'';
}
if (searchTxt != null && searchTxt!= '') {
if (selectedField != null && selectedField != '') {
queryString += ' where ' + selectedField +' = '+searchTxt;
system.debug('queryString -----'+queryString );
}
}
system.debug('queryString -----------'+queryString );
lstQuery= database.query(queryString);
}
public Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
public String selectedObject {get; set;}
public list<sobject >lstQuery{get; set;}
public String selectedField {get; set;}
Public objectController1()
{
selectedObject = 'account';
}
public List<SelectOption> getObjectNames()
{
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;
}
public List<SelectOption> getObjectFields()
{
Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
Schema.SObjectType ObjectSchema = schemaMap.get(selectedObject);
Map<String, Schema.SObjectField> fieldMap = ObjectSchema.getDescribe().fields.getMap();
List<SelectOption> fieldNames = new List<SelectOption>();
for (String fieldName: fieldMap.keySet())
{
fieldNames.add(new SelectOption(fieldName,fieldName));
//fieldMap.get(fieldName).getDescribe().getLabel();//It provides to get the object fields label.
}
return fieldNames;
}
}
I have made few changes, now its perfectly working for me.
Thanks,
Hari
<apex:page controller="objectController9" setup="false"
showHeader="false" sidebar="false">
<apex:form id="frm">
<apex:pageBlock >
<apex:pageBlockSection columns="4">
<apex:pageBlockSectionItem >
<apex:outputlabel value="Object Names :" />
<apex:actionRegion >
<apex:selectList value="{!selectedObject}" size="1">
<apex:selectOptions value="{!ObjectNames}" />
<apex:actionSupport event="onchange" rerender="myFields" />
</apex:selectList>
</apex:actionRegion>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputlabel value="Field Names :" />
<apex:outputPanel id="myFields">
<apex:actionRegion >
<apex:selectList value="{!selectedField}" size="1">
<apex:selectOptions value="{!ObjectFields}" />
</apex:selectList>
</apex:actionRegion>
</apex:outputPanel>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:inputText value="{!searchTxt}" />
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:commandButton value="Search" action="{!searchRecords}"
reRender="x,list,frm" style="margin-left:10px;" />
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
<apex:pageblockTable value="{!lstQuery}" var="eachRecord" id="list">
<apex:column value="{!eachRecord.id}" />
<apex:column value="{!eachRecord['Name']}" />
</apex:pageblockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
------------------------------------------------------------------------------------------------------------------------------------------------------------
public class objectController9
{
public string searchTxt {get; set;}
String queryString;
public void searchrecords() {
system.debug('selectedObject ---selectedField--------'+selectedObject+'---'+selectedField );
queryString = 'SELECT Id, name';
if (selectedObject!= null && selectedObject!= '')
{
queryString += ' from '+ selectedObject+'';
}
if ( searchTxt != null && searchTxt!= '')
{
if (selectedField != null && selectedField != '') {
queryString += ' where ' + selectedField +' = \''+searchTxt+'\'';
system.debug('queryString -----'+queryString );
}
}
system.debug('queryString -----------'+queryString );
lstQuery= database.query(queryString);
}
public Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
public String selectedObject {get; set;}
public list<sobject >lstQuery{get; set;}
public String selectedField {get; set;}
Public objectController9()
{
selectedObject = 'account';
}
public List<SelectOption> getObjectNames()
{
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;
}
public List<SelectOption> getObjectFields()
{
Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
Schema.SObjectType ObjectSchema = schemaMap.get(selectedObject);
Map<String, Schema.SObjectField> fieldMap = ObjectSchema.getDescribe().fields.getMap();
List<SelectOption> fieldNames = new List<SelectOption>();
for (String fieldName: fieldMap.keySet())
{
fieldNames.add(new SelectOption(fieldName,fieldName));
}
return fieldNames;
}
}
Hi,
Please refer VisualForce Developer guide and learn Apex coding
https://resources.docs.salesforce.com/sfdc/pdf/salesforce_pages_developers_guide.pdf
Thanks,
Hari
i'm actually a developer beginner jst strtd learing coding.thnk u ur suggestion was helpfull.