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
Priyanka Reddy 74Priyanka Reddy 74 

How to get editable and non editable fields from fieldset

 Hi All,

I'm having a field set which contains fields of records which include both editable and non editable, can some one suggest me like how I can make retrieve separately both editable and non editable fields.
 
fieldList = new List<String>(); 
        for(Schema.FieldSetMember memberObj : SObjectType.Account.FieldSets.rakch__DemoFieldSet.getFields()){
            qwerty += ','+memberObj.getFieldPath();
            fieldList.add(memberObj.getFieldPath());
        }



Thank you in Advance.
Best Answer chosen by Priyanka Reddy 74
ANUTEJANUTEJ (Salesforce Developers) 
So I was able to find a similar implementation in this link (https://developer.salesforce.com/forums/?id=9060G0000005ZCIQA2)

Can you try the below code once:
 
<apex:page Controller="CompleteFieldController">
    
    <apex:form >
       
       <apex:repeat value="{!allFieldName}" var="key"> 
           <apex:outputText value="{!key}"/>
           <apex:inputField value="{!sobj[key]}"/><br/>
       </apex:repeat>
        
    </apex:form>
    
    
</apex:page>
 
public with sharing class CompleteFieldController {

    Public List<String> allFieldName{get;set;}
    Public Set<String> showName{get;set;}
    Public SObject sobj{get;set;}
    
    Public CompleteFieldController(){
        
        
        sobj = Schema.getGlobalDescribe().get('Account').newSObject();
        allFieldName = new List<String>();
        Map<String, Schema.SobjectField> allMap = 
        Schema.SobjectType.Account.fields.getMap();
        for(Schema.SobjectField field : allMap.values())
        {
            
            Schema.DescribeFieldResult dfr = field.getDescribe();
            if(dfr.isCreateable() && dfr.isUpdateable())
                allFieldName.add(dfr.getName());
        
        }
        
    }
    
    

}

Let me know if it helps you and in case if it is solved close your query by marking it as solved so that it can help others in the future.

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Priyanka,

I found these two snippets that you can use:

for read only: https://salesforce.stackexchange.com/questions/184831/list-of-all-read-only-fields-on-an-object
 
Set<SObjectField> readOnlyFields = new Set<SObjectField>();
for(SObjectField field :sObjectType.Account.fields.getMap().values()){
    if(field.getDescribe().isAccessible()
        && !field.getDescribe().isUpdateable()
        && !field.getDescribe().isCreateable()){
        readOnlyFields.add(field);
    }
}



for editable fields: https://salesforce.stackexchange.com/questions/10532/finding-if-a-custom-field-is-editable-using-apex-code
 
Map<String, Schema.SObjectField> map = Schema.SObjectType.Custom1__c.fields.getMap('your object api name');
   for(String fieldName : map.keySet()) {
       if(map.get(fieldName).getDescribe().isUpdateable()) {
           custom1.put(fieldName , 'some value');
       }
   }


Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
 
Priyanka Reddy 74Priyanka Reddy 74
Hi Anutej,

Thank you for snippets, I tried this but i'm not able to access feilds in visualforce page dynamically.

Can you please do let me know.

Apex:
readOnlyFields   = new List<SObjectField>();
        accessableFields = new List<SObjectField>();
        for(SObjectField field :sObjectType.Account.fields.getMap().values()){
            if(field.getDescribe().isAccessible() && !field.getDescribe().isUpdateable() && !field.getDescribe().isCreateable()){
                readOnlyFields.add(field);
                nonEditableFields +=field + ',';
            }else{
                accessableFields.add(field);
                editableFields +=field + ',';
            }
        }

Visual Force:
 
<apex:repeat value="{!accessableFields}" var="a">
                    <tr><td>{!selectedList1.[a]}</td></tr>
                </apex:repeat>

selectedList1 is an account type.

im facing an error: Invalid field  for SObject Account

Can you please let me know where i went wrong.

Thank you.
ANUTEJANUTEJ (Salesforce Developers) 
It's a little confusing, can you please mention the requirement you are trying to solve so that we can check and respond.

Looking forward to your response.
Priyanka Reddy 74Priyanka Reddy 74
Hi Anutej,

My requirement is to display only editable feilds in visualforce.

Thank you.
 
ANUTEJANUTEJ (Salesforce Developers) 
So I was able to find a similar implementation in this link (https://developer.salesforce.com/forums/?id=9060G0000005ZCIQA2)

Can you try the below code once:
 
<apex:page Controller="CompleteFieldController">
    
    <apex:form >
       
       <apex:repeat value="{!allFieldName}" var="key"> 
           <apex:outputText value="{!key}"/>
           <apex:inputField value="{!sobj[key]}"/><br/>
       </apex:repeat>
        
    </apex:form>
    
    
</apex:page>
 
public with sharing class CompleteFieldController {

    Public List<String> allFieldName{get;set;}
    Public Set<String> showName{get;set;}
    Public SObject sobj{get;set;}
    
    Public CompleteFieldController(){
        
        
        sobj = Schema.getGlobalDescribe().get('Account').newSObject();
        allFieldName = new List<String>();
        Map<String, Schema.SobjectField> allMap = 
        Schema.SobjectType.Account.fields.getMap();
        for(Schema.SobjectField field : allMap.values())
        {
            
            Schema.DescribeFieldResult dfr = field.getDescribe();
            if(dfr.isCreateable() && dfr.isUpdateable())
                allFieldName.add(dfr.getName());
        
        }
        
    }
    
    

}

Let me know if it helps you and in case if it is solved close your query by marking it as solved so that it can help others in the future.
This was selected as the best answer