Saturday, September 17, 2016

Writing unit tests for Zoho Creator apps


Most code is full of bugs. Some estimates suggest that every 20 lines of code will contain a serious bug. What can we do about it? One technique that's proven to reduce bug density is test driven development. By writing unit tests to prove that our code does what it's meant to do, we reduce the likelihood that bugs will creep in.

But how can we write unit tests for Zoho Creator apps? There's no inbuilt testing framework. We could use Selenium to write UI tests but they're slow and laborious to write and maintain.

As an alternative, I propose using HTML pages as unit testing harnesses. Here's an example of a unit test I set up to test some advanced trigonometric functions I added to my app so I could calculate distance using the great circular distance method. Deluge doesn't come with arctan/msatan out of the box, so I had to add those in. Here's an example of what I did to ensure that the functions gave the correct result.

Here's the code to achieve that result:

Let me know what you think. Would be cool if we could get a proper unit testing framework built into the framework but for now, this will have to do.

Thursday, September 8, 2016

Debugging Deluge script in Zoho Creator/Zoho CRM

Deluge is a powerful language that simplifies a ton of things, e.g. database lookups. But I have to say, it's very painful to debug. Interpreting longish scripts takes ages, error messages are obtuse if they exist at all and even the hints in the IDE don't always help (ever tried using containsIgnoreCase based on the auto-complete suggestion?).

Here are my tips for debugging code when it's not working.

1. Split your code up into small custom functions. Nothing's more painful than waiting 30 seconds for a really long (say 1000 lines) HTML page to save and only then get feedback on a syntax error. I've got a few apps that are like this and it's not fun to work with at all. I haven't seen any improvements in the compile speed over the last few years, so my conclusion is that it's always best to write most of your logic code as small custom functions and then build that up in the HTML page so that you'll get quick feedback on syntax errors.

2. If your code doesn't do what you expect it to do, use info statements to log out values along the way. E.g. if I expect a CRM searchRecords task to return certain data and then perform operations on it afterwards, it would be prudent to throw an info crmRecords; call in before so I can check that it's actually retrieving the correct records. Info statements are only shown to admin users but you should still probably remove them after you've finished debugging.

3. If all else fails, comment out blocks of code to identify what's causing the error. I find this helpful for catching syntax errors that the Deluge compiler rarely shows properly. I code in Javascript most of the time and so when I go to Deluge (which looks very similar to JS), the temptation is to write things like listRecords = new List(); (instead of listRecords = List();). This code will often throw some weird error message pointing at a line that is completely unrelated to where the error is or it sometimes doesn't even throw an error at all (particularly when you're writing custom functions in the CRM).

To identify the culprit, I find it easiest to comment out blocks of code until the whole thing compiles and then figure out which of the commented blocks caused the issue.

Linker modules in Zoho CRM for many to many relationships

In Zoho CRM, it's relatively easy to set up a one to one or one to many relationship using lookup fields. E.g. Let's say you're configuring the CRM for a real estate firm and there's a one to one mapping between owners of a house (which we'll use the Contacts module for) and the property itself (which we'll set up in a custom module). Just add a lookup in the property module for Owner and hey presto, you have a one (owner) to many (properties) relationship.

But what about if it's many to many. For example, if instead of the people being the ones selling the house, we actually want to track interested buyers. We have a few options:
1. We could rename Products to Properties and use the inbuilt many to many feature in Leads which allows you to add unlimited products to a Lead. However, if you're not using the Products module, then this won't work. There's no way to set up the same functionality for custom modules or any module apart from Products.

2. We could add 50 separate fields to the property: "Buyer 1", "Buyer 2" but this is incredibly ugly and non-scalable.

3. Preferred option: use a linker module, e.g. "Expression of interest"

In our example, "Expression of interest" would have a lookup for "Potential Buyer" and "Property". As such, it would have a one (buyer/property) to many (expression of interest) relationship with the underlying modules and facilitate a many to many relationship between buyers and properties. Now you can have unlimited expressions of interest for a single property - many buyers can be interested in it and each buyer can be interested in multiple properties.

This is a flexible approach and one I've used in several client projects.

Setting up Chrome extensions with a free trial period


I've been working on chrome extensions recently and while most of the area is well documented and pretty easy to figure out, I found setting up a free trial period to be really difficult. I ended up engaging an expert to help and have documented the steps required to help other people.

1. Publish your app on the Chrome web store if you haven't already done so and set up free trial billing.

2. Setup an app in the Google dev console https://console.developers.google.com/ and obtain the client_id. Sub steps below:

a. Create the project
b. Enable "Chrome Web Store API" for this project
c. Create application using the ID from the extension in the chrome store and when they ask you to setup credentials, choose: "OAuth client ID"

3. Add this to manifest.json:

4. Add a key to the manifest. This allows you to test the extension locally in developer mode. You can get the key in the Chrome developer dashboard if you click on the "More info" for your extension.

The key will be something like this.
key: "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCDE+RMJo4W8/j80mUQ7RHXi0Mv5yCC5l4EuoxMmXFhS5ooXKFgi7MST7E7Vzw/BDFoCUXBgSln44eKyerCSwaU2MW+LFiULeHogPySvmTxzDh8dDAQdfy9u0QMN7Q/6pr5B4lAM8XvWBlnjSCZhLpk08KVkqsd8IzYdLpBCB3lBQIDAQAB",

Make sure you remove spaces before adding it to your manifest. (When you copy it from the dev dashboard, it has unnecessary whitespace on the right hand side).

5. Check that you have same id in the store and in the chrome://extensions tab for you testing copy.

6. Add code to check the licence (note that I'm hardcoding 14 days as the free trial period - change as desired):

This needs to go in as a background script in your manifest.json, e.g.
"background": {
        "scripts": ["checkLicenceKey.js"],
        "persistent": false
}
 
Then in your content script, you'll do something like this:
(You can see I've set up config for allowed email addresses for myself and for people who've paid offline.)

7. Profit!

Gotchas
Things I did wrong along the way:
1. Leaving whitespace in the key
2. Trying to test with the same account I used to set up the extension on the web store. This doesn't work. You'll need to test with a different account.

Saturday, September 3, 2016

Sending emails from a different address in Zoho CRM

Q: "I have a quick question. I want one of the other users to be able to send some email templates from our company address (info@...) and not from their user email. Is it possible to do it?"

Yes, it is possible. When you’re logged in to your Zoho CRM,  you can simply do the following:

1. Click the Emails Tab, then click on ‘Settings’ and Settings window will show up. 

2. At the left hand side of the window, choose ‘Send Mail as’ under the Personalize section, then click on the ‘Add from address’ button and the Add from address window will pop up. 

3. From that window, fill out the Display name and Email ID fields.

4. Choose Zoho SMTP then click on Send Verification tab. 

5. Check your email for the verification code.

6. Enter your code at the ‘Confirm Verification’ window then click on the ‘Confirm’ button. 

Here it is in animated GIF form: