You need to sign in to do that
Don't have an account?
sravani 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.
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
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.
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]);
}
MetadataService service = new MetadataService();
MetadataService.MetadataPort portService = service.createService();