URL クラス
名前空間
使用方法
1// Get a file uploaded through Chatter.
2ContentDocument doc = [SELECT Id FROM ContentDocument
3 WHERE Title = 'myfile'];
4// Create a link to the file.
5String fullFileURL = URL.getOrgDomainURL().toExternalForm() +
6 '/' + doc.id;
7system.debug(fullFileURL);例
1// Create a new account called Acme that we will create a link for later.
2Account myAccount = new Account(Name='Acme');
3insert myAccount;
4
5// Get the base URL.
6String sfdcBaseURL = URL.getOrgDomainURL().toExternalForm();
7System.debug('Base URL: ' + sfdcBaseURL );
8
9// Get the URL for the current request.
10String currentRequestURL = URL.getCurrentRequestUrl().toExternalForm();
11System.debug('Current request URL: ' + currentRequestURL);
12
13// Create the account URL from the base URL.
14String accountURL = URL.getOrgDomainURL().toExternalForm() +
15 '/' + myAccount.Id;
16System.debug('URL of a particular account: ' + accountURL);
17
18// Get some parts of the base URL.
19System.debug('Host: ' + URL.getOrgDomainURL().getHost());
20System.debug('Protocol: ' + URL.getOrgDomainURL().getProtocol());
21
22// Get the query string of the current request.
23System.debug('Query: ' + URL.getCurrentRequestUrl().getQuery());バージョン管理動作の変更
API バージョン 41.0 以降、Apex URL オブジェクトは java.net.URL 型ではなく java.net.URI 型によって表されます。URL オブジェクトがインスタンス化された API バージョンによって、特定のインスタンスへの後続のメソッドコールの動作が決まります。複雑な URL 構造のエッジケースを適切に処理できる RFC 完全互換の URL 解析のために API 41.0 以降のバージョンを使用することを強くお勧めします。また、API 41.0 以降のバージョンでは、入力が有効な RFC 互換 URL または URI 文字列であることが強制されます。
URL のコンストラクター
URL のコンストラクターは次のとおりです。
Url(spec)
署名
public Url(String spec)
パラメーター
- spec
- 型: String
- URL として解析する文字列。
Url(context, spec)
署名
public Url(Url context, String spec)
使用方法
1<scheme>://<authority><path>?<query>#<fragment>このコンストラクターの引数についての詳細は、Java のそれぞれの URL(java.net.URL, java.lang.String) コンストラクターを参照してください。
URL のメソッド
URL のメソッドは次のとおりです。
getCurrentRequestUrl()
署名
public static System.URL getCurrentRequestUrl()
戻り値
型: System.URL
使用方法
要求全体の URL の例は、https://yourInstance.salesforce.com/apex/myVfPage.apexp です。
getDefaultPort()
署名
public Integer getDefaultPort()
戻り値
型: Integer
使用方法
URL の URL スキームまたはストリームプロトコルハンドラーにデフォルトのポート番号が定義されていない場合、-1 を返します。
getFileFieldURL(entityId, fieldName)
署名
public static String getFileFieldURL(String entityId, String fieldName)
パラメーター
戻り値
型: String
使用方法
例:
例
1String fileURL =
2 URL.getFileFieldURL(
3 '087000000000123' ,
4 'AttachmentBody');getOrgDomainUrl()
署名
public static System.Url getOrgDomainUrl()
戻り値
型: System.URL
getOrgDomainUrl() は、コンテキストに関係なく、常に組織のログイン URL を返します。組織への API コールを行うときには、その URL を使用します。
使用方法
getOrgDomainUrl() を使用して、API コードで Salesforce の REST API と SOAP API を操作します。選択リスト値セットとカスタム項目の作成やカスタマイズなどで、ユーザーインターフェース API コールのエンドポイントを取得します。
getOrgDomainUrl() は、Apex コードが実行されている組織のドメイン URL にのみアクセスできます。
このメソッドで取得したドメイン URL を使用して Salesforce API を操作するのに、組織の RemoteSiteSetting は不要です。
例
この例では、Salesforce REST API を使用して、組織の制限値が取得されます。詳細は、『REST API 開発者ガイド』の「制限」を参照してください。
getSalesforceBaseUrl()
署名
public static System.URL getSalesforceBaseUrl()
戻り値
型: System.URL
現在の接続の URL を返します。たとえば、https://MyDomainName.my.salesforce.com や https://MyDomainName.lightning.force.com のような値です。
getUserInfo()
署名
public String getUserInfo()
戻り値
型: String
使用方法
UserInfo 部分が存在しない場合は null を返します。
sameFile(URLToCompare)
署名
public Boolean sameFile(System.URL URLToCompare)
パラメーター
- URLToCompare
- 型: System.URL
使用方法
URI とフラグメントコンポーネントの構文についての詳細は、「RFC3986」を参照してください。