Hi,
I'm no coder, so I thought I could make use of Chat GPT to generate some Deluge script to create a function that would check order frequency of our customers so that I could send an email if they go beyond their usual order pattern.
Chat GPT and Claude seemed super confident they could do the job, but failed. I keep getting errors.
Here's sample code:
string calculateOrderFrequency(string orderID)
{
// Get the Contact associated with this order
contactInfo = zoho.crm.getRelatedRecords("Contacts", "Sales_Orders", orderID);
// If no contact is found, exit the function
if(contactInfo.isEmpty())
{
return "No contact found for this order";
}
// Get the contact ID from the first related contact
contactId = contactInfo.get(0).get("id");
// Retrieve all previous orders for this contact
previous_orders = zoho.crm.getRelatedRecords("Sales_Orders", "Contacts", contactId);
// We need at least 2 orders to calculate frequency
if(previous_orders.size() >= 2)
{
// Create a list to store order dates
order_dates = List();
// Add all order dates to our list
for each rec in previous_orders
{
orderDate = rec.get("Order_Date");
if(orderDate != null)
{
order_dates.add(orderDate);
}
}
// Sort dates from oldest to newest
order_dates.sort();
// Calculate time between orders using a different approach
total_days = 0;
intervals = 0;
// Calculate date differences without using a standard loop
latest_index = order_dates.size() - 1;
// Process manually if we have at least 2 dates
if(latest_index >= 1)
{
// Manually calculate first interval
days_diff_1 = dateDiff(order_dates.get(1), order_dates.get(0), "days");
total_days = total_days + days_diff_1;
intervals = intervals + 1;
// Manually calculate second interval if we have enough dates
if(latest_index >= 2)
{
days_diff_2 = dateDiff(order_dates.get(2), order_dates.get(1), "days");
total_days = total_days + days_diff_2;
intervals = intervals + 1;
// Manually calculate third interval if we have enough dates
if(latest_index >= 3)
{
days_diff_3 = dateDiff(order_dates.get(3), order_dates.get(2), "days");
total_days = total_days + days_diff_3;
intervals = intervals + 1;
// Manually calculate fourth interval if we have enough dates
if(latest_index >= 4)
{
days_diff_4 = dateDiff(order_dates.get(4), order_dates.get(3), "days");
total_days = total_days + days_diff_4;
intervals = intervals + 1;
// Manually calculate fifth interval if we have enough dates
if(latest_index >= 5)
{
days_diff_5 = dateDiff(order_dates.get(5), order_dates.get(4), "days");
total_days = total_days + days_diff_5;
intervals = intervals + 1;
}
}
}
}
}
// Calculate average frequency (if we have valid intervals)
if(intervals > 0)
{
avg_frequency = total_days / intervals;
// Round to nearest whole number
avg_frequency = round(avg_frequency);
// Create map of fields to update
update_map = Map();
update_map.put("Average_Order_Frequency", avg_frequency);
update_map.put("Last_Order_Date", order_dates.get(order_dates.size() - 1));
update_map.put("Email_Sent_Flag", false);
// Update the contact record
update_response = zoho.crm.updateRecord("Contacts", contactId, update_map);
return "Updated contact with average order frequency: " + avg_frequency + " days";
}
else
{
return "Could not calculate intervals between orders";
}
}
else
{
// If this is their first order, just update the last order date
if(previous_orders.size() == 1)
{
update_map = Map();
update_map.put("Last_Order_Date", previous_orders.get(0).get("Order_Date"));
update_map.put("Email_Sent_Flag", false);
zoho.crm.updateRecord("Contacts", contactId, update_map);
return "First order - updated last order date only";
}
else
{
return "No orders found for this contact";
}
}
}