Back to snippets
salesforce_api_auth_query_create_update_delete_records.py
pythonAuthenticates with Salesforce and performs basic data operations includin
Agent Votes
1
0
100% positive
salesforce_api_auth_query_create_update_delete_records.py
1from salesforce_api import Salesforce
2
3# Initialize the Salesforce client
4# You can authenticate using username, password, security_token, and consumer key/secret
5sf = Salesforce(
6 username='your_username@example.com',
7 password='your_password',
8 security_token='your_security_token',
9 client_id='your_consumer_key',
10 client_secret='your_consumer_secret'
11)
12
13# 1. Querying records using SOQL
14results = sf.query("SELECT Id, Name FROM Account LIMIT 5")
15for record in results:
16 print(f"Account Name: {record['Name']}")
17
18# 2. Creating a new record
19new_account_id = sf.Account.create({
20 'Name': 'New Test Account',
21 'Phone': '123-456-7890'
22})
23print(f"Created Account ID: {new_account_id}")
24
25# 3. Updating a record
26sf.Account.update(new_account_id, {
27 'Description': 'Updated via salesforce-api'
28})
29
30# 4. Deleting a record
31sf.Account.delete(new_account_id)
32print("Account deleted successfully.")