一.SalesForce中Schema类的简单介绍

一.Schema以及Schema NameSpace是什么

在SalesForce中Schema指的是应用程序中对象(Object)以及对象之间的各种关系。
Schema NameSpace中包含了好多的类和方法,通过这些类和方法,可以访问Schema的一些基本信息。

二.Schema常用的类和方法

在Schema NameSpace中包含了很多的类以及方法,在此并不会对所有的类和方法做介绍,只会对其中几个类的几个方法做简单介绍。

1.Schema.getGlobalDescribe方法“
这个方法返回系统中所有sObject的一个Map,其中key是sObject Name,value是sObject token。
在此需要介绍一下的是什么是token,token的中文意思有象征性的,作为标志的意思。
在这里的token指的是sObject或者是Field,token中并不包含具体的信息,但是通过token可以获取到sObject或者Field的一些信息。
Schema.SObjectType是sObject token的类型。
Schema.SObjectField是Field token的类型。
Schema.DescribeSObjectResult是sObject describe的类型。
Schema.DescribeFieldResult是Field describe的类型。
代码示例:

Map<String, Schema.SObjectType> map = Schema.getGlobalDescribe();
system.debug(map);

上面代码运行输出的log是

{acceptedeventrelation=AcceptedEventRelation, account=Account, accountchangeevent=AccountChangeEvent, accountcleaninfo=AccountCleanInfo, accountcontactrole=AccountContactRole, accountcontactrolechangeevent=AccountContactRoleChangeEvent, accountfeed=AccountFeed, accounthistory=AccountHistory, accountpartner=AccountPartner, accountshare=AccountShare, ...}

获得某一个sObject的token可以通通过一下两种方式:
例如要获得Account的token
⑴.

Schema.SObjectType type = account.sobjectType;
system.debug(type);

上面代码的输出结果是:Account
(2).

Account account = new Account();
system.debug(account.getsObjectType());

上面代码的输出结果同样是Account

    原文作者:ssspure
    原文地址: https://blog.51cto.com/ssspure/2437954
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