Callout loop not allowed error

No, there is not an immediate workaround to this :-(

The mechanism by which they measure that 'callout depth' is by transmitting an HTTP header on requests originating from Apex, Sfdc-Stack-Depth: 1 - if you try and trick the platform by explicitly setting this header to '' etc, you'll find the value is very sticky!

Unfortunately for us, there is a good business reason behind this "feature" on Salesforce' part. It is by design, to prevents callouts from blocking threads between multiple orgs / instances. sequence diagram

(hopefully this sequence diagram illustrates the intention; as an HTTP callout could take a while, the number of loopback connections is restricted to 1, otherwise imagine the DoS implications)

You might be able to overcome this issue if your B callout were read-only and its contents were updated infrequently.

You could schedule a batch process to regularly perform the callout and avail its response in a Document (or such). Then read that Document when responding to A, decoupling the two. But I admit that may be unlikely in your case...


@user31's answer is absolutely incorrect (or I am misunderstanding the question). There is nothing preventing you from making callouts from Apex REST methods to external systems. Here is some sample code:

@RestResource(urlMapping='/restexample/')
global with sharing class RestExample 
{
    @HttpGet
    global static string doGet() 
    {
        HttpRequest req = new HttpRequest();

        req.setMethod('GET');
        req.setEndpoint('https://www.google.com');
        system.debug('request body: '+req.getbody());

        HTTPResponse res = new http().send(req);

        return res.getBody();
    }
}

Note that you cannot make callouts to Salesforce as that will give you the callout loop error.

You can test this code through curl or something similar. Testing it through anonymous Apex will give the callout loop error as you are making a callout from within Salesforce to Salesforce.