Creating a Function

Creating a Function

When you have a piece of scripting code that you use frequently, it can be better to create a function for this. Instead of copying this piece of code (and having to update each and every instance when you need to change it!), you have it in one central place and can call it whenever you need it. 

Creating a function is quite easy. I'll explain based on a simple example: let's say you have a couple of forms that have some sort of Validation workflow, that checks whether a field that needs to be filled in is not empty. If it is empty, you want to show an Alert message, and you want that message to be consistent in those forms. We will put these messages in a Function.

When you are editing an app, take the following steps:
  1. Go to Workflows --> Functions
  2. Click "Create Function" or "New Function
  3. Fill in the fields. All of these, except for the language, can be changed later.

    1. Create a name for your function. This is also the name you are going to use when calling the function, so give it something descriptive and easy to recognize. In this case, I called it 'getErrorMessage'.
    2. Select the language. In this case, we leave it at Deluge, but you can also select Java or NodeJS.
    3. Select a namespace. You can leave it at Default, but if you're going to add a lot of functions, it is easier to group them in different namespaces. You can always add or change this later. I chose to add a new namespace called 'text'.
    4. Select the return type. This means the sort of variable you are going to be receiving when calling the function, or "void" if you don't need any feedback. In this case, we want to get a text, so we choose String.
    5. Then, we create arguments. In this case, I want to be able to get different results, based on which message I want to display. For instance, if I want to display a message for an address field that was not filled in, I want to get a different message than when I want a message for an empty lookup field. I want to say "address" or "lookup" as an argument, which is a string.
  4. Now that we've created the function, we need to write the script.
    You will get an empty function that looks like this:
  1. string text.getErrorMessage(string msg)
  2. {
  3. return "";
  4. }
The 'return' statement is very important: this is what is going to be sent back when you call the function from your workflow. For now, it's an empty string. It is important that the return is always the very last line in the script. It is also not allowed to have multiple returns. So you can't, for instance, have a list of If and If else statements with a return under every one of those. You should code like this:
  1. string text.getErrorMessage(string msg)
  2. {
  3. text = "";
  4. if ( msg == "address" ) 
  5.     {
  6. text = "Please fill in the address field!";
  7.     }
  8. else if ( msg == "lookup" ) 
  9.     {
  10. text = "Please select a value!" ;
  11.     }
  12. return text;
  13. }
Now that we have created the Function, save it. If we want to now call it from a workflow to show the alert message to the user, we can use the following format:
  1. alert thisapp.text.getErrorMessage("address");
or:
  1. alert thisapp.text.getErrorMessage("lookup");


    • Related Articles

    • Add a custom logo to an app

      Zoho Creator gives you a couple of options for the logo that is displayed in the top left corner of an app: Your company logo (the same for all your apps) An icon from the predefined icon list A 1-3 letter combination Now, you might have different ...
    • Create a European VAT ID checker (also works for Zoho CRM!)

      For invoicing to other businesses within the EU, it is mandatory to check whether the VAT ID of your customer is correct, in order to invoice using a 0% tax rate. I wrote a script that checks the VAT number that you have from your customer against ...
    • Sending images to imagekit.io using API

      imagekit.io is a service for optimising media content for websites. It's API is pretty straightforward, but it's always a bit finicky to get a connection going the way you want it. To make an API call, first follow the steps in "Authorisation" and ...