Invoice Grand Total in words.

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 function helps you achieve just that with the click of a button.

Getting started with the function:

  1. Go to Setup > Automation > Actions > Functions > Configure Function > Write your own.
  2. Provide a name for the function. For example: "Invoice Grand Total". Add a description(optional).
  3. Select the module to be associated asInvoice.
  4. Copy the code given below.
  5. Click "Edit arguments".
  6. Enter the name as "invoiceId" and select the value as "Invoice Id".
  7. Click Save & Execute Script.
  8. Save the changes.

 

The Code: For V1 API - Old Editor:

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;

The Code: For V2 API - DRE Editor:

invoiceDetails = zoho.crm.getRecordById("Invoices",
input.invoiceId.toLong()); 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.update("Invoices", invoiceId.toLong(), mp);
info mp;
info update;

Note:

  • The code is zoho.crm._getRecordById for Version 1.0 of.
  • 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 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!

Return to Tips