如何使用 apex 修改任何字段的元数据

How can I modify the meta data of any field using apex

我正在尝试使用 Apex 更改 salesforce 中对象字段的元数据。例如,我试图将所有必填字段设为非必填字段。我能够使用架构 class 并使用 isNillable() 等方法检索所有必填字段。我想问问有没有什么方法可以修改元数据。

我对此进行了很多搜索,但找不到任何有用的结果。

Schema.DescribeSObjectResult a_desc = objects.get(Name_of_of_object_whose_fields_are_to_be_retrieved).getDescribe(); 

Map<String, Schema.SObjectField> a_fields = a_desc.fields.getMap();
Set<string> x=a_fields.keySet();


//I am making a map of fieldname and bool(field required or not)        
Map<String,boolean> result=new Map<String,boolean>();
for(String p:x)
result.put(p,a_fields.get(p).getDescribe().isCreateable() && !a_fields.get(p).getDescribe().isNillable() && !a_fields.get(p).getDescribe().isDefaultedOnCreate());
//what I want is to modify isNillable and other attributes and make these changes to the fields.

您不能将所有必填字段设为非必填字段,因为其中许多字段在数据库级别是必填字段,无法修改。

例如,名称字段(在任何具有名称字段的对象上)始终是必需的。您无法更改此 属性。同样,在标准对象和子对象上始终需要主从关系字段。

要更改可修改的自定义字段的元数据,您必须使用元数据 API。它在 Apex 中不可用,除非您使用像 apex-mdapi 这样的包装器。作为警告,通过 Apex 以广泛的方式修改贵组织的元数据是 危险的 。您可以很容易地以这种方式对您的组织及其功能造成损害。我强烈建议您不要尝试这样做。必填字段是有原因的。