+ Start a Discussion
PRIYAN NEERUDUPRIYAN NEERUDU 

PRIYAN NEERUDU


i Have 2 picklist one should have all subjects, second picklist should have fields related to selected objects in picklist 1, now if I select a field in picklist 2 and metion a field valu in an one input filed then records with same field value in  selected object should be displayed
 
Sudhir ChowdarySudhir Chowdary
Hi,
Try Following Code:

Class:
public class objectController1
{

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 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));
                
            
            }
            return fieldNames;
      }       
}

Page:
<apex:page controller="objectController1" 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>