+ Start a Discussion
sandeep reddy 37sandeep reddy 37 

about scheema programing to get all the fields dynamically

when i select particular object that object allfields need to display how is it possiple please tell me 

public class displaylistofallfields {
    public map<string,schema.SObjectType> obj{set;get;}
    public set<string> lobj{set;get;}
    public list<selectoption> pick{set;get;}
    public string selected{set;get;}  
    public list<selectoption> outfields{set;get;}
    public set<string> fields{set;get;}
    public displaylistofallfields(){
        outfields=new list<selectoption>();
        obj=schema.getGlobalDescribe();
        lobj=new set<string>();
        fields=new set<string>();
      lobj=obj.keySet();
        for(string s:lobj){
            selectoption op=new selectoption(s,s);
            pick.add(op);
        }
        
    }
    public void display(){
     list<schema.SObjectfield>   allfields =pick.getlabel(selected);
            for(string f :allfields){
            selectoption p=new selectoption(f.getlabel(),f.getlabel());
            outfields.add(p);
          }
 }
}
just please tell me
 
Amit Chaudhary 8Amit Chaudhary 8
Please check below post. I hope that will help you
1) http://amitsalesforce.blogspot.in/search/label/Apex%20Describe
2) http://amitsalesforce.blogspot.in/2015/11/apex-describe-dynamic-retrieval-of.html
 
public with sharing class DescibeDemoController 
{
    public Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
    public String selectedObject {get; set;}
    public List<FieldWrapper> listField{get;set;}

    public DescibeDemoController() 
    {
        listField = new List<FieldWrapper>();
    }

    // find all sObjects available in the organization
    public  List<SelectOption> getListObejectName() 
    {
        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;
    }

    
    // Find the fields for the selected object
    public void showFields() 
    {
        listField.clear();
        Map <String, Schema.SObjectField> fieldMap = schemaMap.get(selectedObject).getDescribe().fields.getMap();
        for(Schema.SObjectField sfield : fieldMap.Values())
        {
            schema.describefieldresult dfield = sfield.getDescribe();
            FieldWrapper wObj = new FieldWrapper();
            wObj.fieldName = dfield.getLabel ();
            wObj.fieldAPIName = dfield.getname();
            listField.add(wObj);
        }
    }

    public class FieldWrapper
    {
        public String fieldName {get; set;}
        public String fieldAPIName {get; set;}
    }

}
<apex:page controller="DescibeDemoController">
    <apex:form id="Describe">
        <apex:pageBlock id="block2" >
            <apex:pageblockbuttons location="top" >
                    <apex:commandButton value="Show Fields" action="{!showFields}" />
            </apex:pageblockbuttons>
            
            <apex:pageblocksection >
                <apex:pageBlockSectionItem >
                    <apex:outputLabel >Object Name</apex:outputLabel>
                    <apex:selectList value="{!selectedObject}" size="1">
                        <apex:selectOptions value="{!ListObejectName}"/>
                    </apex:selectList>
                </apex:pageBlockSectionItem>
            </apex:pageblocksection>
        </apex:pageBlock>
        
        <apex:pageBlock id="result" title="Field Detail for {!selectedObject}" rendered="{!if(listField.size > 0 ,true,false)}"   >
            <apex:pageBlockTable value="{!listField}" var="field" rendered="{!if(listField.size > 0 ,true,false)}"> 
                <apex:column value="{!field.fieldName }" headerValue="Name" />
                <apex:column value="{!field.fieldAPIName }"  headerValue="API Name"/>
            </apex:pageblockTable>
        </apex:pageblock>
    </apex:form>
</apex:page>
User-added image

Let us know if this will help you

Thanks
Amit Chaudhary

 
Sumit Kumar Singh 9Sumit Kumar Singh 9
Hello Sandeep ,
You can try this -
<apex:page controller="Schemadef ">
<apex:pageMessages></apex:pageMessages>
 <apex:form>
     <apex:selectList value="{!objName}" size="1" >
        <apex:selectOptions value="{!objList}" />
        <apex:actionSupport event="onchange" />
    </apex:selectList> &nbsp; &nbsp;
     <apex:selectList value="{!fieldName}" size="1" >
        <apex:selectOptions value="{!fieldList}" />
    </apex:selectList>
    <br/>
 </apex:form>
</apex:page>
public class Schemadef {
    
    public String objName{get;set;}
    public String fieldName{get;set;}
    ApexPages.Message myMsg;
    
    public List<SelectOption> getobjList() {
        List<SelectOption> options = new List<SelectOption>();
        Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
        for(String objName : schemaMap.keySet()) {
            options.add(new SelectOption(objName , objName ));
        }
        return options;
    }
    
    public List<SelectOption> getfieldList() {
        List<SelectOption> options = new List<SelectOption>();
        if(objName!=null && objName!=''){
            Map<String, SObjectField> fields = Schema.getGlobalDescribe().get(objName).getDescribe().fields.getMap();
            for(String s : fields.keySet()) {
                options.add(new SelectOption(s, s));
            }
        }
        return options;
    }
}
Thanks,
Sumit Kumar Singh
sandeep reddy 37sandeep reddy 37
thanks for replay sumit but its getting ui error on vf is error :is An unexpected error has occurred. Your development organization has been notified. my code is apex is public class displaylistofallfields { public map obj{set;get;} public set lobj{set;get;} public list pick{set;get;} public string selected{set;get;} public list outfields{set;get;} public set fields{set;get;} public displaylistofallfields(){ outfields=new list(); obj=schema.getGlobalDescribe(); lobj=new set(); fields=new set(); lobj=obj.keySet(); for(string s:lobj){ selectoption op=new selectoption(s,s); pick.add(op); } } public void display(){ Map fieldMap = obj.get(selected).getDescribe().fields.getMap(); for(schema.SObjectField f :fieldmap.values()){ schema.describeFieldresult d=f.getdescribe(); selectoption p=new selectoption(d.getlabel(),d.getlabel()); outfields.add(p); } } } and vf page is
Sumit Kumar Singh 9Sumit Kumar Singh 9
Degrade to version to 26 and try..
It should work.