Lesson Progress
0% Complete

This is complicated. But, remember, we mapped it out. We build it little by little. We use info statements to check our logic on everything.

// Initial Variables
totalAmount = 0;
count = 0;
dealCount = deals.size();
account = deals.get(0).get("Account_Name.Account_Name");
// ------------------ Time to cycle through all the deals ------------------
for each  deal in deals
{
	// ----------- If this deal is for the same account as the prior --------------
	if(account = deal.get("Account_Name.Account_Name"))
	{
		account = deal.get("Account_Name.Account_Name");
		accountId = deal.get("Account_Name").get("id");
		totalAmount = totalAmount + deal.get("Amount");
		count = count + 1;
		if(count = dealCount)
		{
			info "Going to update because this is the last deal. Amount for " + account + " is " + totalAmount;
			updateLast = zoho.crm.updateRecord("Accounts",accountId,{"Closed_Last_12":totalAmount});
			info updateLast;
		}
	}
	else
	{
		// ----------- If this deal is for a different account than the prior --------------
		info "Going to update the previous account, " + account + ", because it's now different. Amount of " + totalAmount + ". New account to total up: " + deal.get("Account_Name.Account_Name");
		updatePriorAccount = zoho.crm.updateRecord("Accounts",accountId,{"Closed_Last_12":totalAmount});
		info updatePriorAccount;
		totalAmount = 0;
		account = deal.get("Account_Name.Account_Name");
		accountId = deal.get("Account_Name").get("id");
		totalAmount = totalAmount + deal.get("Amount");
		count = count + 1;
		if(count = dealCount)
		{
			info "Going to update because this is the last deal. Amount for " + account + " is " + totalAmount;
			updateLast = zoho.crm.updateRecord("Accounts",accountId,{"Closed_Last_12":totalAmount});
			info updateLast;
		}
	}
}