DSatishchandra commited on
Commit
113f186
·
verified ·
1 Parent(s): 9bf5d8f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -8
app.py CHANGED
@@ -75,34 +75,42 @@ def load_add_ons_from_salesforce():
75
  except Exception as e:
76
  return []
77
  # Function to save cart summary in Salesforce
78
- # Function to save cart summary in Salesforce
79
  def save_cart_summary_to_salesforce(cart_data, total_cost):
80
  try:
 
 
 
 
 
 
 
 
81
  # Create the Order record
82
  order_record = {
83
- 'Name': 'Order', # You can dynamically set a name, e.g., "Order <timestamp>"
84
  'Total_Cost__c': total_cost,
85
  'Order_Date__c': datetime.now().isoformat()
86
  }
87
  order_result = sf.Order__c.create(order_record)
88
- order_id = order_result['id'] # Get the created Order record ID
89
 
90
- # Create Order Item records for each item in the cart
91
  for item in cart_data:
92
- extras = ", ".join(extra['name'] for extra in item['extras']) # Combine extras into a string
93
  order_item_record = {
94
  'Name': item['name'],
95
- 'Order__c': order_id, # Link to the parent Order
96
  'Quantity__c': item['quantity'],
97
  'Price__c': item['price'],
98
  'Extras__c': extras,
99
- 'Instructions__c': item['instructions'],
100
  'Total_Cost__c': item['totalCost']
101
  }
102
- sf.Order_Item__c.create(order_item_record) # Create each order item in Salesforce
103
 
104
  return "Order and items saved successfully in Salesforce!"
105
  except Exception as e:
 
106
  return f"Error saving order in Salesforce: {str(e)}"
107
 
108
 
 
75
  except Exception as e:
76
  return []
77
  # Function to save cart summary in Salesforce
 
78
  def save_cart_summary_to_salesforce(cart_data, total_cost):
79
  try:
80
+ # Log input data for debugging
81
+ print("Saving Cart Data:", cart_data)
82
+ print("Total Cost:", total_cost)
83
+
84
+ # Validate inputs
85
+ if not cart_data or total_cost <= 0:
86
+ return "Cart is empty or total cost is invalid."
87
+
88
  # Create the Order record
89
  order_record = {
90
+ 'Name': f"Order {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}",
91
  'Total_Cost__c': total_cost,
92
  'Order_Date__c': datetime.now().isoformat()
93
  }
94
  order_result = sf.Order__c.create(order_record)
95
+ order_id = order_result['id']
96
 
97
+ # Create Order Item records
98
  for item in cart_data:
99
+ extras = ", ".join(extra['name'] for extra in item.get('extras', []))
100
  order_item_record = {
101
  'Name': item['name'],
102
+ 'Order__c': order_id,
103
  'Quantity__c': item['quantity'],
104
  'Price__c': item['price'],
105
  'Extras__c': extras,
106
+ 'Instructions__c': item.get('instructions', ''),
107
  'Total_Cost__c': item['totalCost']
108
  }
109
+ sf.Order_Item__c.create(order_item_record)
110
 
111
  return "Order and items saved successfully in Salesforce!"
112
  except Exception as e:
113
+ print(f"Error saving order in Salesforce: {str(e)}")
114
  return f"Error saving order in Salesforce: {str(e)}"
115
 
116