0

I'm attempting to execute a simple anonymous Apex script via Python with the simple_salesforce library.

from simple_salesforce import Salesforce

sf = Salesforce(instance=scratch_org_instance, session_id=access_token)
result = sf.toolingexecute('executeAnonymous', method='GET',data={"anonymousBody": 'Integer x = 0;'})
print(result)

Upon executing I'm receiving the following compile error {'line': 1, 'column': 1, 'compiled': False, 'success': False, 'compileProblem': "Unexpected token '<EOF>'.", 'exceptionStackTrace': None, 'exceptionMessage': None}

The Apex provided in the anonymousBody parameter seems correct, so I'm thinking my request might be off.

1 Answer 1

0

Turns out simple_salesforce nulls out the data parameter in their toolingexecute implementation. I've resolved my problem via the following: result = sf.toolingexecute('executeAnonymous/?anonymousBody=Integer x=0;','GET')

Snippet from their library to illustrate this (see the data parameter):

def toolingexecute(self, action, method='GET', data=None, **kwargs):
        """Makes an HTTP request to an TOOLING REST endpoint
        Arguments:
        * action -- The REST endpoint for the request.
        * method -- HTTP method for the request (default GET)
        * data -- A dict of parameters to send in a POST / PUT request
        * kwargs -- Additional kwargs to pass to `requests.request`
        """
        # If data is None, we should send an empty body, not "null", which is
        # None in json.
        json_data = json.dumps(data) if data is not None else None
        result = self._call_salesforce(
            method,
            self.tooling_url + action,
            name="toolingexecute",
            data=json_data, **kwargs
        )
        try:
            response_content = result.json()
        # pylint: disable=broad-except
        except Exception:
            response_content = result.text

        return response_content

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .