Thursday, 13 December 2012

Dynamic Apex Example

Here i am going to explain a simple example for dynamic apex.


Here  we have page with a picklist containing all the objects in org.When we select an object from it then it navigates to you to insertion page of selected  object otherwise it simply shows the fields in pageblock table  on visualforce page.

Controller for this page
===================
//This is class for Displaying All objects in picklist
public class AllObjDynamicDisplayClass{

   /* public PageReference fields() {
        return null;
    }*/


    //public String objectName { get; set; }

    public String ren { get; set; }
    public PageReference Fields() {
         Schema.SObjectType gd;
          Schema.DescribeSobjectResult a11;
        if(selectedObj!=null){
              gd = Schema.getGlobalDescribe().get(selectedObj);
              a11=gd.getDescribe();
        }
        pagereference pg;
      //here we are comparing the selected object is creatable or not
        if(a11.isCreateable() && a11.isAccessible() && a11.isDeprecatedAndHidden()==false && a11.isQueryable() && a11.isSearchable())
        pg=new Pagereference('/'+a11.getKeyPrefix()+'/e?');
        return pg;
    }
//we are returning the fields from selected object if it is not creatable
   public List<Schema.SObjectField> getFields(){
    List<Schema.SObjectField> sss=new list<Schema.SObjectField>();
    if(selectedObj!=null){
    sss.clear();
    Schema.SObjectType gd = Schema.getGlobalDescribe().get(selectedObj);
    Schema.DescribeSobjectResult a11=gd.getDescribe(); 
    Map<String, Schema.SObjectField> M = a11.fields.getMap();
    for(Schema.SObjectField s1:m.values()){
        system.debug('----'+s1+'\n');
        sss.add(s1);
    }
   }
    return sss;
}
    public List<SelectOption> items { get; set; }
    public String selectedObj { get; set; }
    public AllObjDynamicDisplayClass(){
        List<Schema.sObjecttype> lst= Schema.getGlobalDescribe().values();//this is used for getting all the objects from org
        items= new List<SelectOption>();
        items.add(new SelectOption('','--None--'));
        for(Schema.sobjecttype s:lst){
       // if(String.valueOf(s).contains('__c'))
            items.add(new SelectOption(string.valueOf(s),String.valueOf(s)));
        }
    }
}

page
========

<apex:page controller="AllObjDynamicDisplayClass" sidebar="false">
    <apex:form >
        <apex:actionFunction action="{!fields}" name="fun"/>
        <apex:pageBlock >
            <apex:pageblockSection ></apex:pageblockSection>
             <apex:SelectList value="{!selectedObj}" size="1" onchange="fun()">
                 <apex:selectOptions value="{!items}"/>
             </apex:SelectList>
                  <apex:pageBlocktable value="{!Fields}" var="f" >
              <apex:column headerValue="{!selectedObj} Field Names" value="{!f}"/>
              </apex:pageBlocktable>
        </apex:pageBlock>
    </apex:form>
</apex:page>
============================

Output:
======

3 comments:

  1. Thanks for a good example. It made the concept understandable.

    ReplyDelete
  2. Hi Balaji,

    Can u please tel me how to write test class for this controller.

    Thanks
    Lakshman

    ReplyDelete

What’s the difference between Einstein Article Recommendations and Suggested Articles?

How Does Einstein Article Recommendations Work? Einstein Article Recommendations helps support agents resolve customer cases efficiently by ...