function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mohammed AzarudeenMohammed Azarudeen 

Trigger for Converting Amount(Currency/Number) to Words

Has anyone else come up with a solution by Trigger for this? I have been trying to figure this out for a few weeks now but to no avail.

for e.g
 
Amount - 200$
Amount in words - Two hundred Dollar only.

I found apex class from this website http://http://salesforcewithkiran.blogspot.in/2013/05/number-to-words-in-apex.html

that works fine for apex class but not for apex trigger.

and I found another site https://sites.google.com/a/forcegenie.com/kb/home/numberstowords/7-ways-to-integrate-with-your-salesforce-org/continouos-conversion-from-number-currency-to-text-using-a-trigger with trigger program to convert but they calling a NumbersToWords apex class but there is no code for that class.

Need a Trigger to convert numbers/currency to words

Anyone have idea about this?

Thanks!
Anup JadhavAnup Jadhav
I have used a java code to do this obviously for a java based application. But you can take the code from here (http://www.rgagnon.com/javadetails/java-0426.html) and convert it to apex. Perhaps you can share it on github when it works for you. :-)
Mohammed AzarudeenMohammed Azarudeen
Ok answer for this question is found check this link http://salesforce.stackexchange.com/questions/97612/trigger-for-converting-amountcurrency-number-to-words/97615#97615

thanks @Keith C

Am just pasting the code written by @Keith C in Salesforce.Stackexchange.com

The class I referenced in question works on a single number that must be a positive Longvalue. So in trigger will have to loop over what can be the many (bulk) values passed to the trigger via the Trigger.new list of the trigger context, convert the value to a Long and then assign the resulting String. Because it is a before trigger, the result will automatically be saved to the database.

if it were a couple of custom fields on Contact, the code would look  like this:
trigger MyTrigger on Contact (before insert, before update) {
    for (Contact c : Trigger.new) {
        if (c.Number__c != null && c.Number__c >= 0) {
            Long n = c.Number__c.longValue();
            c.Text__c = NumberToWord.english_number(n);
        } else {
            c.Text__c = null;
        }
    }
}

(will also have to copy and add the NumberToWord class to org before creating the trigger.)
Rupal KumarRupal Kumar
hi,
try this code
 First You Create NumberToWord Class-
public with sharing class NumberToWord {  
      static String[] to_19 = new string[]{ 'zero', 'one',  'two', 'three', 'four',  'five',  'six',  
      'seven', 'eight', 'nine', 'ten',  'eleven', 'twelve', 'thirteen',  
      'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen' };  
    static String[] tens = new string[]{ 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'};  
    static String[] denom = new string[]{ '',  
      'thousand',   'million',     'billion',    'trillion',    'quadrillion',  
      'quintillion', 's!xtillion',   'septillion',  'octillion',   'nonillion',  
      'decillion',  'undecillion',   'duodecillion', 'tredecillion',  'quattuordecillion',  
      's!xdecillion', 'septendecillion', 'octodecillion', 'novemdecillion', 'vigintillion' };  
    // convert a value < 100 to English.    
   public static String convert_nn(integer val) {  
      if (val < 20)  
        return to_19[val];  
      if(val == 100)  
          return 'One Hundred';  
      for (integer v = 0; v < tens.size(); v++) {  
       String dcap = tens[v];  
        integer dval = 20 + 10 * v;  
        if (dval + 10 > val) {  
          if (Math.Mod(val,10) != 0)  
            return dcap + ' ' + to_19[Math.Mod(val,10)];  
          return dcap;  
        }      
      }  
      return 'Should never get here, less than 100 failure';  
    }  
    // convert a value < 1000 to english, special cased because it is the level that kicks   
    // off the < 100 special case. The rest are more general. This also allows you to  
    // get strings in the form of "forty-five hundred" if called directly.  
    public static String convert_nnn(integer val) {  
      String word = '';  
     integer rem = val / 100;  
      integer mod = Math.mod(val,100);  
      if (rem > 0) {  
        word = to_19[rem] + ' hundred';  
        if (mod > 0) {  
          word += ' ';  
        }  
      }  
      if (mod > 0) {  
        word += convert_nn(mod);  
      }  
      return word;  
    }  
    public static String english_number(long val) {  
      if (val < 100) {  
        return convert_nn(val.intValue());  
      }  
     if (val < 1000) {  
        return convert_nnn(val.intValue());  
      }  
      for (integer v = 0; v < denom.size(); v++) {  
        integer didx = v - 1;  
        integer dval = (integer)Math.pow(1000, v);  
        if (dval > val) {  
          integer mod = (integer)Math.pow(1000, didx);  
          integer l = (integer) val / mod;  
          integer r = (integer) val - (l * mod);  
          String ret = convert_nnn(l) + ' ' + denom[didx];  
          if (r > 0) {  
            ret += ', ' + english_number(r);  
          }  
          return ret;  
        }  
     }  
      return 'Should never get here, bottomed out in english_number';  
    }  
  }
Next Step is Create trigger-
trigger AmountToWords on Opportunity (before insert, before update) {


    for (Opportunity opp: Trigger.new) {
        if (opp.Amount!= null && opp.Amount >= 0) {
            Long n =opp.Amount.longValue();
            opp.AmountInWords__c = NumberToWord.english_number(n);
        } else {
           opp.AmountInWords__c = null;
        }
    }
}

Thanks

Rupal