function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Wasim AkramWasim Akram 

i want a picklist of all objects custom and standared both when i select any object i want his object required fields name and type please help me

Tejpal KumawatTejpal Kumawat
Hello Wasim,

You need to read salesforce Schema Class : https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_schema.htm

Some snippt here :
Map < String, Schema.SObjectType > gd = Schema.getGlobalDescribe();
Schema.SObjectType objType = gd.get('your_object');
Schema.DescribeSObjectResult objDescribe = objType.getDescribe();
Regards
Tej Pal Kumawat
Skype : tejpalkumawat1991

If this answers your question mark Best Answer it as solution and then hit Like!
 
Wasim AkramWasim Akram
Hi Tejpal Your code is too good but i want all object whitch i get but now i need to get selected object Required fields and field type
Wasim AkramWasim Akram
public selectoption[] getobjects() {
        selectoption[] objects = new selectoption[0];
        map<string,schema.sobjecttype> describe = schema.getglobaldescribe();
        for(string objectname:describe.keyset()) {
            objects.add(new selectoption(objectname,describe.get(objectname).getdescribe().getname()));
        }
        return objects;
    }

i write this code for fetch all object its done but now i want Required Fields of Selected Object
Tejpal KumawatTejpal Kumawat
Hello Wasim,

Now you need to describe object & its field.
Map <String, Schema.SObjectField> fieldMap = schemaMap.get(selectedObject).getDescribe().fields.getMap();
for(Schema.SObjectField sfield : fieldMap.Values())
{
schema.describefieldresult dfield = sfield.getDescribe();
system.debug('#######' + dfield );
Pair field = new Pair();
field.key = dfield.getname();
system.debug('#######4444' + field.key);
field.val = dfield.getType () + ' : ' + dfield.getLabel ();
lstfieldname.add(field);
}

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_fields_describe.htm#apex_methods_system_fields_describe

Regards
Tej Pal Kumawat
Skype : tejpalkumawat1991

If this answers your question mark Best Answer it as solution and then hit Like!

 
Wasim AkramWasim Akram
Compile Error: Invalid type: Pair at line 21 column 26
Wasim AkramWasim Akram
This Is My Code For Fetch All Object And His Fields But There Are fetch All Fields i want only Required Fields Like This Image Its opportunity page of required fields
User-added image

-----------Controller---------------------
public class allCustomAndStandaredObject {
    public string currentobject { get; set; }
    public string currentfield { get; set; }
    public Boolean flag{get;set;}
    public selectoption[] getobjects() {
        selectoption[] objects = new selectoption[0];
        map<string,schema.sobjecttype> describe = schema.getglobaldescribe();
        for(string objectname:describe.keyset()) {
            objects.add(new selectoption(objectname,describe.get(objectname).getdescribe().getname()));
        }
        flag = true;
        return objects;
    }
    public selectoption[] getfields() {
        selectoption[] fields = new selectoption[0];
        map<string,schema.sobjecttype> describe = schema.getglobaldescribe();
        if(describe.containskey(currentobject)) {
            map<string,schema.sobjectfield> fieldmap = describe.get(currentobject).getdescribe().fields.getmap();
            for(string fieldname:fieldmap.keyset()) {
                fields.add(new selectoption(fieldname,fieldmap.get(fieldname).getdescribe().getlabel()));
                //Schema.DisplayType fielddataType = fieldMap.get(fieldName).getDescribe().getType();
            }
        }
        return fields;
    }
}



-----------Page--------------

<apex:page controller="allCustomAndStandaredObject">
    <apex:form id="form">
        <apex:pageBlock>
            <apex:pageBlockSection columns="1">
                <apex:pageBlockSectionItem>
                    <apex:outputLabel>Objects</apex:outputLabel>
                    <apex:selectList size="1" value="{!currentobject}">
                        <apex:selectOption itemValue="--Select a Object--"/>
                        <apex:selectOptions value="{!objects}"/>
                        <apex:actionSupport event="onchange" reRender="form"/>
                    </apex:selectList>
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
            <apex:pageBlockSection rendered="{!flag}">
                <apex:outputLabel>Field</apex:outputLabel>
                <apex:selectList size="1" value="{!currentfield}">
                    <apex:selectOption itemValue="--Select a Field--"/>
                    <apex:selectOptions value="{!fields}"/>
                </apex:selectList>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
Tejpal KumawatTejpal Kumawat
Hello Wasim,

IF(field -->  [is Creatable AND is NOT Nillable AND is NOT Defaulted On Create]
THEN Field is Required.

If you want to do this for all fields in an object here is a sample code:
Map<String, Schema.SObjectField> describeFields = Schema.SObjectType.THEOBJECT.fields.getMap();
//CREATE A MAP WITH FIELD NAME AS KEY AND A BOOLEAN (Required) AS VALUE
Map<String, Boolean> fieldIsRequired = new Map<String, Boolean>();
Map<String, Schema.DisplayType> fieldsTypes = new Map<String, Schema.DisplayType>();
for(String field : describeFields.keyset()){
Schema.DescribeFieldResult desribeResult = describeFields.get(f).getDescribe();
//IF FIELD IS CREATEABLE AND IS NOT NILLABLE AND IS NOT DEFAULTED ON CREATE THEN ITS REQUIRED
fieldIsRequired.put(field,desribeResult .isCreateable() && !desribeResult.isNillable() && !desribeResult.isDefaultedOnCreate());
}
If you want to check if there is any required field with null value, you could do something like this:
for(String field : fieldIsRequired.keySet()){
if(fieldsTypes.get(field) == Schema.DisplayType.STRING){
String value = (String) YourObject.get(field); //THIS WORKS ONLY FOR STRING FIELDS, IN ANY OTHER CASE YOU SHOULD CAST IT TO THE CORRECT TYPE
if((value == null || value == '') && fieldIsRequired.get(field)){
System.debug('Value can not be null for field "'+ field+'".\n');
}
}
}
Regards
Tej Pal Kumawat
Skype : tejpalkumawat1991

If this answers your question mark Best Answer it as solution and then hit Like!
 
ManojjenaManojjena
HI Wasim,

Check below link it wil give you all fields of selected onject ,you need to add condition to filter only required fields .

http://manojjena20.blogspot.in/2015/10/selecting-any-object-and-display-all.html 

Let me know if it helps !!
Thanks
Manoj