16

I am reading the custom fields of a custom object Custom__c and then for each field I am inserting a new value:

  Public Custom__c custom1 {get;set;}

   Map<String, Schema.SObjectField> map = Schema.SObjectType.Custom1__c.fields.getMap();
   for(String fieldName : map.keySet()) {
    custom1.put(fieldName , 'some value');
   }

The problem is, some of the fields are made non-editable at when I do a put() on them, it gives me error.

What I want to do is

    Map<String, Schema.SObjectField> map = Schema.SObjectType.Custom1__c.fields.getMap();
   for(String fieldName : map.keySet()) {
       if(fieldName is editable) {
           custom1.put(fieldName , 'some value');
       }
   }

Is there a way in Apex to check if a custom field is editable? The custom fields are of different data types (text, reference, picklist..so on).

Making all fields editable in the admin side is not an option for me so I need a programmatic way to check for 'editable' condition.

Thanks for the help.

3 Answers 3

17
Map<String, Schema.SObjectField> fieldMap = Schema.SObjectType.Custom1__c.fields.getMap();
for(String fieldName : fieldMap.keySet()) {
    if(fieldMap.get(fieldName).getDescribe().isUpdateable()) {
        custom1.put(fieldName , 'some value');
    }
}
3
  • 3
    It would be helpful if you added an explanation to your code.
    – Mike Chale
    Apr 10, 2013 at 11:07
  • Is there a way to do this if I only know the object type at runtime, I have a page where the user enters a custom object name and cant find a dynamic way to get the object. Seems like there should be a Schema.SObjectType.getObject('MyCustomClassName')
    – Joelio
    Sep 5, 2013 at 18:38
  • 1
    Talk is cheap, Show me the code ~ Linus Torvalds Dec 16, 2019 at 5:56
10

Available through the Describe API. You are looking for Schema.DescribeFieldResult.isUpdateable().

0
4

Being editable and updateable is not the same thing for any sobject field.

For example:ActivatedDate, ActivatedById, LastReferencedDate, LastViewedDate, StatusCode, EmailBouncedReason etc.

These are all standard fields and all of them are updateable but not editable.

Since you can't reproduce this situation for custom fields and those fields all have spesific api names for all object, i suggest you to create an IsUpdateable map from fieldname to isupdateable result and set them as false for those special fields, if you will use them on a visualforce page on the rendered attribute or just control them with a clause on the getter method.

3
  • Joelio, If you mean what i understood from your message, and you mean the api name by object name, then you can do: Map<String , Schema.SObjectType> globalDescription = Schema.getGlobalDescribe(); Schema.sObjectType sObjType = globalDescription.get(SobjApiName); Schema.DescribeSObjectResult res = sObjType.getDescribe(); Map<String , Schema.SObjectField> mapFieldList = res.fields.getMap(); for(Schema.SObjectField field : mapFieldList.values()){ Schema.DescribeFieldResult fieldResult = field.getDescribe(); fieldResult.isUpdateable(); } is your answer
    – umithuckan
    Jan 15, 2016 at 14:03
  • but taking an object name or label from user is always a risky thing to do because of error margin.For those kind of processes, you can just use selectoption as follows: List<SelectOption> Options =new List<SelectOption>(); for(Schema.SObjectType objTyp : Schema.getGlobalDescribe().Values()){ Schema.DescribeSObjectResult describeSObjectResultObj = objTyp.getDescribe(); options.add(new SelectOption(describeSObjectResultObj .getName(),describeSObjectResultObj.getLabel())); }
    – umithuckan
    Jan 15, 2016 at 14:04
  • and use it as a value of apex:selectOptions under apex:selectList tag.Then you will have the api name when an option is selected.
    – umithuckan
    Jan 15, 2016 at 14:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .