Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
    List<SelectOption> options = new List<SelectOption>();
    Schema.SObjectType leadSchema = schemaMap.get('contact');
    Map<String, Schema.SObjectField> fieldMap = leadSchema.getDescribe().fields.getMap();
    for (String fieldName: fieldMap.keySet()) {
        String fieldLabel = fieldMap.get(fieldName).getDescribe().getLabel();
        String fieldNameAPI = fieldMap.get(fieldName).getDescribe().getName();    
        Schema.DisplayType fielddataType = fieldMap.get(fieldName).getDescribe().getType();
         options.add(new SelectOption(fieldNameAPI,fieldLabel)) ;  
        //options.add(new SelectOption(fieldNameAPI+' '+string.valueof(fielddataType),fieldLabel)) ;  
    system.debug(':____:   '+options);

In this Code I am add the All field of Contact but my need is remove some field in this option

How can I remove some field in the select options in this code like as Id, also some not writeable field.


You can add a condition to filter based on Schema.DisplayType enum. Like:

fielddataType != Schema.DisplayType.String

Apex DescribeFieldResult has properties which can be used for additional filtering, like isAccessible() in your case.

  • 1
    @vikasrathi if this answers your question, mark it as such :) – Javier García Manzano Dec 18 '17 at 10:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.