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
sravani vemulasravani vemula 

How can we set custom field visibility programatically

I have created a custom field for standard object (Opportunity) and the field is created successfully. But when I am trying to edit or insert a new opportunity the custom field is not visible.
KaranrajKaranraj
Make sure that field in included in the page layout and also field accessibility is enabled for all profiles
Gaurav KheterpalGaurav Kheterpal

Setup>Create/Customize(Create for custom objects, Customize for standard objects)><obj name>>Fields>(your field) at the top you should see "View field accessibility". Make sure your have the right permissions there.

Setup>Create/Customize(Create for custom objects, Customize for standard objects)>(Object Name)>Page Layouts -> make sure the field accessibility is enabled for all profiles

 
sravani vemulasravani vemula
Thanks for the reply. 
I have searched for enabling field accessibility for all profiles and also to include the page layout but with no success. I have created the custom field programatically using SOAP API.
 Can you please send me any reference to set field accessibility for the fields created using metadata API programatically.
Dorina RaduDorina Radu
Trying to enable visibility from code as well, any way to do this? Any help would be great. Thank you.
Matthew RatliffMatthew Ratliff
Yes you can using the metdata API.  There are two classes that you'll need "Profile" and "ProfileFieldLevelSecurity"  You can do something like the following to accomplish this once you have access to the metadata service in your local env:

    private void activateFields(SObject obj){
        sObject clone = obj.getSObjectType().newSObject();
        SObjectType otype = obj.getSObjectType();
        Map<String, sObjectField> fieldMap = DescribesCache.getInstance().cacheSObjectFieldMap(otype);
        for(String key: fieldMap.keySet()) {
            sObjectField sField = fieldMap.get(key);
            DescribeFieldResult describe = DescribesCache.getInstance().cacheSObjectField(otype, sField, key);
            if(!describe.isAccessible()){
                updateFieldLevelSecurity(otype.getDescribe().getName()+'.'+key, true);
            }
        }
    }
    private void updateFieldLevelSecurity(String fieldToSecure, Boolean security)
    {
        MetadataService.MetadataPort service = createService();
        MetadataService.Profile admin = new MetadataService.Profile();
        admin.fullName = UserInfo.getUserName();
        admin.custom = security;
        MetadataService.ProfileFieldLevelSecurity fieldSec = new MetadataService.ProfileFieldLevelSecurity();
        fieldSec.field=fieldToSecure;
        fieldSec.editable=true;
        admin.fieldPermissions  = new MetadataService.ProfileFieldLevelSecurity[] {fieldSec} ;
        List<MetadataService.SaveResult> results =
            service.updateMetadata(
                new MetadataService.Metadata[] { admin });
        handleSaveResults(results[0]);
    }
Matthew RatliffMatthew Ratliff
Replace the call to createService with the following (missed adding this in the first post):

        MetadataService service = new MetadataService();
        MetadataService.MetadataPort portService = service.createService();