Spaces:
Sleeping
Sleeping
Create salesforce_utils.py
Browse files- salesforce_utils.py +71 -0
salesforce_utils.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import base64
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# ⚠️ Replace these with your real Salesforce credentials and Connected App values
|
| 6 |
+
USERNAME = "your-username"
|
| 7 |
+
PASSWORD = "your-password"
|
| 8 |
+
SECURITY_TOKEN = "your-security-token"
|
| 9 |
+
CLIENT_ID = "your-client-id"
|
| 10 |
+
CLIENT_SECRET = "your-client-secret"
|
| 11 |
+
LOGIN_URL = "https://login.salesforce.com" # Use https://test.salesforce.com for sandbox
|
| 12 |
+
|
| 13 |
+
def get_token():
|
| 14 |
+
"""
|
| 15 |
+
Authenticates with Salesforce using username-password OAuth2 flow.
|
| 16 |
+
Returns: access_token, instance_url
|
| 17 |
+
"""
|
| 18 |
+
url = f"{LOGIN_URL}/services/oauth2/token"
|
| 19 |
+
payload = {
|
| 20 |
+
'grant_type': 'password',
|
| 21 |
+
'client_id': CLIENT_ID,
|
| 22 |
+
'client_secret': CLIENT_SECRET,
|
| 23 |
+
'username': USERNAME,
|
| 24 |
+
'password': PASSWORD + SECURITY_TOKEN
|
| 25 |
+
}
|
| 26 |
+
response = requests.post(url, data=payload).json()
|
| 27 |
+
if 'access_token' in response:
|
| 28 |
+
return response['access_token'], response['instance_url']
|
| 29 |
+
else:
|
| 30 |
+
raise Exception(f"Salesforce login failed: {response}")
|
| 31 |
+
|
| 32 |
+
def get_salesforce_objects(token, instance_url):
|
| 33 |
+
"""
|
| 34 |
+
Fetches all Salesforce objects (standard and custom).
|
| 35 |
+
"""
|
| 36 |
+
headers = {'Authorization': f'Bearer {token}'}
|
| 37 |
+
url = f"{instance_url}/services/data/v56.0/sobjects"
|
| 38 |
+
response = requests.get(url, headers=headers)
|
| 39 |
+
return response.json().get('sobjects', [])
|
| 40 |
+
|
| 41 |
+
def create_record(object_name, data, token, instance_url):
|
| 42 |
+
"""
|
| 43 |
+
Creates a record in the given Salesforce object.
|
| 44 |
+
"""
|
| 45 |
+
headers = {
|
| 46 |
+
'Authorization': f'Bearer {token}',
|
| 47 |
+
'Content-Type': 'application/json'
|
| 48 |
+
}
|
| 49 |
+
url = f"{instance_url}/services/data/v56.0/sobjects/{object_name}/"
|
| 50 |
+
response = requests.post(url, json=data, headers=headers)
|
| 51 |
+
return response.json()
|
| 52 |
+
|
| 53 |
+
def attach_pdf(record_id, file_path, token, instance_url):
|
| 54 |
+
"""
|
| 55 |
+
Attaches a PDF file to a Salesforce record using Attachment object.
|
| 56 |
+
"""
|
| 57 |
+
with open(file_path, "rb") as f:
|
| 58 |
+
body = base64.b64encode(f.read()).decode()
|
| 59 |
+
|
| 60 |
+
data = {
|
| 61 |
+
"ParentId": record_id,
|
| 62 |
+
"Name": os.path.basename(file_path),
|
| 63 |
+
"Body": body
|
| 64 |
+
}
|
| 65 |
+
headers = {
|
| 66 |
+
'Authorization': f'Bearer {token}',
|
| 67 |
+
'Content-Type': 'application/json'
|
| 68 |
+
}
|
| 69 |
+
url = f"{instance_url}/services/data/v56.0/sobjects/Attachment"
|
| 70 |
+
response = requests.post(url, json=data, headers=headers)
|
| 71 |
+
return response.json()
|