• 69、schema的相关方法


    public class SObjectSchema {
        
        public void testSchema(){
            //获取SObject的token
            //1、先获取所有token,然后通过key获取需要的token
            //2、直接获取指定的sObject的token
            
            //1、通过获取全部描述信息,然后get方法获取需要的指定字段的描述信息
            Map<String,Schema.SObjectType> allSObjectTypeDescribes = Schema.getGlobalDescribe();
            Schema.SObjectType merchandiseType1 = allSObjectTypeDescribes.get('Merchandise__c');
            
            //2、直接获取指定sObject的token
            Schema.SObjectType merchandiseType2 = Merchandise__c.SObjectType;
            
            /*
             * 获取Schema.DescribeSObjectResult
             * 1、通过token的getDescribe方法**/
            Schema.DescribeSObjectResult merchandiseResult = merchandiseType1.getDescribe();
            
            /*
             * 2、通过System命名空间下的Schema的方法
             * */
            List<String> sObjectTypes = new String[]{'Merchandise__c'};
            List<Schema.DescribeSObjectResult> merchandiseResult1 = Schema.describeSObjects(sObjectTypes);
            System.debug(merchandiseResult.getLabel());
            System.debug('sObject的API的名称为' + merchandiseResult.getName());
            System.debug('Student表是否为自定义的Object :' + (merchandiseResult.isCustom() ? '是':'否'));   
            //---------------------------------------//
            List<Schema.ChildRelationship> childRelationResult = merchandiseResult.getChildRelationships();
            for(Schema.ChildRelationship child : childRelationResult){
                System.debug('merchandise子Object的关联名称:'+ child.getRelationshipName());
            }
            /**
             * 以下操作位获取field的元信息结果,以Education__c为例
             * 两种操作方式:
             * 1、通过DescribeSObjectResult的fields方法获取token,然后再通过getDescribe方法获取
             * 2、直接获取字段然后使用getDescribe方法
             * */
            Map<String,SObjectField> sObjectFieldMaps = merchandiseResult.fields.getMap();
            SObjectField educationField = sObjectFieldMaps.get('Name');
            Schema.DescribeFieldResult educationFieldResult = educationField.getDescribe();
            Schema.DisplayType educationType = educationFieldResult.getType();
            System.debug('education字段类型为:'+educationType);
            System.debug('education字段API名称为:'+educationFieldResult.getName());
            System.debug('education字段label名称为:'+educationFieldResult.getLabel());
            //------------------------------//
            List<Schema.PicklistEntry> educationListValues = educationFieldResult.getPicklistValues();
            Map<String,Object> educationListValueMap = new Map<String,Object>();
            for(Schema.PicklistEntry educationListItem : educationListValues){
                educationListValueMap.put(educationListItem.getValue(),
                new Map<String,Object>{
                    'value'=>educationListItem.getValue(),
                    'isActive'=>educationListItem.isActive(),
                    'isDefaultValue'=>educationListItem.isDefaultValue(),
                    'label'=>educationListItem.getLabel()
                });
            }
            Set<String> educationListValuesSet = educationListValueMap.keySet();
            System.debug('educations values'+educationListValuesSet);
            
        }
    }

    运行后的结果如下所示:

  • 相关阅读:
    转载:从git仓库中删除不必要的文件
    问题:Swiper父容器隐藏时实例化组件,组件滑动失效
    图片预加载
    移动端苹果手机:图片没有加载完成前,白色边框线是怎么来的
    bower 安装依赖提示 EINVRES Request to https://bower.herokuapp.com/packages/xxx failed with 502
    H5序列帧播放
    盟军敢死队
    二维游戏开发的点滴
    用c语言开发游戏 快乐的痛 笑着哭
    ibatis
  • 原文地址:https://www.cnblogs.com/weizhen/p/6439222.html
Copyright © 2020-2023  润新知