Welcome back everyone!
Last week, we learnt how to add pre-defined notes in a click. This week, let's look at a custom function that helps you add the invoice grand total in words with the click of a button.
Business scenario:
Countries across the globe follow different numbering formats with comma and decimals being used interchangeably:
- In the US, comma (,) is used as thousands separators where as in Germany, period (.) is used.
- In the US, period (.) is used as decimal separator where as in Germany, comma (,) is used.
Thus, three thousand fifty-five and eight tenths is displayed as 3,025.8 in the US and 3.025,8 in Germany. For cheques, the practice of writing the amount in words not only helps avert malpractice, but also clears any discrepancies associated with number format. If you are dealing with multi-national clients, it is best to extend this practice to your client invoices, and this weeks custom function helps you achieve just that with the click of a button.
Getting started with the custom function:
- Go to Setup > Automation > Actions > Custom Functions > Configure Custom Function > Write your own .
- Provide a name for the button. For example: “Total in words”. Add a description(optional).
- Choose the module as " Invoice ".
- Click “ Free flow scripting ”.
- Copy the code given below.
- Click “ Edit arguments ”.
- Enter the name as “ invoiceId ” and select the value as “ Invoice Id ”.
- Save the changes.
- Click Save to create the button.
The script:
Code for Version 2.0 API:
invoiceDetails = zoho.crm.getRecordById("Invoices", input.invoiceId.toLong());
val_s = ifnull(invoiceDetails.get("Grand_Total"),"0").toLong();
//val_s=input.val.toString();
th = {"", "thousand", "million", "billion", "trillion"};
// uncomment this line for English Number System
// th = {"","thousand","million", "milliard","billion"};
dg = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
tn = {"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
tw = {"twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
x = val_s.length();
if (x > 15)
{
info "too big";
}
else
{
s = val_s.replaceAll("(?)",",",false).removeFirstOccurence(",").removeLastOccurence(",").toList();
str = "";
sk = 1;
bypass = false;
for each index i in s
{
cur = (s.get(i)).toLong();
info cur;
if (!bypass)
{
if (((x - i) % 3) == 2)
{
if (cur == 1)
{
next = (s.get((i + 1))).toLong();
info next;
str = (str + tn.get(next)) + " ";
bypass = true;
sk = 1;
}
else if (cur != 0)
{
str = (str + tw.get((cur - 2))) + " ";
info str;
sk = 1;
}
}
else if (cur != 0)
{
str = (str + dg.get(cur)) + " ";
info str;
if (((x - i) % 3) == 0)
{
str = str + "hundred ";
sk = 1;
}
}
}
else
{
bypass = false;
}
if (((x - i) % 3) == 1)
{
if (sk != 0)
{
str = (str + th.get(floor(((x - i - 1) / 3)))) + " ";
info str;
sk = 0;
}
}
}
info str;
}
upperstr = str.toUpperCase();
mp = map();
mp.put("Grand_Total_in_Words", upperstr);
update = zoho.crm.update("Invoices", invoiceId.toLong(), mp);
info mp;
info update;
Code for Version 1.0 API:
invoiceIdStr = input.invoiceId.toString();
invoiceDetails = zoho.crm.getRecordById("Invoices", input.invoiceId);
val_s = ifnull(invoiceDetails.get("Grand Total"),"0");
//val_s=input.val.toString();
th = {"", "thousand", "million", "billion", "trillion"};
// uncomment this line for English Number System
// th = {"","thousand","million", "milliard","billion"};
dg = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
tn = {"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
tw = {"twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
x = val_s.length();
if (x > 15)
{
info "too big";
}
else
{
s = val_s.replaceAll("(?)",",",false).removeFirstOccurence(",").removeLastOccurence(",").toList();
str = "";
sk = 1;
bypass = false;
for each index i in s
{
cur = (s.get(i)).toLong();
info cur;
if (!bypass)
{
if (((x - i) % 3) == 2)
{
if (cur == 1)
{
next = (s.get((i + 1))).toLong();
info next;
str = (str + tn.get(next)) + " ";
bypass = true;
sk = 1;
}
else if (cur != 0)
{
str = (str + tw.get((cur - 2))) + " ";
info str;
sk = 1;
}
}
else if (cur != 0)
{
str = (str + dg.get(cur)) + " ";
info str;
if (((x - i) % 3) == 0)
{
str = str + "hundred ";
sk = 1;
}
}
}
else
{
bypass = false;
}
if (((x - i) % 3) == 1)
{
if (sk != 0)
{
str = (str + th.get(floor(((x - i - 1) / 3)))) + " ";
info str;
sk = 0;
}
}
}
info str;
}
upperstr = str.toUpperCase();
mp = map();
mp.put("Grand Total (in Words)", upperstr);
update = zoho.crm.updateRecord("Invoices", invoiceIdStr, mp);
info mp;
info update;
This is how the result looks like:
Note:
- Create a custom field named 'Grand Total (in Words)' in your invoice module. Note that this is part of the code (mp.put("Grand Total (in Words)", upperstr);). Just in case you use a different field name, update the code accordingly.
- The above code adds the grand total in words in your invoice record.
- Use merge fields to reflect the same in the invoice templates to be sent to your clients
- This custom function handles up to 15 digits and rounds-off to the nearest whole number.
Found this useful? Try it out and let me know how it works! If you have questions, do not hesitate to ask! Share this with your team if you find it useful. Do check out other custom functions shared in this serieshere.
See you all next week with another interesting custom function. Ciao!
Update: As you must be aware, API V1.0 will be deprecated and support for version 1.0 API will be available only till Dec 31, 2018. Version 1.0 compatible Functions will continue to work until Dec 31, 2019. You're advised to migrated to API Version 2.0 at the earliest. Check this announcement for more. We've updated the post to include the Version 2.0 compatible Function.