获取 apex 包前缀名称?
有没有办法在 apex 中获取托管包的前缀名称?
我有一个 SOSL 查询,但该应用程序位于开发人员组织和托管包中,如果我有办法获取包名称,那就太好了,因为我不必对其进行硬编码。
谢谢。
is there a way to get the prefix name of a managed package in apex?
I have a SOSL query but the app is in a developer org and in a managed package, if I have a way to get the package name, it would be great, because I don't have to hard code it.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,做到这一点的唯一方法是使用元数据 API。您可以调用describeMetadata(),然后评估organizationNamespace 描述元数据结果。
在 Apex 内,您可以使用动态 SOQL/DML 执行不太优雅的 try/catch 方法,至少可以确定您怀疑的内容是否确实存在。尽管这不会像元数据 API 那样告诉您实际存在什么名称空间前缀。
莱西是正确的,您不需要前缀,尽管名称歧义可能会成为一个问题。例如,如果您有一个自定义对象
Expense__c
并安装了包含ACCT__Expense__c
的会计包,那么您肯定希望显式包含ACCT__
如果打算访问受管包对象而不是您自己的对象,请添加前缀。The only way to do this, AFAIK, is to use the Metadata API. You can call describeMetadata() and then evaluate the organizationNamespace (String) value returned in describeMetadataResult.
From within Apex you could do a less elegant try/catch approach with dynamic SOQL/DML, at least to determine if something you suspect is there is actually there. Though this won't tell you what namespace prefix actually IS there like the Metadata API will.
Lacey is correct that you don't need the prefix, though name ambiguity can become a problem. So for example, if you have a custom object
Expense__c
and have installed an accounting package which includesACCT__Expense__c
, you definitely want to explicitly include theACCT__
prefix if intending to access the managed package object as opposed to your own.如果传入的命名空间不是已安装包(或正在开发的托管包)的有效命名空间,则 UserInfo.isCurrentUserLicensed('nsPrefix') 将引发 TypeException。我认为这是在没有元数据 API 的情况下您所能得到的最接近的结果。
The UserInfo.isCurrentUserLicensed('nsPrefix') will throw a TypeException if the namespace passed in is not a valid namespace of an installed package (or managed package you're developing). I think this is the closest you're going to get without the metadata API.