Loading

Apex callout from one Salesforce Org to another Salesforce Org

Date de publication: May 6, 2026
Description

Salesforce supports making Apex callouts from one Salesforce org (Org A) to another Salesforce org (Org B) using REST or SOAP web services. This is useful when two Salesforce orgs need to exchange data in real time — for example, when a company manages a subsidiary in a separate Salesforce org and needs to trigger record creation in Org B from a process running in Org A.
To implement this, Org B exposes a web service, and Org A makes an outbound callout to invoke it. Org A must have Org B's endpoint registered in its Remote Site Settings before the callout can succeed.

Important constraint:
Org B must not make any further outbound callouts as part of the same transaction. Salesforce does not allow callouts from within a callout context (a callout cannot trigger another callout).

 

Résolution

Setting Up the Web Service in Org B

1. In Org B, create an Apex class with a web service method. For example, create a class named AccountPlan containing a method createAccountPlan() that performs the desired operation and returns a String result. Mark the method with the webservice keyword so it can be exposed as a WSDL-based web service.

2. Once the class is created, generate the WSDL for the class from Setup | Apex Classes, and download it.


Creating the REST Service in Org A to Invoke Org B

1. In Org A, use the downloaded WSDL to auto-generate the stub classes: navigate to Setup | Apex Classes | Generate from WSDL. This creates a stub class (for example, soapSforceComSchemasClassAccplan and AsyncSoapSforceComSchemasClassAccplan )

2. Create an Apex class as follows: 
 

@RestResource(urlMapping='/createAccountPlan/*')
global with sharing class CallAccountPlan{
    
    @HttpGet
    global static String doGet(){
        soapSforceComSchemasClassAccplan.AccountPlan stub = new soapSforceComSchemasClassAccplan.AccountPlan();
        stub.SessionHeader = new soapSforceComSchemasClassAccplan.SessionHeader_element();
        stub.SessionHeader.sessionId = Session Id;
        return stub.createAccountPlan();
        }
}


Testing the Integration

  1. Add Org B's endpoint in Setup | Security | Remote Site Settings in Org A.
  2. Open Workbench in Org A and use the REST Explorer. Make a GET request to the REST endpoint path defined in Org A's @RestResource annotation (for example, /services/apexrest/createAccountPlan/).
  3. Verify the response returns the expected output from Org B's createAccountPlan() method.


Conclusion:
When a REST web service in Org A is called by an external system, that REST service can in turn make callouts to other Salesforce orgs, provided Org B does not make further callouts within the same transaction.



Numéro d’article de la base de connaissances

000387962

 
Chargement
Salesforce Help | Article