anusha(salesforce developer)

Thursday, 15 September 2016

get values from sObject dynamically in visualforce page?

How to get values from sObject dynamically in visualforce page?

Below is the simple example to get values from sObject in visualforce page.

Visualforce Page:
<apex:page controller="search">
<apex:form >

      <apex:pageBlock >
            <apex:pageBlockSection columns="2">
                  <apex:pageBlockSectionItem >
                        Enter the text to search
                  </apex:pageBlockSectionItem>
                  <apex:pageBlockSectionItem >
                        <apex:inputText value="{!searchKeyword}"/>
                  </apex:pageBlockSectionItem>
                  <apex:pageBlockSectionItem >
                        <apex:selectRadio value="{!searchCategory}" >
                              <apex:selectOption itemValue="Member__c" itemlabel="Member"/>
                              <apex:selectOption itemValue="Blog__c" itemlabel="Blog"/>
                              <apex:selectOption itemValue="Photo__c" itemlabel="Photo"/>
                        </apex:selectRadio>
                  </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
            <apex:pageBlockButtons location="bottom">
                  <apex:commandButton value="Search" action="{!find}" reRender="result"/>
            </apex:pageBlockButtons>
      </apex:pageBlock>
     
      <apex:pageBlock id="result" >
      <apex:pageBlockTable value="{!result}" var="res">
                  <apex:repeat value="{!objectFields}" var="field">
                        <apex:column value="{!res[field]}"/>
                  </apex:repeat>
      </apex:pageBlockTable>       
      </apex:pageBlock>
     
</apex:form>
</apex:page>

Apex:

public class Search
{
      public String searchKeyWord {get;set;}
      public String searchCategory;
      public List<String> objectFields {get;set;}    
      public sObject sObjectToBind {get;set;}  
      public List<sObject> result {get;set;}
     
      public search()
      {
            searchCategory = 'Member__c';
      }
     
      public void find()
      {    
            /*    Getting fields for the sObject    */
        objectFields =  new List<String>();  
        Map<String , Schema.SObjectType> globalDescription = Schema.getGlobalDescribe();   
        Schema.sObjectType sObjType = globalDescription.get(searchCategory);             
        sObjectToBind = sObjType.newSObject();  
        Schema.DescribeSObjectResult r1 = sObjType.getDescribe();  
          
        Map<String , Schema.SObjectField> mapFieldList = r1.fields.getMap();  
        for(Schema.SObjectField field : mapFieldList.values())  
        {  
            Schema.DescribeFieldResult fieldResult = field.getDescribe();  
            if(fieldResult.isAccessible())  
            {  
                objectFields.add(fieldResult.getName());
            }  
        }  
       
        /*    Building Query with the fields    */
        Integer i = 0;
        String fieldsToFetch = '';
        for(String temp:objectFields)
        {       
            Integer len = objectFields.size();
            if(i==len-1)
            {
                  fieldsToFetch = fieldsToFetch + temp;
            }
            else
            {
                  fieldsToFetch = fieldsToFetch + temp + ',';
            }
            i++;
        }
            String sql = 'Select ' + fieldsToFetch + ' From ' + searchCategory + ' WHERE Name LIKE \'%' + searchKeyword + '%\'';
            result = Database.Query(sql);
      }
     
      /*    Getting and Setting values for SearchCategory radio button    */
      public String getsearchCategory()
      {
            return searchCategory;
      }
     
      public void setsearchCategory(String searchCategory)
      {
            this.searchCategory = searchCategory;
      }    
Output:

No comments:

Post a Comment