Not sure if this belongs here or in StackOverflow, seems 50/50.

I am trying to figure out how to build a dynamic query using simple_salesforce and there is no problem using a completely static query such as query1() below, however when trying to build the query with variables I receive the error in the question title.

Exception has occurred. simple_salesforce.exceptions.SalesforceMalformedRequest, Malformed request https://myInstance.my.salesforce.com/services/data/v38.0/query/?q=SELECT+Id%2C+FirstName%2C+LastName%2C+Email+FROM+Contact+WHERE+LastName+%3D+%2F%22Jones%2F%22. Response content: [{'message': '\nEmail FROM Contact WHERE LastName = /"Jones/"\n ^\nERROR at Row:1:Column:68\nBind variables only allowed in Apex code', 'errorCode': 'MALFORMED_QUERY'}]

def query1():
    query = "SELECT Id, FirstName, LastName, Email FROM Contact WHERE LastName = 'Jones'"
    sf = connectToSF()
    result = sf.query(query)
    print(json.dumps(result, indent=4))
    // Works perfectly, returns JSON with query results

def query2():
    fields = 'Id, FirstName, LastName, Email'
    obj = 'Contact'
    compare_this = 'LastName'
    to_this = '/"Jones/"'
    query2 = "SELECT {0} FROM {1} WHERE {2} = {3}".format(
        fields, obj, compare_this, to_this)
    sf = connectToSF()
    result2 = sf.query(query2) //Error thrown here
    print(json.dumps(result2, indent=4))

I've tried these permutations with the same error:

to_this = r'Jones'
to_this = r'/"Jones/"'
to_this = '/"Jones/"'
to_this = '"Jones"'

1 Answer 1


You need it to be a String whose content is wrapped in single quotes ('...').

This approach should work:

to_this = "'Jones'"
  • You know, I should have figured that when I typed out query1. Thanks!
    – JaredT
    Jun 20, 2018 at 18:13

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.