Spaces:
Sleeping
Sleeping
Update app.py
#2
by
sairamtelagamsetti
- opened
app.py
CHANGED
@@ -149,82 +149,84 @@ def extract_attributes(extracted_text):
|
|
149 |
def filter_valid_attributes(attributes, valid_fields):
|
150 |
return {ATTRIBUTE_MAPPING[key]: value for key, value in attributes.items() if ATTRIBUTE_MAPPING[key] in valid_fields}
|
151 |
|
152 |
-
|
153 |
-
def interact_with_salesforce(mode, entry_type, quantity, extracted_text):
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
185 |
|
186 |
-
# Convert extracted keys to match Salesforce API field names
|
187 |
-
mapped_attributes = {}
|
188 |
-
for key, value in attributes.items():
|
189 |
-
field_name_sf = key.replace(" ", "_") + "__c" # Convert to Salesforce format
|
190 |
-
if field_name_sf in valid_fields:
|
191 |
-
mapped_attributes[field_name_sf] = value # Only keep valid fields
|
192 |
-
|
193 |
-
mapped_attributes[field_name] = quantity # Ensure Quantity is added
|
194 |
-
|
195 |
-
if not mapped_attributes:
|
196 |
-
return "No valid attributes found to export."
|
197 |
-
|
198 |
-
# Creating a new record with only valid attributes
|
199 |
-
sf_object.create(mapped_attributes)
|
200 |
-
return f"β
Record created in {object_name} with extracted valid attributes and Quantity: {quantity}."
|
201 |
-
|
202 |
-
elif mode == "Exit":
|
203 |
-
if entry_type == "Sales":
|
204 |
-
object_name = "Inventory_Management__c"
|
205 |
-
field_name = "Quantity_Sold__c"
|
206 |
-
elif entry_type == "Non-Sales":
|
207 |
-
object_name = "Un_Billable__c"
|
208 |
-
field_name = "Sold_Out__c"
|
209 |
-
|
210 |
-
# Extract product name
|
211 |
-
product_name = match_product_name(extracted_text)
|
212 |
-
if not product_name:
|
213 |
-
return "Product name could not be matched from the extracted text."
|
214 |
-
|
215 |
-
query = f"SELECT Id, {field_name} FROM {object_name} WHERE Product_Name__c = '{product_name}' LIMIT 1"
|
216 |
-
response = sf.query(query)
|
217 |
-
|
218 |
-
if response["records"]:
|
219 |
-
record_id = response["records"][0]["Id"]
|
220 |
-
updated_quantity = quantity
|
221 |
-
sf.__getattr__(object_name).update(record_id, {field_name: updated_quantity})
|
222 |
-
return f"β
Updated record for product '{product_name}' in {object_name}. New {field_name}: {updated_quantity}."
|
223 |
-
else:
|
224 |
-
return f"β No matching record found for product '{product_name}' in {object_name}."
|
225 |
-
|
226 |
-
except Exception as e:
|
227 |
-
return f"β Error interacting with Salesforce: {str(e)}"
|
228 |
# Function to pull structured data from Salesforce and display as a table
|
229 |
def pull_data_from_salesforce():
|
230 |
try:
|
|
|
149 |
def filter_valid_attributes(attributes, valid_fields):
|
150 |
return {ATTRIBUTE_MAPPING[key]: value for key, value in attributes.items() if ATTRIBUTE_MAPPING[key] in valid_fields}
|
151 |
|
152 |
+
# Function to interact with Salesforce based on mode and type
|
153 |
+
def interact_with_salesforce(mode, entry_type, quantity, extracted_text):
|
154 |
+
try:
|
155 |
+
sf = Salesforce(
|
156 |
+
username=SALESFORCE_USERNAME,
|
157 |
+
password=SALESFORCE_PASSWORD,
|
158 |
+
security_token=SALESFORCE_SECURITY_TOKEN
|
159 |
+
)
|
160 |
+
|
161 |
+
# Mapping mode and entry_type to Salesforce object and field
|
162 |
+
object_name = None
|
163 |
+
field_name = None
|
164 |
+
product_name_field = "Productname__c"
|
165 |
+
|
166 |
+
if mode == "Entry":
|
167 |
+
if entry_type == "Sales":
|
168 |
+
object_name = "VENKATA_RAMANA_MOTORS__c"
|
169 |
+
field_name = "Quantity__c"
|
170 |
+
elif entry_type == "Non-Sales":
|
171 |
+
object_name = "UNBILLING_DATA__c"
|
172 |
+
field_name = "TotalQuantity__c"
|
173 |
+
|
174 |
+
# Get valid fields from Salesforce object schema
|
175 |
+
sf_object = sf.__getattr__(object_name)
|
176 |
+
schema = sf_object.describe()
|
177 |
+
valid_fields = {field["name"] for field in schema["fields"]}
|
178 |
+
|
179 |
+
# Extract attributes from the extracted text
|
180 |
+
attributes = extract_attributes(extracted_text)
|
181 |
+
|
182 |
+
# Extract product name explicitly
|
183 |
+
product_name = match_product_name(extracted_text)
|
184 |
+
if product_name and product_name_field in valid_fields:
|
185 |
+
attributes[product_name_field] = product_name
|
186 |
+
|
187 |
+
# Convert extracted keys to match Salesforce API field names
|
188 |
+
mapped_attributes = {}
|
189 |
+
for key, value in attributes.items():
|
190 |
+
field_name_sf = key.replace(" ", "_") + "__c" # Convert to Salesforce format
|
191 |
+
if field_name_sf in valid_fields:
|
192 |
+
mapped_attributes[field_name_sf] = value # Only keep valid fields
|
193 |
+
|
194 |
+
mapped_attributes[field_name] = quantity # Ensure Quantity is added
|
195 |
+
|
196 |
+
if not mapped_attributes:
|
197 |
+
return "No valid attributes found to export."
|
198 |
+
|
199 |
+
# Creating a new record with only valid attributes
|
200 |
+
sf_object.create(mapped_attributes)
|
201 |
+
return f"β
Record created in {object_name} with extracted valid attributes and Quantity: {quantity}."
|
202 |
+
|
203 |
+
elif mode == "Exit":
|
204 |
+
if entry_type == "Sales":
|
205 |
+
object_name = "Inventory_Management__c"
|
206 |
+
field_name = "Quantity_Sold__c"
|
207 |
+
elif entry_type == "Non-Sales":
|
208 |
+
object_name = "Un_Billable__c"
|
209 |
+
field_name = "Sold_Out__c"
|
210 |
+
|
211 |
+
# Extract product name
|
212 |
+
product_name = match_product_name(extracted_text)
|
213 |
+
if not product_name:
|
214 |
+
return "Product name could not be matched from the extracted text."
|
215 |
+
|
216 |
+
query = f"SELECT Id, {field_name} FROM {object_name} WHERE Product_Name__c = '{product_name}' LIMIT 1"
|
217 |
+
response = sf.query(query)
|
218 |
+
|
219 |
+
if response["records"]:
|
220 |
+
record_id = response["records"][0]["Id"]
|
221 |
+
updated_quantity = quantity
|
222 |
+
sf.__getattr__(object_name).update(record_id, {field_name: updated_quantity})
|
223 |
+
return f"β
Updated record for product '{product_name}' in {object_name}. New {field_name}: {updated_quantity}."
|
224 |
+
else:
|
225 |
+
return f"β No matching record found for product '{product_name}' in {object_name}."
|
226 |
+
|
227 |
+
except Exception as e:
|
228 |
+
return f"β Error interacting with Salesforce: {str(e)}"
|
229 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
230 |
# Function to pull structured data from Salesforce and display as a table
|
231 |
def pull_data_from_salesforce():
|
232 |
try:
|