Update app.py
Browse files
app.py
CHANGED
|
@@ -167,26 +167,44 @@ def interact_with_salesforce(mode, entry_type, quantity, attributes):
|
|
| 167 |
|
| 168 |
filtered_attributes = filter_valid_attributes(attributes, valid_fields)
|
| 169 |
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
else:
|
| 189 |
-
#
|
| 190 |
filtered_attributes = {
|
| 191 |
product_field_name: attributes["Product name"],
|
| 192 |
model_field_name: attributes["Model Name"],
|
|
@@ -195,8 +213,14 @@ def interact_with_salesforce(mode, entry_type, quantity, attributes):
|
|
| 195 |
field_names[0]: quantity # Assuming there's only one field to update
|
| 196 |
}
|
| 197 |
print(f"Creating new record in {object_name} with attributes {filtered_attributes}") # Debugging line
|
| 198 |
-
|
| 199 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
|
| 201 |
# Function to process image, extract attributes, and allow editing
|
| 202 |
def process_image(image, mode, entry_type, quantity):
|
|
|
|
| 167 |
|
| 168 |
filtered_attributes = filter_valid_attributes(attributes, valid_fields)
|
| 169 |
|
| 170 |
+
try:
|
| 171 |
+
if mode == "Exit":
|
| 172 |
+
query_conditions = [
|
| 173 |
+
f"{product_field_name} = '{attributes['Product name']}'",
|
| 174 |
+
f"{model_field_name} = '{attributes['Model Name']}'",
|
| 175 |
+
f"{stage_field_name} = '{attributes['Stage']}'",
|
| 176 |
+
f"{hp_field_name} = '{attributes['H.P.']}'"
|
| 177 |
+
]
|
| 178 |
+
|
| 179 |
+
query = f"SELECT Id, {', '.join(field_names)} FROM {object_name} WHERE {' AND '.join(query_conditions)} LIMIT 1"
|
| 180 |
+
print(f"Executing query: {query}") # Debugging line
|
| 181 |
+
|
| 182 |
+
response = sf.query(query) # Execute SOQL query
|
| 183 |
+
|
| 184 |
+
if response["records"]: # If a matching record is found
|
| 185 |
+
record_id = response["records"][0]["Id"]
|
| 186 |
+
updated_fields = {field: quantity for field in field_names} # Prepare update data
|
| 187 |
+
print(f"Updating record ID {record_id} with fields {updated_fields}") # Debugging line
|
| 188 |
+
|
| 189 |
+
sf_object.update(record_id, updated_fields) # Update record
|
| 190 |
+
result_message = f"✅ Updated record for product '{attributes['Product name']}' in {object_name}. Updated fields: {updated_fields}."
|
| 191 |
+
|
| 192 |
+
else:
|
| 193 |
+
# No exact match found, create a new record
|
| 194 |
+
filtered_attributes = {
|
| 195 |
+
product_field_name: attributes["Product name"],
|
| 196 |
+
model_field_name: attributes["Model Name"],
|
| 197 |
+
stage_field_name: attributes["Stage"],
|
| 198 |
+
hp_field_name: attributes["H.P."],
|
| 199 |
+
field_names[0]: quantity # Assuming there's only one field to update
|
| 200 |
+
}
|
| 201 |
+
print(f"Creating new record in {object_name} with attributes {filtered_attributes}") # Debugging line
|
| 202 |
+
|
| 203 |
+
sf_object.create(filtered_attributes) # Create new record
|
| 204 |
+
result_message = f"✅ No exact match found. A new record has been created in {object_name}."
|
| 205 |
+
|
| 206 |
else:
|
| 207 |
+
# If mode is not "Exit", create a new record directly
|
| 208 |
filtered_attributes = {
|
| 209 |
product_field_name: attributes["Product name"],
|
| 210 |
model_field_name: attributes["Model Name"],
|
|
|
|
| 213 |
field_names[0]: quantity # Assuming there's only one field to update
|
| 214 |
}
|
| 215 |
print(f"Creating new record in {object_name} with attributes {filtered_attributes}") # Debugging line
|
| 216 |
+
|
| 217 |
+
sf_object.create(filtered_attributes) # Create new record
|
| 218 |
+
result_message = f"✅ Data successfully exported to Salesforce object {object_name}."
|
| 219 |
+
|
| 220 |
+
except Exception as e:
|
| 221 |
+
result_message = f"❌ Error interacting with Salesforce: {str(e)}"
|
| 222 |
+
|
| 223 |
+
return result_message
|
| 224 |
|
| 225 |
# Function to process image, extract attributes, and allow editing
|
| 226 |
def process_image(image, mode, entry_type, quantity):
|