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
-----------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; } }
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!
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 : Regards
Tej Pal Kumawat
Skype : tejpalkumawat1991
If this answers your question mark Best Answer it as solution and then hit Like!
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
Now you need to describe object & its 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!
-----------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>
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: If you want to check if there is any required field with null value, you could do something like this: Regards
Tej Pal Kumawat
Skype : tejpalkumawat1991
If this answers your question mark Best Answer it as solution and then hit Like!
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