Tooling API execute anonymous results

I am trying to run an execute anonymous statement using tooling API.
I am running a very simple

System.debug('test');

Now the issue is as this is a GET request, the response that I get is like this –

{"line":-1,"column":-1,"compiled":true,"success":true,"compileProblem":null,"exceptionStackTrace":null,"exceptionMessage":null}

How can I get the results of my code execution to be displayed in my called UI.?

Thanks,

Ray

Answer

if you want to see a debug output then you need to get it separately.

With Tooling API it involves:
1. Seting TraceFlag
https://developer.salesforce.com/docs/atlas.en-us.api_tooling.meta/api_tooling/tooling_api_objects_traceflag.htm
2. Downloading log file
https://developer.salesforce.com/docs/atlas.en-us.api_tooling.meta/api_tooling/tooling_api_objects_apexlog.htm

An example flow can be something like this:
1. Create TraceFlag (this is to tell SFDC to start logging)
2. executeanonymous
3. retrieve debug log
4. delete TraceFlag

step 4 is not strictly necessary but helps keeping things clean.

Also, I am not sure if your use case demands REST specifically and SOAP is a no go, but if you could use SOAP API then you might want to look into SOAP API Developer’s Guide.
With Apex SOAP API you do not have to go through as many steps as with Tooling API to get a debug log.
Have a look at the DebuggingHeader documentation: https://developer.salesforce.com/docs/atlas.en-us.196.0.apexcode.meta/apexcode/sforce_api_header_debuggingheader.htm

With Apex API (SOAP version) “executeAnonymous” process looks like so (pseudocode):

val apexCode: String = ...
val conn = getApexConnection
val debugHeader = new DebuggingHeader_element()
debugHeader.setDebugLevel("Debugonly")
conn.__setDebuggingHeader(debugHeader)
val res = conn.executeAnonymous(apexCode)
val log = conn.getDebuggingInfo.getDebugLog

Attribution
Source : Link , Question Author : Kaushik Ray , Answer Author : gaiser

Leave a Comment