+ Start a Discussion
Sachin Prasad 16Sachin Prasad 16 

unable to save trigger

Hi All,

I have written a class as below:

public class NumbersToWordsConversionClass {     
public string wordText { get; set; }    
public Integer numberVal { get; set; }     
// Action method to be called by button or link    
 public void convert() {         wordText = numberToEnglish(numberVal);     }     static String[] firstTwenty = new String[] { 'NIL','One','Two','Three','Four','Five','Six','Seven','Eight','Nine',             'Ten','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen','Seventeen','Eighteen','Ninteen' };     static String[] tens = new String[] { '','','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety' };     static String[] powers = new String[] { 'Crore','Lakh','Thousand','Hundred' };     static Integer[] thresholds = new Integer[] { 10000000, 100000, 1000, 100 };     
static String[] convert(Integer value) {         
String[] result = new String[0];         
Boolean less20 = value < 20, less100 = value < 100;       
  if(less20) {             result.add(firstTwenty[value]);         }
 else if(less100) {            
 Integer tenValue = value / 10, oneValue = Math.mod(value, 10);             
result.add(tens[tenValue]);             
if(oneValue > 0) {                 
result.add(firstTwenty[oneValue]);             }         }       
  if(less100) {             return result;         }         if(value > 0) {           
  for(Integer index = 0; index < thresholds.size(); index++) {               
  if(thresholds[index] <= value) {                 
    result.addAll(convert(value / thresholds[index]));                
     result.add(powers[index]);                 
    value = Math.mod(value, thresholds[index]);                 }             }         
    if(value > 0) {                 result.addAll(convert(value));             }         }   
      return result;     }     public static String numberToEnglish(Integer value) {         return value != null && value >= 0? String.join(convert(value),' '): '';     } }

however when i am trying to  write the trigger as below it is failing please assist;
getting error as
Compile Error: expecting a right parentheses, found 'Value' at line 5 column 101

trigger ConvertCurrencyToWords on Opportunity (before insert, before update)
 { for(Opportunity record: Trigger.new)
  { if(record.Amount != null)
  {
   record.Amount_in_Words__c = NumbersToWordsConversionClass.numberToEnglish(record.Amount.‌​integer Value());
   }
   }
   }
Best Answer chosen by Sachin Prasad 16
Akshay_DhimanAkshay_Dhiman
Hi Sachin,

First you need to check your Amount_in_Words__c field on Opportunity is Text Type so then this can store String information and when you'll call numberToEnglish(Integer value) Method then you need to pass only Integer type value. So you need to convert decimal value (record.Amount) to Integer value before passing as a parameter.

Now I give you a solution as below :
trigger ConvertCurrencyToWords on Opportunity (before insert, before update){
  for(Opportunity record: Trigger.new){ 
      if(record.Amount != null){
      // Integer value as a parameter
           record.Amount_in_Words__c = NumbersToWordsConversionClass.numberToEnglish(Integer.valueOf(record.Amount)); 
         }
      }
  }
Regards
Akshay

All Answers

Hans LissHans Liss
@Sachin, there's something strange about the parameter you are trying to send to the numberToEnglish() method. Shouldn't that just be "record.Amount"? It looks like there's a space in there, too.
Sachin Prasad 16Sachin Prasad 16
@Hans Liss, i am trying to convert the amount field to words, i tried with record.amount still not working.
Hans LissHans Liss
Amount is a Decimal value and your method requires an Integer, so you need to cast it to an Integer:
            NumbersToWordsConversionClass.numberToEnglish((Integer)record.Amount);
I tried it and it seems to work fine.
Akshay_DhimanAkshay_Dhiman
Hi Sachin,

First you need to check your Amount_in_Words__c field on Opportunity is Text Type so then this can store String information and when you'll call numberToEnglish(Integer value) Method then you need to pass only Integer type value. So you need to convert decimal value (record.Amount) to Integer value before passing as a parameter.

Now I give you a solution as below :
trigger ConvertCurrencyToWords on Opportunity (before insert, before update){
  for(Opportunity record: Trigger.new){ 
      if(record.Amount != null){
      // Integer value as a parameter
           record.Amount_in_Words__c = NumbersToWordsConversionClass.numberToEnglish(Integer.valueOf(record.Amount)); 
         }
      }
  }
Regards
Akshay
This was selected as the best answer
Hans LissHans Liss
@Sachin, please remember to mark this as "Solved" and select a Best Answer if you get it working.
Sachin Prasad 16Sachin Prasad 16
Thanks @Hans and @Akshay this works. :)
Akshay_DhimanAkshay_Dhiman
Hi Sachin,
Thanks for selecting my answer as a best answer. It's my pleasure to help you! 

Regards,
Akshay