Pranay25 commited on
Commit
833e1ba
·
verified ·
1 Parent(s): 38399ff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -4
app.py CHANGED
@@ -29,6 +29,11 @@ SALESFORCE_USERNAME = os.getenv("SALESFORCE_USERNAME", "sathkruthatech@hms.com")
29
  SALESFORCE_PASSWORD = os.getenv("SALESFORCE_PASSWORD", "Hms@2025")
30
  SALESFORCE_SECURITY_TOKEN = os.getenv("SALESFORCE_SECURITY_TOKEN", "jcZQm9DJC8F2ne7o2iWU32ciB")
31
 
 
 
 
 
 
32
  # Initialize PaddleOCR
33
  ocr = PaddleOCR(use_angle_cls=True, lang='en')
34
 
@@ -72,7 +77,11 @@ def filter_valid_attributes(attributes, valid_fields):
72
  # Function to create a record in Salesforce
73
  def interact_with_salesforce(attributes):
74
  try:
75
- # Initialize Salesforce connection using environment variables only
 
 
 
 
76
  sf = Salesforce(
77
  username=SALESFORCE_USERNAME,
78
  password=SALESFORCE_PASSWORD,
@@ -91,9 +100,12 @@ def interact_with_salesforce(attributes):
91
  valid_fields = {field["name"] for field in schema["fields"]}
92
  print(f"Valid fields for {object_name}: {valid_fields}")
93
 
94
- # Check field permissions
95
- field_permissions = {field["name"]: field["createable"] for field in schema["fields"]}
96
- print(f"Field permissions (createable): {field_permissions}")
 
 
 
97
 
98
  # Filter attributes to match valid Salesforce fields
99
  filtered_attributes = filter_valid_attributes(attributes, valid_fields)
@@ -105,6 +117,12 @@ def interact_with_salesforce(attributes):
105
  if "Age__c" in filtered_attributes:
106
  filtered_attributes["Age__c"] = int(filtered_attributes["Age__c"])
107
 
 
 
 
 
 
 
108
  # Minimal test: Try creating a record with just the required Name field
109
  minimal_test = sf_object.create({"Name": "Minimal Test Record"})
110
  print(f"Minimal test successful: Created record with ID: {minimal_test['id']}")
 
29
  SALESFORCE_PASSWORD = os.getenv("SALESFORCE_PASSWORD", "Hms@2025")
30
  SALESFORCE_SECURITY_TOKEN = os.getenv("SALESFORCE_SECURITY_TOKEN", "jcZQm9DJC8F2ne7o2iWU32ciB")
31
 
32
+ # Log the credentials being used (for debugging)
33
+ print(f"Using Salesforce credentials - Username: {SALESFORCE_USERNAME}")
34
+ print(f"Password set: {'Yes' if SALESFORCE_PASSWORD else 'No'}")
35
+ print(f"Security token set: {'Yes' if SALESFORCE_SECURITY_TOKEN else 'No'}")
36
+
37
  # Initialize PaddleOCR
38
  ocr = PaddleOCR(use_angle_cls=True, lang='en')
39
 
 
77
  # Function to create a record in Salesforce
78
  def interact_with_salesforce(attributes):
79
  try:
80
+ # Validate that credentials are not empty
81
+ if not all([SALESFORCE_USERNAME, SALESFORCE_PASSWORD, SALESFORCE_SECURITY_TOKEN]):
82
+ raise ValueError("One or more Salesforce credentials are missing. Check environment variables.")
83
+
84
+ # Initialize Salesforce connection
85
  sf = Salesforce(
86
  username=SALESFORCE_USERNAME,
87
  password=SALESFORCE_PASSWORD,
 
100
  valid_fields = {field["name"] for field in schema["fields"]}
101
  print(f"Valid fields for {object_name}: {valid_fields}")
102
 
103
+ # Check field permissions and picklist values for Gender__c
104
+ field_details = {field["name"]: {
105
+ "createable": field["createable"],
106
+ "picklist_values": [val["value"] for val in field.get("picklistValues", [])] if field.get("picklistValues") else None
107
+ } for field in schema["fields"]}
108
+ print(f"Field details: {field_details}")
109
 
110
  # Filter attributes to match valid Salesforce fields
111
  filtered_attributes = filter_valid_attributes(attributes, valid_fields)
 
117
  if "Age__c" in filtered_attributes:
118
  filtered_attributes["Age__c"] = int(filtered_attributes["Age__c"])
119
 
120
+ # Validate Gender__c against picklist values
121
+ if "Gender__c" in filtered_attributes:
122
+ gender_values = field_details.get("Gender__c", {}).get("picklist_values", [])
123
+ if gender_values and filtered_attributes["Gender__c"] not in gender_values:
124
+ raise ValueError(f"Invalid value for Gender__c: '{filtered_attributes['Gender__c']}'. Allowed values: {gender_values}")
125
+
126
  # Minimal test: Try creating a record with just the required Name field
127
  minimal_test = sf_object.create({"Name": "Minimal Test Record"})
128
  print(f"Minimal test successful: Created record with ID: {minimal_test['id']}")