+ Start a Discussion
Akhil R NathAkhil R Nath 

I have created a Visualforce page which have selectoption of standard objects, when we choose the objects it is displaying the record details, but I need to add a filter conditon for each object depends on each object fields,

public class DynamicCopy {
public String selectedSObject {get;set;}

    public List<SelectOption> objects = new List<SelectOption>();

    public List<String> recordList {get;set;}//this list will hold record from selected sObject

    public String selectedRecord {get;set;}
    public string FilterRecords{get;set;}
       public Contact con{get;set;}    

    public Id selectedRecordId {get;set;}
        List<string> conditions = new List<string>();    


     public  DynamicCopy(){
                Con = new Contact();         

    }
    
    /*

    This method will add all sObjectName in sObjectNames list

    */
    
       public List<SelectOption> getSObjectNames(){
       objects.add(new SelectOption('','Select'));
       objects.add(new SelectOption('Account','Account'));
       objects.add(new SelectOption('Lead','Lead'));
       objects.add(new SelectOption('Opportunity','Opportunity'));
       objects.add(new SelectOption('Contact','Contact'));
            system.debug('objects'+objects);
       return objects;
           
    
    }
    public PageReference generateRecordList(){
        try{
            if(selectedSObject != null && selectedSObject != ''){          

                String recordQuery = 'SELECT Id, Name, Email FROM '+selectedSObject+' LIMIT 100';

                List<sObject> records = Database.query(recordQuery);

                recordProcessing(Database.query(recordQuery), true);
                

            }

        } catch(Exception e){

            ApexPages.Message myMsg;
        }
        return null;
    }
    
    public Void FilterRecords(){
        if(selectedSObject != null && selectedSObject != ''){  
            
        }
        String strQuery ='SELECT Name,LeadSource,Phone From Contact WHERE Id!=null '; 
         if(Con.Name !=null && Con.Name !=''){            
            conditions.add('Name Like \'%' +Con.Name +'%\' ');            
        }        
        if(Con.LeadSource !=null && Con.LeadSource !=''){            
            conditions.add('LeadSource Like\'%' +Con.LeadSource +'%\' ');            
        }

        
        
    }

     public void recordProcessing(List<sObject> records, boolean nameIncluded){

        if(recordList == null){

            recordList = new List<String>();

        } else{

            recordList.clear();

        }

        for(sObject sObj : records){

            String recordName;

            if(nameIncluded){

            recordName = String.valueOf(sObj.get('Name'))+' - '+String.valueOf(sObj.get('Id'));

            } else{

                recordName = String.valueOf(sObj.get('Id'));

            }

            recordList.add(recordName);

        }

        

        if(recordList.size() == 0){

            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.INFO,'No record found for this sObject!!');

            ApexPages.addMessage(myMsg);

        }

    }


}

<apex:page controller="DynamicCopy" sidebar="false">
    <apex:form id="pageForm">
        <apex:pageMessages />
        <apex:selectList label="Select Object Name" title="Select Object Name" multiselect="false" value="{!selectedSObject}" size="1">
            <apex:actionSupport event="onchange" action="{!generateRecordList}" reRender="pageForm"/> 
            <apex:selectOptions value="{!SObjectNames}" /> 
        </apex:selectList>
         <table border="2px">

         <apex:dataList id="output" value="{!recordList}" var="r">
         {!r}
        </apex:dataList>
             
       </table>

        

    </apex:form>
</apex:page>

Can Someone help me how to add a filter condition to page depend on each object.
ANUTEJANUTEJ (Salesforce Developers) 
Hi Akhil,

Although I have not implemented the scenario I tried searching for conditional rendering of the vfpage and I found the below dev forum link that discussed the same[conditional rendering of vfpage] below is the link to the thread.

>> https://success.salesforce.com/answers?id=9063A000000iXQNQA2

I hope this helps.

Regards,
Anutej
{tushar-sharma}{tushar-sharma}
Hi Akhil,

You need to use rendered attribute
<apex:outputpanel rendered="{!selectedSObject == 'Account'}">
</apex:outputpanel>


If you want to store record then this list type should be sObject and not String.
 
public List<sObject> recordList


If this answer helps you, please mark it as accepted.

Regards,
Tushar Sharma
https://newstechnologystuff.com/