Writing Your First Deluge Function in Zoho CRM
A working Deluge function that marks an account as a customer when a deal is won, with the workflow rule, argument mapping, error checks and testing steps explained line by line.
Zoho CRM’s built-in field updates are built around the record that triggered the rule. The moment you want a won deal to read its account, decide something and then write back to it, you need a function. That is usually the first piece of Deluge a Zoho team writes, and it is a good one to learn on because it touches every basic: reading a record, following a lookup, writing to another module and checking the result.
Here is the job we will build. When a deal moves to Closed Won, set the linked account’s Customer Status to “Active Customer” and, if it is empty, stamp First Won Date with today’s date. Sales stops having to remember it, and your account list finally shows who is a customer.
Before you write any code
Create the two fields on the Accounts module first: a picklist called Customer Status (include “Active Customer” as an option) and a date field called First Won Date.
Then find their API names. Deluge never uses the label you see on the layout. It uses the API name, which Zoho generates from the label when the field is created, normally with underscores (Customer_Status, First_Won_Date). If someone later renames the label, the API name stays the same, which is exactly why code should rely on it. Check the real names in the field’s properties rather than guessing, especially on an older account where fields have been renamed a few times.
Creating the function
Go to Setup > Developer Hub > Functions > Create Function, choose Deluge, and set the category to Automation. That category is what lets a workflow rule call it. Give it one argument, dealId, as a string.
void automation.mark_account_as_customer(String dealId)
{
deal = zoho.crm.getRecordById("Deals", dealId.toLong());
account = deal.get("Account_Name");
if(account == null)
{
info "Deal " + dealId + " has no account linked. Nothing updated.";
return;
}
accountId = account.get("id").toLong();
accountRecord = zoho.crm.getRecordById("Accounts", accountId);
updateMap = Map();
updateMap.put("Customer_Status", "Active Customer");
if(accountRecord.get("First_Won_Date") == null)
{
updateMap.put("First_Won_Date", zoho.currentdate.toString("yyyy-MM-dd"));
}
response = zoho.crm.updateRecord("Accounts", accountId, updateMap);
if(response.get("id") == null)
{
info "Account update failed: " + response;
}
else
{
info "Account " + accountId + " marked as Active Customer.";
}
}
What each part is doing
The argument arrives as text. Workflow arguments are passed as strings, while the CRM tasks expect a numeric record ID. That is what .toLong() is for. Forgetting it is one of the most common first-function errors.
A lookup is a map, not an ID. deal.get("Account_Name") does not return a number. It returns a small map holding the linked account’s id and name. You pull the ID out of that map, then use it to fetch or update the account.
The null check matters more than it looks. Deals created from a quick form or an import sometimes have no account. Without that check, the next line fails on an empty value and the rule dies with an error nobody reads. With it, the function writes a clear log line and stops.
Reading before writing protects history. First Won Date should record the first win, not the latest one. Fetching the account and only adding the date when the field is empty means a customer’s tenth deal does not overwrite the date of their first.
Check the response. On success, updateRecord returns a map containing the record’s id and modified details. On failure it returns a map with "status": "error", a code such as INVALID_DATA, and a message. Testing for the id and logging the whole response otherwise gives you something useful to read when it breaks.
A default that surprises people
According to Zoho’s Deluge documentation, an updateRecord call does not fire workflow rules on the record it updates unless you ask it to. Approvals, blueprints and orchestration still run by default. Workflows do not.
Usually that is what you want, because it stops one function’s update from setting off a chain of other rules. If you do need the account’s own workflows to run, pass the options map:
response = zoho.crm.updateRecord("Accounts", accountId, updateMap, {"trigger":{"workflow"}});
Be careful turning this on. A workflow on Accounts that updates the deal, which then fires this function again, is how people build loops. Passing an empty trigger list does the opposite and suppresses all of them.
Zoho also documents a newer zoho.crm.v8.updateRecord variant with the same arguments. Either works for this job. Pick one and use it consistently across your functions so the next person is not comparing two styles.
Connecting it to a workflow rule
Go to Setup > Automation > Workflow Rules > Create Rule and pick the Deals module. Set it to run when a record is edited, with the condition Stage is Closed Won. If your organisation has more than one won stage, include each of them.
Under Instant Actions, choose Function and select mark_account_as_customer. Zoho then asks you to map the argument. Map dealId to the deal’s ID merge field, which appears as ${Deals.Deals Id}. Save the rule and confirm it is active.
One design note. “When a record is edited” with a stage condition means the rule can run again if someone edits a deal that is already Closed Won. In this function that is harmless, because the status is simply set again and the date is protected. When you write functions that send emails or create records, plan for repeat runs from the start, for example by checking a field before acting, and review the rule’s repeat setting so it only fires as often as you intend.
Testing it properly
Inside the function editor, click Run. Zoho asks for a value for dealId. Paste the ID of a test deal (it is the long number at the end of the record’s URL) and watch the Console for your info lines.
Test three cases, not one:
| Test deal | Expected log | Check on the account |
|---|---|---|
| Linked account, First Won Date empty | Marked as Active Customer | Status set, date is today |
| Linked account, date already filled | Marked as Active Customer | Date unchanged |
| No account linked | No account linked. Nothing updated. | Nothing changed |
Once the rule is live, the same info output from real runs goes to the function’s execution logs, under Setup > Developer Hub > Functions, then the function’s Logs. That is the first place to look when someone says “it didn’t update”. If your team also runs automations outside Zoho, the same thinking about failures that never alert anyone applies; we cover it in our piece on n8n error workflows.
Where to go next
Keep the function in a sandbox or test on dummy records until all three cases pass, then switch the rule on. After that, list the jobs your team still does by hand between modules (copying an owner, updating a renewal date, flagging a parent account) and turn the most frequent one into your second function.
If you would rather have Deluge, workflows and the data model set up by people who do this daily, see our CRM implementation and optimisation service or request a quote.