检索 salesforce 实例 URL 而不是 Visualforce 实例
从 Visualforce 页面,我需要检索我们组织的 salesforce 实例的 URL,而不是 Visual Force URL。
例如,我需要 https://cs1.salesforce.com
而不是 https://c.cs1.visual.force.com
这是我迄今为止尝试过的我得到的结果:
从 VF 页面访问站点全局变量:
返回空
旁注:$Site.xxx
中的所有内容似乎都返回 null
。
从 Apex 控制器:
public String getSfInstance() { return ApexPages.currentPage().getHeaders().get('Host'); }
和
public String getSfInstance() { 返回 URL.getSalesforceBaseUrl().toExternalForm(); }
分别返回 c.cs1.visual.force.com
和 https://c.cs1.visual.force.com
。
问题:如何检索我想要的内容:https://cs1.salesforce.com
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这是我在 Apex 触发器中使用的东西
这给了我正确的 URL
Here's something that I used within my Apex Trigger
This gives me proper URL
这是一个已知问题,
URL.getSalesforceBaseUrl()
应该提供此信息,但它没有。然而实际上,这对功能的影响非常有限。它们的实例域和顶级域是可以互换的,因为请求不属于一个域的 URL 会被重定向到另一个域。
例如,如果您从 cs1.salesforce.com 寻找 /apex/myPage,您将被重定向到 c.cs1...反之亦然,从 apex 域请求 /ID 将使您重定向到实例域(除非详细操作已被覆盖) )
如果这对您没有帮助,则有一种解决方法,尽管非常难看:)创建一个自定义对象来存储基本 url 并在插入/更新触发器之前创建,该触发器会将 baseURL 字段设置为
URL.getSalesforceBaseUrl().toExternalForm()
。显然,触发器是平台上唯一可以发挥作用的地方(除了执行匿名,这没有多大用处)。设置应用程序时,将某些内容插入到该表中,然后使用 SOQL 检索基本 url。This is a known issue, the
URL.getSalesforceBaseUrl()
should provide this information but it does not. However in reality this has very limited functional impact.Their instance and apex domains are interchangeable in the sense that requesting a URL that does not belong to one gets redirected to the other.
for example if you seek /apex/myPage from cs1.salesforce.com you'll get redirected to c.cs1... and vise versa requesting /ID from apex domain will get you redirected to instance domain (unless detail action has been overridden)
If this does not help you there is one workaround, albeit very ugly :) create a custom object to store the base url and create before insert/update trigger which will set the baseURL field to
URL.getSalesforceBaseUrl().toExternalForm()
. Apparently trigger is the only place on the platform where this will work (aside from execute anonymous which is not of much use). When setting up the app insert something into that table and later use SOQL to retrieve base url.这是一个 Apex 属性,您可以将其放入实用程序类中,该类将可靠地返回您的组织的实例。使用此功能,您可以通过将“.salesforce.com”附加到实例来轻松构建组织的 Salesforce URL:
仅供参考:对于场景 (1),4 部分主机名的第一个可能会变得非常复杂,但您始终会能够找到实例名称作为第二部分。对于那些感兴趣的人,场景 1 的语法遵循以下模式:
Here is an Apex property that you can throw into a Utility class that will reliably return the instance for your org. Using this, you can easily construct your organization's Salesforce URL by appending ".salesforce.com" to the Instance:
FYI: For Scenario (1), the 1st of the 4-part hostname can get really complicated, but you'll always be able to find the Instance name as the 2nd part. For those who are interested, the syntax of Scenario 1 follows this pattern:
这里有一个非常漂亮的小片段,它的作用是 VisualforcePages :-)
// Returns: https: //cs7.salesforce.com/00kM00000050jFMIAY
Here you have a quite nice and small snippet, that does, what it should for VisualforcePages :-)
// Returns: https://cs7.salesforce.com/00kM00000050jFMIAY
使用:
Url.getOrgDomainUrl().toExternalForm()
谢谢,Tim Lewis
注意版本之间的行为变化,并且对 My Domain 设置敏感:
https://na1.salesforce.com
https://na1.salesforce.com
https://na1。 salesforce.com
https://mydomain.my.salesforce.com
https://mydomain.my.salesforce.com
https://mydomain.my.salesforce.com
My Domain 在新组织中是强制性的,自 21 年冬季起生效。
增强型域在所有内容中都是强制性的组织于 22 年夏季生效。
<子>
另请参阅用于证明 Pod/实例端点的 Salesforce Identity API .
Use:
Url.getOrgDomainUrl().toExternalForm()
Thanks, Tim Lewis
Note behaviour changes between releases and is sensitive to My Domain settings:
https://na1.salesforce.com
https://na1.salesforce.com
https://na1.salesforce.com
https://mydomain.my.salesforce.com
https://mydomain.my.salesforce.com
https://mydomain.my.salesforce.com
My Domain is mandatory in new orgs effective Winter '21.
Enhanced Domains is mandatory in all orgs effective Summer '22.
See also the Salesforce Identity API which attests the pod/instance endpoint.
修复 Alex_E 片段:
Fix to Alex_E snippet:
这对我有用:
This works for me:
这是与正则表达式有关的事情
Here is something to do with regex