检索 salesforce 实例 URL 而不是 Visualforce 实例

发布于 2025-01-07 06:51:21 字数 912 浏览 5 评论 0 原文

从 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.comhttps://c.cs1.visual.force.com

问题:如何检索我想要的内容:https://cs1.salesforce.com

From a visualforce page, I need to retrieve our organization's salesforce instance's URL, and not the visual force URL.

For example I need https://cs1.salesforce.com instead of https://c.cs1.visual.force.com

Here's what I've tried so far and the outcome I got:

Accessed the Site global variable from the VF Page:

<apex:outputText value="{!$Site.Domain}" /> returns null

Sidenote: Everything in $Site.xxx seems to return null.

From the Apex controller:

public String getSfInstance()
{
  return ApexPages.currentPage().getHeaders().get('Host');
}

and

public String getSfInstance()
{
  return URL.getSalesforceBaseUrl().toExternalForm();
}

returns c.cs1.visual.force.com and https://c.cs1.visual.force.com, respectively.

Question: How do I retrieve what I want: https://cs1.salesforce.com?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

谢绝鈎搭 2025-01-14 06:51:21

这是我在 Apex 触发器中使用的东西

System.URL.getSalesforceBaseUrl().getHost().remove('-api' );

这给了我正确的 URL

Here's something that I used within my Apex Trigger

System.URL.getSalesforceBaseUrl().getHost().remove('-api' );

This gives me proper URL

戒ㄋ 2025-01-14 06:51:21

这是一个已知问题,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.

伤感在游骋 2025-01-14 06:51:21

这是一个 Apex 属性,您可以将其放入实用程序类中,该类将可靠地返回您的组织的实例。使用此功能,您可以通过将“.salesforce.com”附加到实例来轻松构建组织的 Salesforce URL:

public class Utils {

 // Returns the Salesforce Instance that is currently being run on,
 // e.g. na12, cs5, etc.
public static String Instance {
    public get {
        if (Instance == null) {
            //
            // Possible Scenarios:
            //
            // (1) ion--test1--nexus.cs0.visual.force.com  --- 5 parts, Instance is 2nd part
            // (2) na12.salesforce.com      --- 3 parts, Instance is 1st part
            // (3) ion.my.salesforce.com    --- 4 parts, Instance is not determinable

            // Split up the hostname using the period as a delimiter
            List<String> parts = System.URL.getSalesforceBaseUrl().getHost().replace('-api','').split('\\.');
            if (parts.size() == 3) Instance = parts[0];
            else if (parts.size() == 5) Instance = parts[1];
            else Instance = null;
        } return Instance;
    } private set;
}

// And you can then get the Salesforce base URL like this:
public static String GetBaseUrlForInstance() {

     return 'https://' + Instance + '.salesforce.com';

}

仅供参考:对于场景 (1),4 部分主机名的第一个可能会变得非常复杂,但您始终会能够找到实例名称作为第二部分。对于那些感兴趣的人,场景 1 的语法遵循以下模式:

     <MyDomain>--<SandboxName>--<Namespace>.<Instance>.visual.force.com

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:

public class Utils {

 // Returns the Salesforce Instance that is currently being run on,
 // e.g. na12, cs5, etc.
public static String Instance {
    public get {
        if (Instance == null) {
            //
            // Possible Scenarios:
            //
            // (1) ion--test1--nexus.cs0.visual.force.com  --- 5 parts, Instance is 2nd part
            // (2) na12.salesforce.com      --- 3 parts, Instance is 1st part
            // (3) ion.my.salesforce.com    --- 4 parts, Instance is not determinable

            // Split up the hostname using the period as a delimiter
            List<String> parts = System.URL.getSalesforceBaseUrl().getHost().replace('-api','').split('\\.');
            if (parts.size() == 3) Instance = parts[0];
            else if (parts.size() == 5) Instance = parts[1];
            else Instance = null;
        } return Instance;
    } private set;
}

// And you can then get the Salesforce base URL like this:
public static String GetBaseUrlForInstance() {

     return 'https://' + Instance + '.salesforce.com';

}

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:

     <MyDomain>--<SandboxName>--<Namespace>.<Instance>.visual.force.com
浅沫记忆 2025-01-14 06:51:21

这里有一个非常漂亮的小片段,它的作用是 VisualforcePages :-)

        String sUrlRewrite = System.URL.getSalesforceBaseUrl().getHost();
        //  Example: c.cs7.visual.force.com
        sUrlRewrite = 'https://'
                + sUrlRewrite.substring(2,6) 
                + 'salesforce.com'
                + '/'
                + recordId;

// Returns: https: //cs7.salesforce.com/00kM00000050jFMIAY

Here you have a quite nice and small snippet, that does, what it should for VisualforcePages :-)

        String sUrlRewrite = System.URL.getSalesforceBaseUrl().getHost();
        //  Example: c.cs7.visual.force.com
        sUrlRewrite = 'https://'
                + sUrlRewrite.substring(2,6) 
                + 'salesforce.com'
                + '/'
                + recordId;

