show the required fields in picklist

How to Show The Required Fields in Salesforce Picklist Dynamically?

To show the require fields in Salesforce picklist you have to implement these codes in apex class.

Apex class:

public class objectController {
    public Map < String, Schema.SObjectType > schemaMap = Schema.getGlobalDescribe();
    public String selectedObject {
        get;
        set;
    }
    public String selectedField {
        get;
        set;
    }
    public string selectedField1 {
        get;
        set;
    }
    public String selectedobject1 {
        get;
        set;
    }
    Public objectController() {
        selectedObject = ‘account’;
        selectedobject1 = ’lead’;
    }
    public List < SelectOption > getObjectNames() {
        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;
    }
    public List < SelectOption > getObjectFields() {
        Map < String, Schema.SObjectType > schemaMap = Schema.getGlobalDescribe();
        Schema.SObjectType ObjectSchema = schemaMap.get(selectedObject);
        Map < String, Schema.SObjectField > fieldMap = ObjectSchema.getDescribe().fields.getMap();
        List < SelectOption > fieldNames = new List < SelectOption > ();
        for (String fieldName: fieldMap.keyset()) {
            Schema.SObjectField fied = fieldMap.get(fieldName);
            Schema.DescribefieldResult fres = fied.getDescribe();
            if (!fres.isNillable() && fres.Type != Schema.DisplayType.Boolean) {
                Boolean isReq = fres.isNillable();
                fieldNames.add(new SelectOption(fieldName, fieldName + ‘ * ’));
            }
            if (fres.isNillable() && fres.Type != Schema.DisplayType.Boolean) {
                Boolean isReq = fres.isNillable();
                fieldNames.add(new SelectOption(fieldName, fieldName));
            }
        }
        return fieldNames;
    }
    public list < selectoption > getfieldnames() {
        map < string, schema.SobjectType > schemakeymap = schema.getglobaldescribe();
        schema.SobjectType schemakey = schemakeymap.get(selectedobject1);
        Map < String, Schema.SObjectField > fieldMap = schemakey.getDescribe().fields.getMap();
        List < SelectOption > fieldNames = new List < SelectOption > ();
        for (string fieldname: fieldMap.keyset()) {
            fieldNames.add(new selectoption(fieldname, fieldname));
        }
        return fieldNames;
    }
}

Salesforce Visualforce pages:

By using Visualforce you can call these Methods .

Page Editor getlists1

Advertisement

Popular Salesforce Blogs