Loading
Salesforce から送信されるメールは、承認済ドメインからのみとなります続きを読む

指定ログイン情報を使用している場合に OAuth トークンが自動で更新されない

公開日: Apr 24, 2026
説明

Salesforce SOAP API エンドポイントと共に指定ログイン情報を使用する場合、有効期限が切れていると、{!$Credential.OAuthToken} などのログイン情報の差し込み項目ではトークンが自動で更新されません。

以下はコールアウトの例です。
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:sfa/services/Soap/u/37.0/00DA0000000Ah7w');
req.setMethod('POST');
req.setHeader('Content-Type','text/xml; charset=utf-8');
req.setHeader('SOAPAction','wewew');
String payload='<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:partner.soap.sforce.com">'+
   '<soapenv:Header>'+
         '<urn:SessionHeader>'+
         '<urn:sessionId>{!$Credential.OAuthToken}</urn:sessionId>'+
      '</urn:SessionHeader>'+
   '</soapenv:Header>'+
   '<soapenv:Body>'+
      '<urn:query>'+
         '<urn:queryString>select id from contact limit 10</urn:queryString>'+
      '</urn:query>'+
   '</soapenv:Body>'+
'</soapenv:Envelope>';
System.debug('');
req.setBody(payLoad);
Http http = new Http();
HTTPResponse res = http.send(req);
System.debug(res.getBody());

上記のコードでは、トークンの有効期限が切れた場合の SOAP API 応答は <sf:exceptionCode>INVALID_SESSION_ID</sf:exceptionCode> となります。
 
解決策

このような動作の原因は、期限切れのセッションヘッダーがある場合の Salesforce SOAP API HTTP 応答が 500 であることです。 
サービスが HTTP 401に戻らない限り、指定ログイン情報ではトークンを自動で更新できません。

回避策:
 
1.REST サービスにダミーリクエストを実行し、認証フローを開始します。
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:sfa/services/data/v22.0/query/?q=SELECT+id+from+contact');
req.setMethod('GET');
HttpResponse res = h.send(req);

2.SOAP API コールを再度開始します。これには更新済みのトークンが含まれています。
ナレッジ記事番号

000381864

 
読み込み中
Salesforce Help | Article