// Returns: https://cs7.salesforce.com/00kM00000050jFMIAY

愚人国度 2025-01-14 06:51:21

使用:    Url.getOrgDomainUrl().toExternalForm()

谢谢,Tim Lewis


注意版本之间的行为变化,并且对 My Domain 设置敏感:

  • @Future 上下文返回https://na1.salesforce.com
  • Visualforce 上下文返回 https://na1.salesforce.com
  • Force.com 站点上下文返回 https://na1。 salesforce.com
  • @Future 上下文返回 https://mydomain.my.salesforce.com
  • Visualforce 上下文返回 https://mydomain.my.salesforce.com
  • Force.com 站点上下文返回https://mydomain.my.salesforce.com

My Domain 在新组织中是强制性的,自 21 年冬季起生效。
增强型域在所有内容中都是强制性的组织于 22 年夏季生效。

<子>

// Not to be confused with Url.getSalesforceBaseUrl()
// http://na1.salesforce.com (can happen in async apex)
// https://c.na1.visual.force.com (local Visualforce Page)
// https://ns.na1.visual.force.com (packaged Visualforce Page)
// https://custom.my.salesforce.com (org has My Domain enabled)
// https://sandbox-mydomain.na1.force.com (sandbox site with My Domain...)

另请参阅用于证明 Pod/实例端点的 Salesforce Identity API .

Use:    Url.getOrgDomainUrl().toExternalForm()

Thanks, Tim Lewis


Note behaviour changes between releases and is sensitive to My Domain settings:

  • @Future context returns https://na1.salesforce.com
  • Visualforce context returns https://na1.salesforce.com
  • Force.com Site context returns https://na1.salesforce.com
  • @Future context returns https://mydomain.my.salesforce.com
  • Visualforce context returns https://mydomain.my.salesforce.com
  • Force.com Site context returns 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.

// Not to be confused with Url.getSalesforceBaseUrl()
// http://na1.salesforce.com (can happen in async apex)
// https://c.na1.visual.force.com (local Visualforce Page)
// https://ns.na1.visual.force.com (packaged Visualforce Page)
// https://custom.my.salesforce.com (org has My Domain enabled)
// https://sandbox-mydomain.na1.force.com (sandbox site with My Domain...)

See also the Salesforce Identity API which attests the pod/instance endpoint.

你怎么敢 2025-01-14 06:51:21

修复 Alex_E 片段:

    String sUrlRewrite = System.URL.getSalesforceBaseUrl().getHost();
    String sfBaseProtocol = System.URL.getSalesforceBaseUrl().getProtocol();

    //remove namespace
    integer firstDotPos = sUrlRewrite.indexOf('.');
    sURlRewrite = sURlRewrite.substring(firstDotPos+1);

    //replace visual.force with salesforce
    sURlRewrite = sURlRewrite.replace('visual.force', 'salesforce');
    sUrlRewrite = sfBaseProtocol+'://'+sURlRewrite;

serverURL = sUrlRewrite;

Fix to Alex_E snippet:

    String sUrlRewrite = System.URL.getSalesforceBaseUrl().getHost();
    String sfBaseProtocol = System.URL.getSalesforceBaseUrl().getProtocol();

    //remove namespace
    integer firstDotPos = sUrlRewrite.indexOf('.');
    sURlRewrite = sURlRewrite.substring(firstDotPos+1);

    //replace visual.force with salesforce
    sURlRewrite = sURlRewrite.replace('visual.force', 'salesforce');
    sUrlRewrite = sfBaseProtocol+'://'+sURlRewrite;

serverURL = sUrlRewrite;
无法言说的痛 2025-01-14 06:51:21

这对我有用:

String sUrlRewrite = System.URL.getSalesforceBaseUrl().getProtocol() 
    + '://' + System.URL.getSalesforceBaseUrl().getHost() 
    + '/' + record.Id;

This works for me:

String sUrlRewrite = System.URL.getSalesforceBaseUrl().getProtocol() 
    + '://' + System.URL.getSalesforceBaseUrl().getHost() 
    + '/' + record.Id;
嘴硬脾气大 2025-01-14 06:51:21

这是与正则表达式有关的事情

public String getURL() {
    return String.format(
      'https://{0}.salesforce.com',
      new String[]{
        URL.getSalesforceBaseUrl().getHost().substringAfter('.').substringBefore('.')
      });
}

Here is something to do with regex

public String getURL() {
    return String.format(
      'https://{0}.salesforce.com',
      new String[]{
        URL.getSalesforceBaseUrl().getHost().substringAfter('.').substringBefore('.')
      });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文