【探索Apex开发之】怎么取得一个Object的所有自定义字段(使用Apex实现)【推荐使用】
2023年09月13日
文章浏览:368
需求: 当我们想把一个Object里面的,所有的自定义字段都给取出来

我们也可以使用Schema命名空间下面的方法来实现

Apex代码如下

Schema.DescribeSObjectResult objectDescribeResult = Study__c.sObjectType.getDescribe();  
Map < String , Schema.SObjectField > mapFieldList = objectDescribeResult.fields.getMap();    


for (Schema.SObjectField field : mapFieldList.values() ) { 
    Schema.DescribeFieldResult fieldResult = field.getDescribe(); 
    if ( fieldResult.isCustom() ) {

        System.debug('字段名是----------->' + fieldResult.getLabel());
        System.debug('字段API参照名是----------->' + fieldResult.getName());
    }
}


取得的结果如下

58.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;NBA,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WAVE,INFO;WORKFLOW,INFO
Execute Anonymous: Schema.DescribeSObjectResult objectDescribeResult = Study__c.sObjectType.getDescribe();  
Execute Anonymous: Map < String , Schema.SObjectField > mapFieldList = objectDescribeResult.fields.getMap();    
Execute Anonymous: 
Execute Anonymous: 
Execute Anonymous: for (Schema.SObjectField field : mapFieldList.values() ) { 
Execute Anonymous:     Schema.DescribeFieldResult fieldResult = field.getDescribe(); 
Execute Anonymous:     if ( fieldResult.isCustom() ) {
Execute Anonymous: 
Execute Anonymous:         System.debug('字段名是----------->' + fieldResult.getLabel());
Execute Anonymous:         System.debug('字段API参照名是----------->' + fieldResult.getName());
Execute Anonymous:     }
Execute Anonymous: }
00:48:08.1 (1009225)|USER_INFO|[EXTERNAL]|0055h000004YWm7|yusizhong009@gmail.com|(GMT+09:00) Japan Standard Time (Asia/Tokyo)|GMT+09:00
00:48:08.1 (1050067)|EXECUTION_STARTED
00:48:08.1 (1061665)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
00:48:08.1 (4843723)|SYSTEM_MODE_ENTER|true
00:48:08.1 (5663710)|SYSTEM_MODE_ENTER|true
00:48:08.1 (6083588)|SYSTEM_MODE_ENTER|true
00:48:08.1 (6340626)|SYSTEM_MODE_ENTER|true
00:48:08.1 (6581494)|SYSTEM_MODE_ENTER|true
00:48:08.1 (6893138)|SYSTEM_MODE_ENTER|true
00:48:08.1 (7150756)|SYSTEM_MODE_ENTER|true
00:48:08.1 (7430480)|SYSTEM_MODE_ENTER|true
00:48:08.1 (7697416)|SYSTEM_MODE_ENTER|true
00:48:08.1 (8010031)|SYSTEM_MODE_ENTER|true
00:48:08.1 (8226744)|SYSTEM_MODE_ENTER|true
00:48:08.1 (8514628)|SYSTEM_MODE_ENTER|true
00:48:08.1 (8737396)|SYSTEM_MODE_ENTER|true
00:48:08.1 (9190251)|USER_DEBUG|[9]|DEBUG|字段名是----------->Memo
00:48:08.1 (9272677)|USER_DEBUG|[10]|DEBUG|字段API参照名是----------->Memo__c
00:48:08.1 (9346307)|SYSTEM_MODE_ENTER|true
00:48:08.1 (9653740)|USER_DEBUG|[9]|DEBUG|字段名是----------->Certification
00:48:08.1 (9716035)|USER_DEBUG|[10]|DEBUG|字段API参照名是----------->Certification__c
00:48:08.1 (9800952)|SYSTEM_MODE_ENTER|true
00:48:08.1 (10109965)|USER_DEBUG|[9]|DEBUG|字段名是----------->CloseDate
00:48:08.1 (10156811)|USER_DEBUG|[10]|DEBUG|字段API参照名是----------->CloseDate__c
00:48:08.1 (10213612)|SYSTEM_MODE_ENTER|true
00:48:08.1 (10478245)|USER_DEBUG|[9]|DEBUG|字段名是----------->Year
00:48:08.1 (10521538)|USER_DEBUG|[10]|DEBUG|字段API参照名是----------->Year__c
00:48:08.1 (10583745)|SYSTEM_MODE_ENTER|true
00:48:08.1 (10862072)|USER_DEBUG|[9]|DEBUG|字段名是----------->Month
00:48:08.1 (10916201)|USER_DEBUG|[10]|DEBUG|字段API参照名是----------->Month__c
00:48:08.1 (10966882)|SYSTEM_MODE_ENTER|true
00:48:08.1 (11222632)|USER_DEBUG|[9]|DEBUG|字段名是----------->SalesTargetAmount_Direct
00:48:08.1 (11275600)|USER_DEBUG|[10]|DEBUG|字段API参照名是----------->SalesTargetAmount_Direct__c
00:48:08.1 (11335806)|SYSTEM_MODE_ENTER|true
00:48:08.1 (11720753)|USER_DEBUG|[9]|DEBUG|字段名是----------->SalesByYear_BD_parent
00:48:08.1 (11765874)|USER_DEBUG|[10]|DEBUG|字段API参照名是----------->SalesByYear_BD_parent__c
00:48:08.1 (11820998)|SYSTEM_MODE_ENTER|true
00:48:08.1 (12100262)|USER_DEBUG|[9]|DEBUG|字段名是----------->MyEmail
00:48:08.1 (12145968)|USER_DEBUG|[10]|DEBUG|字段API参照名是----------->MyEmail__c
00:48:08.12 (12286961)|CUMULATIVE_LIMIT_USAGE
00:48:08.12 (12286961)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of Publish Immediate DML: 0 out of 150
  Number of DML rows: 0 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

00:48:08.12 (12286961)|CUMULATIVE_LIMIT_USAGE_END

00:48:08.1 (12379993)|CODE_UNIT_FINISHED|execute_anonymous_apex
00:48:08.1 (12406322)|EXECUTION_FINISHED


关注 收藏