Search code examples
pythonsalesforcesalesforce-lightning

Get all Salesforce data lead in Python


I want to get all the data of a lead object and I did this script to obtain the data of a lead and i only get an amount of 2000.

What do I have to do to get all the amount in Salesforce of an object by pyhon?

from simple_salesforce import Salesforce
from datetime import datetime
import csv
import os
import json
import account

SALESFORCE_USERNAME = '123'
PASSWORD = '123'
SECURITY_TOKEN = '123'

def main():
    # Authentication settings
    sf = Salesforce(username=SALESFORCE_USERNAME,
                    password=PASSWORD,
                    security_token=SECURITY_TOKEN)

    # Lead Column setting to be acquired
    columns = [
        "Id",
        "Email",
        "Company",
        "Address",
        "Phone",
        "FirstName",
        "LastName",
        "CreatedDate",
    ]
    sosl = 'SELECT {0[0]}, {0[1]}, {0[2]}, {0[3]}, {0[4]}, {0[5]}, {0[6]} , {0[7]} , {0[8]} FROM Lead '.format(
        columns)

    # Data acquisition with SOSL
    data = sf.query(sosl)

    # Delete CSV file if it exists
    output_csv = 'output.csv'
    if os.path.exists(output_csv):
        os.remove(output_csv)

    # Write to CSV file
    for k, v in data.items():
        if type(v) is list:
            with open(output_csv, 'w', newline="") as f:
                writer = csv.DictWriter(f, fieldnames=columns)
                writer.writeheader()
                for d in v:
                    data = json.loads(json.dumps(d))
                    del data['attributes']
                    d = datetime.strptime(
                        data['CreatedDate'], '%Y-%m-%dT%H:%M:%S.%f%z')
                    data['CreatedDate'] = d.strftime('%Y-%m-%d %H:%M:%S')
                    writer.writerow(data)


if __name__ == '__main__':
    main()

If anyone knows, please let me know.


Solution

  • You can obtain all responsive records via the query_all() or query_all_iter() methods, which are documented under Queries in the simple_salesforce documentation.

    Note that the query you are running is SOQL, not SOSL. SOSL is for full-text search.