Help with Custom Function. Trying to find average amount of last 10 won deals
Hello Team - I have a custom function that should calculate the average amount of the last 10 won deals. The custom function is working but it is giving the wrong amount. Can somebody shed light on what I am missing?
The average amount should be around 19k but the custom function is calculating 11k
Below is the code and attached is the Deals related list under Accounts.
resp = zoho.crm.getRelatedRecords("Deals","Accounts",accountid);
iteration = 0;
total = 0.0;
count = 0;
average = 0.0;
for each ele in resp
{
iteration = iteration + 1;
probability = ele.get("Probability");
if(probability == 100 && iteration <= 10)
{
grand = ifnull(ele.get("Amount"),"0.0");
total = total + grand;
count = count + 1;
average = total / count;
}
}
mp = Map();
mp.put("Avg_Amount_Last_12_Orders",average.round(0));
update = zoho.crm.updateRecord("Accounts",accountid,mp);
info mp;
Thanks in advance for any assistance.