Spaces:
Build error
Build error
Update app.py
Browse files148 unmodified lines149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230148 unmodified linesdef filter_valid_attributes(attributes, valid_fields):return {ATTRIBUTE_MAPPING[key]: value for key, value in attributes.items() if ATTRIBUTE_MAPPING[key] in valid_fields}#📊 Function to interact with Salesforce based on mode and typedef interact_with_salesforce(mode, entry_type, quantity, extracted_text):try:sf = Salesforce(username=SALESFORCE_USERNAME,password=SALESFORCE_PASSWORD,security_token=SALESFORCE_SECURITY_TOKEN)# Mapping mode and entry_type to Salesforce object and fieldobject_name = Nonefield_name = Noneif mode == "Entry":if entry_type == "Sales":object_name = "VENKATA_RAMANA_MOTORS__c"field_name = "Quantity__c"elif entry_type == "Non-Sales":object_name = "UNBILLING_DATA__c"field_name = "TotalQuantity__c"# Get valid fields from Salesforce object schemasf_object = sf.__getattr__(object_name)schema = sf_object.describe()valid_fields = {field["name"] for field in schema["fields"]}# Extract attributes from the extracted textattributes = extract_attributes(extracted_text)# Extract product name explicitlyproduct_name = match_product_name(extracted_text)if product_name and product_name_field in valid_fields:attributes[product_name_field] = product_name# Convert extracted keys to match Salesforce API field namesmapped_attributes = {}for key, value in attributes.items():field_name_sf = key.replace(" ", "_") + "__c" # Convert to Salesforce formatif field_name_sf in valid_fields:mapped_attributes[field_name_sf] = value # Only keep valid fieldsmapped_attributes[field_name] = quantity # Ensure Quantity is addedif not mapped_attributes:return "No valid attributes found to export."# Creating a new record with only valid attributessf_object.create(mapped_attributes)return f"✅ Record created in {object_name} with extracted valid attributes and Quantity: {quantity}."elif mode == "Exit":if entry_type == "Sales":object_name = "Inventory_Management__c"field_name = "Quantity_Sold__c"elif entry_type == "Non-Sales":object_name = "Un_Billable__c"field_name = "Sold_Out__c"# Extract product nameproduct_name = match_product_name(extracted_text)if not product_name:return "Product name could not be matched from the extracted text."query = f"SELECT Id, {field_name} FROM {object_name} WHERE Product_Name__c = '{product_name}' LIMIT 1"response = sf.query(query)if response["records"]:record_id = response["records"][0]["Id"]updated_quantity = quantitysf.__getattr__(object_name).update(record_id, {field_name: updated_quantity})return f"✅ Updated record for product '{product_name}' in {object_name}. New {field_name}: {updated_quantity}."else:return f"❌ No matching record found for product '{product_name}' in {object_name}."except Exception as e:return f"❌ Error interacting with Salesforce: {str(e)}"# Function to pull structured data from Salesforce and display as a tabledef pull_data_from_salesforce():try:148 unmodified lines149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232148 unmodified linesdef filter_valid_attributes(attributes, valid_fields):return {ATTRIBUTE_MAPPING[key]: value for key, value in attributes.items() if ATTRIBUTE_MAPPING[key] in valid_fields}# Function to interact with Salesforce based on mode and typedef interact_with_salesforce(mode, entry_type, quantity, extracted_text):try:sf = Salesforce(username=SALESFORCE_USERNAME,password=SALESFORCE_PASSWORD,security_token=SALESFORCE_SECURITY_TOKEN)# Mapping mode and entry_type to Salesforce object and fieldobject_name = Nonefield_name = Noneproduct_name_field = "Productname__c"if mode == "Entry":if entry_type == "Sales":object_name = "VENKATA_RAMANA_MOTORS__c"field_name = "Quantity__c"elif entry_type == "Non-Sales":object_name = "UNBILLING_DATA__c"field_name = "TotalQuantity__c"# Get valid fields from Salesforce object schemasf_object = sf.__getattr__(object_name)schema = sf_object.describe()valid_fields = {field["name"] for field in schema["fields"]}# Extract attributes from the extracted textattributes = extract_attributes(extracted_text)# Extract product name explicitlyproduct_name = match_product_name(extracted_text)if product_name and product_name_field in valid_fields:attributes[product_name_field] = product_name# Convert extracted keys to match Salesforce API field namesmapped_attributes = {}for key, value in attributes.items():field_name_sf = key.replace(" ", "_") + "__c" # Convert to Salesforce formatif field_name_sf in valid_fields:mapped_attributes[field_name_sf] = value # Only keep valid fieldsmapped_attributes[field_name] = quantity # Ensure Quantity is addedif not mapped_attributes:return "No valid attributes found to export."# Creating a new record with only valid attributessf_object.create(mapped_attributes)return f"✅ Record created in {object_name} with extracted valid attributes and Quantity: {quantity}."elif mode == "Exit":if entry_type == "Sales":object_name = "Inventory_Management__c"field_name = "Quantity_Sold__c"elif entry_type == "Non-Sales":object_name = "Un_Billable__c"field_name = "Sold_Out__c"# Extract product nameproduct_name = match_product_name(extracted_text)if not product_name:return "Product name could not be matched from the extracted text."query = f"SELECT Id, {field_name} FROM {object_name} WHERE Product_Name__c = '{product_name}' LIMIT 1"response = sf.query(query)if response["records"]:record_id = response["records"][0]["Id"]updated_quantity = quantitysf.__getattr__(object_name).update(record_id, {field_name: updated_quantity})return f"✅ Updated record for product '{product_name}' in {object_name}. New {field_name}: {updated_quantity}."else:return f"❌ No matching record found for product '{product_name}' in {object_name}."except Exception as e:return f"❌ Error interacting with Salesforce: {str(e)}"# Function to pull structured data from Salesforce and display as a tabledef pull_data_from_salesforce():try:
app.py+77-75