I imported the Tooling API WSDL back into Apex as toolingSoapSforceCom
and added a Remote Site setting from my Salesforce pod/domain.
I could then call the Tooling API with:
toolingSoapSforceCom.SforceService toolingService = new toolingSoapSforceCom.SforceService();
toolingService.SessionHeader = new toolingSoapSforceCom.SessionHeader_element();
toolingService.SessionHeader.SessionId = UserInfo.getSessionId();
toolingService.DebuggingHeader = new toolingSoapSforceCom.DebuggingHeader_element();
toolingService.DebuggingHeader.debugLevel = 'DEBUGONLY';
toolingService.DebuggingHeader.categories = new List< toolingSoapSforceCom.LogInfo>();
toolingSoapSforceCom.LogInfo apexLogInfo = new toolingSoapSforceCom.LogInfo();
apexLogInfo.category='Apex_code';
apexLogInfo.level='FINEST';
toolingService.DebuggingHeader.categories.add(apexLogInfo);
toolingSoapSforceCom.ExecuteAnonymousResult executeResponse = toolingService.executeAnonymous('System.debug(\'hello World\');');
System.assert(executeResponse.compiled);
System.debug(toolingService.DebuggingInfo)
When doing similar calls the the Apex WSDL I would then retrieve the resulting Apex Log from the DebuggingInfo header. It would appear that the DebuggingInfo header either isn't coming back from the call or isn't being populated by wsdl2apex.
My second attempt was to use a direct Http request using the XML body generated by the above attempt.
Http h = new Http();
HttpRequest req = new HttpRequest();
URL baseUrl = URL.getSalesforceBaseUrl();
req.setEndpoint( baseUrl.toExternalForm() + '/services/Soap/T/'+'30.0');
req.setMethod('POST');
req.setHeader('Content-Type', 'text/xml');
req.setHeader('SOAPAction', '""');
req.setBody('<?xml version="1.0" encoding="UTF-8"?><env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><env:Header><DebuggingHeader xmlns="urn:tooling.soap.sforce.com"><categories><category>Apex_code</category><level>FINEST</level></categories><debugLevel>DEBUGONLY</debugLevel></DebuggingHeader><SessionHeader xmlns="urn:tooling.soap.sforce.com"><sessionId>'+UserInfo.getSessionId+'</sessionId></SessionHeader></env:Header><env:Body><executeAnonymous xmlns="urn:tooling.soap.sforce.com"><String>System.debug(\'hello World\');</String></executeAnonymous></env:Body></env:Envelope>' );
HttpResponse res = h.send(req);
System.debug(res);
System.debug(res.getBody());
System.debug(res.getHeaderKeys())
Using HttpResonse.getHeaderKeys() I went looking for the DebuggingInfo SoapHeader in the response. It didn't appear to be there.
I could however query the ApexLog via SOQL and see new records being created that contained the logs.
Attempt # 3 was to using the Apex web service rather than the tooling API. Again, I imported the WSDL using wsdl2apex.
soapSforceComApex.Apex apexService = new soapSforceComApex.Apex();
apexService.SessionHeader = new soapSforceComApex.SessionHeader_element();
apexService.SessionHeader.SessionId = UserInfo.getSessionId();
apexService.DebuggingHeader = new soapSforceComApex.DebuggingHeader_element();
apexService.DebuggingHeader.debugLevel = 'DEBUGONLY';
apexService.DebuggingHeader.categories = new List< soapSforceComApex.LogInfo>();
soapSforceComApex.LogInfo apexLogInfo = new soapSforceComApex.LogInfo();
apexLogInfo.category='Apex_code';
apexLogInfo.level='FINEST';
apexService.DebuggingHeader.categories.add(apexLogInfo);
soapSforceComApex.ExecuteAnonymousResult executeResponse = apexService.executeAnonymous('System.debug(\'hello World\');');
System.assert(executeResponse.compiled);
System.debug(apexService.DebuggingInfo);
This was partially successful. The CALLOUT_RESPONSE did have a <soapenv:Header><DebuggingInfo><debugLog>
in the response. However, this wasn't populated into the DebuggingInfo property in Apex. However, you could copy the XML from the CALLOUT_REQUEST and send it via HttpRequest to https://na5.salesforce.com/services/Soap/s/30.0 so you can parse the log out of the response.
The CALLOUT_REQUEST XML:
<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<env:Header>
<DebuggingHeader xmlns="http://soap.sforce.com/2006/08/apex">
<categories>
<category>Apex_code</category>
<level>FINEST</level>
</categories>
<debugLevel>DEBUGONLY</debugLevel>
</DebuggingHeader>
<SessionHeader xmlns="http://soap.sforce.com/2006/08/apex">
<sessionId>00D700000000001!AQ_NOTAREALSESSIONID_HZIe</sessionId>
</SessionHeader>
</env:Header>
<env:Body>
<executeAnonymous xmlns="http://soap.sforce.com/2006/08/apex">
<String>System.debug('hello World');</String>
</executeAnonymous>
</env:Body>
</env:Envelope>
Best Answer
The results were erratic. So I tried the same in Chrome. And it did not fail. So we believe the issue is Firefox (Not supported by Salesforce).
Please feel free to comment if you have other scenarios or think the answer is not correct