We have a Python test library that uses Simple Salesforce that we use to run tests on our code base. So far we have been using endpoints to trigger Apex methods from outside the org. As per the documentation under "Using Apex", we can only call endpoints. Is there any way to trigger an Apex method without exposing an endpoint, maybe using the session Id or something?


You cannot directly execute Apex Code purely using the REST API. The two means for executing code directly are Web Services (SOAP API) and Execute Anonymous (Tooling API). You could modify the Simple Salesforce library to use either of these techniques, but you'll need to write your own code to get this to happen. Using the Tooling API would arguably be easier, as you just call /services/data/vXX.X/tooling/executeAnonymous?anonymousBody=<<encoded-apex-code>>, but requires Author Apex in order to run. Using a web service requires implementing a simple SOAP client, such as the one implemented in the AJAX Toolkit (sforce.apex.execute). One could look at the source and port it to Python, but again, this is work you'd have to do, since the library doesn't currently have this feature.


It's possible, although it may not be useful or wise in all situations.

simple_salesforce does not directly provide Tooling API support, but it does provide a generic Salesforce.restful() method to make a call to (a) REST API. You can execute Anonymous Apex (assuming your user account has the right to do so) like this:

import simple_salesforce

sf = simple_salesforce.Salesforce(YOUR_CREDENTIALS_HERE)
result = sf.restful('tooling/executeAnonymous', 
                    { 'anonymousBody': 'insert new Account(Name=\'Test\');' })

You'll get back a response like:

OrderedDict([('line', -1), ('column', -1), ('compiled', True), ('success', True), ('compileProblem', None), ('exceptionStackTrace', None), ('exceptionMessage', None)])

Bearing in mind that Anonymous Apex applies user permissions as test context does not, I would suggest that this is broadly not a good way to perform testing. It is more useful in glue scripts. For automated testing, I would rather use Salesforce DX to initiate the test run and return results.

  • I really want to upvote because it's certainly a creative solution, but it's such a dangerous practice that I'd rather nobody get the idea that this is ok to do in most cases. – Sebastian Kessel Mar 7 '18 at 18:39
  • 1
    I wholeheartedly agree, @SebastianKessel, that it is in most cases a terrible idea. I have some sandbox setup-type scripts where I need this, though, and I think it's a fair solution there. – David Reed Mar 7 '18 at 18:44
  • It definitely is. I admit I only didn't upvote to avoid the impression of your idea being a good one for all cases. – Sebastian Kessel Mar 7 '18 at 18:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.