+ Start a Discussion
Manoj Goswami 5Manoj Goswami 5 

How to list all required fields of a selected object ?

Apex: 
<apex:page controller="allObjectListClass">
    <apex:pageBlock>
        <apex:form id = "myForm">
            
            <apex:pageBlockSection>
                <apex:selectList id="objList" value="{!selectedObj}" size="1">
                    <apex:selectOptions value="{!objName}"/>
                    <apex:actionSupport event="onchange" reRender="myForm"/>
                </apex:selectList>  
            </apex:pageBlockSection>
            
            <apex:pageBlockSection>
                <apex:pageblockTable value="{!fieldName}" var="f">
                    <apex:column value="{!f}"/>
                </apex:pageblockTable>
            </apex:pageBlockSection>
            
        </apex:form>        
    </apex:pageBlock>    
</apex:page>
Controller :
public class allObjectListClass {
    public String selectedObj {get;set;}
    
    public static List<selectOption> getObjName(){
        List<selectOption> options = new List<selectOption>();
        
        for ( Schema.SObjectType o : Schema.getGlobalDescribe().values() )
        {
            Schema.DescribeSObjectResult objResult = o.getDescribe();           
            system.debug( 'Sobject API Name: ' + objResult.getName() +' Sobject Label Name: ' + objResult.getLabel());           
            options.add(new SelectOption(objResult.getName(),objResult.getLabel()));
        }
        return options;
    }
    
    public static List<String> getFieldName(){
        List<String> reqFields = new List<String>();
        /*
			Required Code 
		*/
        return reqFields;
    }
}
I am getting a hard time dealing with schema methods, i have gone through the Salesforce documentation though.
Thanks in advance...


 
Best Answer chosen by Manoj Goswami 5
Prashant Pandey07Prashant Pandey07
Hi Manoj,

You can execute below code..you will get all the required field from the object.

I made selectedObj as a static variable so that we can access in your static method. 

Please mark this as a best answer if you this class helps you to solve your problem


 
public class allObjectListClass {
public static String selectedObj{get;set;}

    public static List<selectOption> getObjName(){
  
       List<selectOption> options = new List<selectOption>();
        for ( Schema.SObjectType o : Schema.getGlobalDescribe().values() )
        {
           Schema.DescribeSObjectResult objResult = o.getDescribe();           
           system.debug( 'Sobject API Name: ' + objResult.getName() +' Sobject Label Name: ' + objResult.getLabel());           
           options.add(new SelectOption(objResult.getName(),objResult.getLabel()));
         
        }
        return options;
    }

    
    public static List<String> getFieldName(){
     List<String> reqFields = new List<String>();
           if(selectedObj!=null){
        system.debug('^^^^^^'+selectedObj );
    
        Map <String,Schema.SObjectType> gd = Schema.getGlobalDescribe();
        Schema.SObjectType sobjType = gd.get(selectedObj);
        Schema.DescribeSObjectResult r = sobjType.getDescribe();
        Map<String, Schema.SObjectField> MapofField = r.fields.getMap();
                       for(String fieldName : MapofField.keySet()) {
                        Schema.SObjectField field = MapofField .get(fieldName);
                        Schema.DescribeFieldResult F = field.getDescribe();
        if( F.isCreateable() && !F.isNillable() && !F.isDefaultedOnCreate() )
        {
       reqFields.add(fieldName );
        }
       }

    }
    return reqFields;    
}

}


<!--************Visualfroce***********-->

<apex:page controller="allObjectListClass">
   <apex:pageBlock >
        <apex:form id="myForm">
            
            <apex:pageBlockSection >
                <apex:selectList id="objList" value="{!selectedObj}" size="1">
                    <apex:selectOptions value="{!objName}"/>
                    <apex:actionSupport event="onchange" reRender="myForm"/>
                </apex:selectList>  
            </apex:pageBlockSection>
            
            <apex:pageBlockSection >
                <apex:pageblockTable value="{!fieldName}" var="f">
                    <apex:column value="{!f}"/>
                </apex:pageblockTable>
            </apex:pageBlockSection>
            
        </apex:form>        
    </apex:pageBlock>   
</apex:page>

 

All Answers

v varaprasadv varaprasad
Hi Manoj,

Please check sample code here : 
List<String> reqFields = new List<String>();
Schema.DescribeSObjectResult r = systemObjectType.getDescribe();
            Map<String,Schema.SObjectField> M = r.fields.getMap();
            for(String fieldName : M.keySet())
            { 
                Schema.SObjectField field = M.get(fieldName);
                Schema.DescribeFieldResult F = field.getDescribe();
               //A nillable field can have empty content. A isNillable Boolean non-nillable field must have a value for the object to be                       //created or saved. 
              // if F.isNillable() is false then field is mandatory
              Boolean isFieldreq  = F.isNillable()
              if(isFieldreq  == false)
             reqFields.add(field ); 
          }

Hope this helps you.

Please let me know in case of any other assistance.

Thanks
Varaprasad

 
Prashant Pandey07Prashant Pandey07
Hi Manoj,

You can execute below code..you will get all the required field from the object.

I made selectedObj as a static variable so that we can access in your static method. 

Please mark this as a best answer if you this class helps you to solve your problem


 
public class allObjectListClass {
public static String selectedObj{get;set;}

    public static List<selectOption> getObjName(){
  
       List<selectOption> options = new List<selectOption>();
        for ( Schema.SObjectType o : Schema.getGlobalDescribe().values() )
        {
           Schema.DescribeSObjectResult objResult = o.getDescribe();           
           system.debug( 'Sobject API Name: ' + objResult.getName() +' Sobject Label Name: ' + objResult.getLabel());           
           options.add(new SelectOption(objResult.getName(),objResult.getLabel()));
         
        }
        return options;
    }

    
    public static List<String> getFieldName(){
     List<String> reqFields = new List<String>();
           if(selectedObj!=null){
        system.debug('^^^^^^'+selectedObj );
    
        Map <String,Schema.SObjectType> gd = Schema.getGlobalDescribe();
        Schema.SObjectType sobjType = gd.get(selectedObj);
        Schema.DescribeSObjectResult r = sobjType.getDescribe();
        Map<String, Schema.SObjectField> MapofField = r.fields.getMap();
                       for(String fieldName : MapofField.keySet()) {
                        Schema.SObjectField field = MapofField .get(fieldName);
                        Schema.DescribeFieldResult F = field.getDescribe();
        if( F.isCreateable() && !F.isNillable() && !F.isDefaultedOnCreate() )
        {
       reqFields.add(fieldName );
        }
       }

    }
    return reqFields;    
}

}


<!--************Visualfroce***********-->

<apex:page controller="allObjectListClass">
   <apex:pageBlock >
        <apex:form id="myForm">
            
            <apex:pageBlockSection >
                <apex:selectList id="objList" value="{!selectedObj}" size="1">
                    <apex:selectOptions value="{!objName}"/>
                    <apex:actionSupport event="onchange" reRender="myForm"/>
                </apex:selectList>  
            </apex:pageBlockSection>
            
            <apex:pageBlockSection >
                <apex:pageblockTable value="{!fieldName}" var="f">
                    <apex:column value="{!f}"/>
                </apex:pageblockTable>
            </apex:pageBlockSection>
            
        </apex:form>        
    </apex:pageBlock>   
</apex:page>

 
This was selected as the best answer
Manoj Goswami 5Manoj Goswami 5
@Prashant, 
       Thanks , worked like a charm.
pranav shah 40pranav shah 40
Hi Team,

What if i want to display same list of Sobject in Alphabetically order in VF page,..
How it can be done

Regards
Pranav Shah