Safeguarding your script with sufficient null checks is a discipline that every Zoho developer should cultivate. Imagine the frustration when troubleshooting a long and complex script only to find out that the errors were caused by certain null values. Why go through the headache of fixing it when you can avoid it? When scripting, make it a habit to ask yourself this question – will this value ever be null? If it’s a yes, that’s a place for a null check! Here are 3 tips and best practices to help you kickstart the habit.
lookupID = ifNull(record.get("Lookup_Field"),{"id":"none"}).get("id");
if (lookupID != "none")
{
//Write your subsequent actions here
}
The ifNull() function is also useful when you are getting certain fields values to be populated somewhere else.
When performing a zoho.crm.searchRecords function, we need to take into account that sometimes there will not be a match in the search. Without a null check in place, your subsequent actions that depend on the search record will fail. The search record Deluge task returns a list variable. The size() and isEmpty() can be used to validate against a list.
email = "hello@theworkflowacademy.com";
contact = zoho.crm.searchRecords("Contacts", "Email:equals:"+email);
if (contact.size() > 0)
{
//Write your subsequent actions here
}
if (field != null)
{
//Write your subsequent actions here
}
if (field != "")
{
//Write your subsequent actions here
}
Zoho Invoice/Books: Validate with containKey().
if (record.get("custom_field_hash").containKey("Custom_Field"))
{
//Write your subsequent actions here
}
Note: All Zoho Invoice/Books custom fields will be stored in a “custom_field_hash” map in the record info.
Book a free 30-minutes consultation with a Zoho expert or send us an email