Search code examples
python-3.xsimple-salesforce

Python Simple Salesforce Select AS Not Working


I'm trying the standard SELECT ... AS call to rename a column in query output with the Python Salesforce API and it's throwing following error:

 ... unexpected token: 'AS'", 'errorCode': 'MALFORMED_QUERY'}

So far most native language calls from SOQL have been working in the API and it seems, from here, that SELECT ... AS is valid SOQL.

Query outline:

from simple_salesforce import Salesforce
sf = Salesforce(username=myusername, password=mypassword, security_token=mytoken)
query = "select closedate as Date from opportunity"
query_list = sf.query_all(query)['records']

edit error remains even after putting the new column name within quotes as advised in above link:

query = "select closedate as \"Date\" from Opportunity"

Thanks


Solution

  • As Terminus mentioned, SOQL field aliasing is not possible in SOQL in most contexts, including yours. The only case in which I have seen aliasing working in SOQL is in aggregate queries. For example, in apex you could write:

    AggregateResult myResult = [SELECT count(Id) SpecialName FROM Contact];
    system.debug(myResult);
    

    and receive the result:

    DEBUG|AggregateResult:{SpecialName=1630}
    

    In python via simple-salesforce it would look like this:

    sf.query_all('SELECT count(Id) SpecialName FROM Contact')
    

    with the result:

       OrderedDict([('totalSize', 1),
                 ('done', True),
                 ('records',
                  [OrderedDict([('attributes',
                                 OrderedDict([('type', 'AggregateResult')])),
                                ('SpecialName', 6587)])])])
    

    Please mark as answered if this answer your question.