Developer Console, Execute Anonymous - HTTP CODE[414]
The Developer Console is using the REST version of the Tooling API to perform the execute anonymous call. The length issue is a restriction of how the call encodes the apex into the URL.
/executeAnonymous/?anonymousBody= <url encoded body>
It is URL encoding your Apex onto the query string. Clearly there are limits to how long this can be.
I found a few viable alternatives.
- Create a temporary Apex class with the anon Apex in a static method. Then call that static method from Anon Apex. This works, but kind of defeats the purpose of anon apex.
- Use a tool (or direct call) that runs the Anonymous Apex via the SOAP API. The SOAP API puts the anonymous Apex in the SOAP POST request, so it doesn't care what the length is (beyond the Apex upper limits on class size).
- You can also run Anonymous Apex code via the older Apex SOAP API.
You can also save the anonymous apex into a file and run it via the CLI. Sample command:
sfdx force:apex:execute -f "C:\temp\MyApexScript.apex" > result.log