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
Adam RycroftAdam Rycroft 

Help with test class for trigger to change currency field to words

Hi,

I found this apex class that does what I need it to and wrote the trigger which works great in the sandbox. However, I'm at a complete loss as to how I would even start writing a test class for this. Please help!
trigger convertNumbersToWords on Grant__c (before Insert, Before Update) { 
   NumberTOWordConvertion ntoWord = new NumberTOWordConvertion();
    For(Grant__c T : trigger.new){
        if(T.Amount__c>0){

         String numbertoWord = ntoWord.getNumberTOWordConvertion(T.Amount__c);
         T.Text_amount__c = numbertoWord;
          
        }
    }
}
 
public class NumberTOWordConvertion {

    // Call this method with Number to convert
    public String getNumberTOWordConvertion(Decimal num) {

        Decimal junkVal = num;
        Decimal junkValCents = junkVal - Math.floor(junkVal);
        junkVal = Math.floor(junkVal);

        String obStr = junkVal.toPlainString();
        String[] numReversed = obStr.split('');
        String[] actnumber = reverse(numReversed);
        String firstHalf = convertInWords(numReversed, actnumber);

        Integer tmp = Math.round(junkValCents * 100);
        junkValCents = (Decimal)tmp / 100; System.debug('jj :' + junkValCents);
        String CentsStr = junkValCents.toPlainString();
        String secondHalf;
        if (CentsStr == '0') {
            secondHalf = '';
        } else if (CentsStr.length() != 4) {
            CentsStr = CentsStr + '0';
            CentsStr = CentsStr.substring(2);
            String [] numReversedCents = CentsStr.split('');
            String[] actnumberCents = reverse(numReversedCents);
            secondHalf = convertInWords(numReversedCents, actnumberCents);
        } else {
            CentsStr = CentsStr.substring(2);
            String [] numReversedCents = CentsStr.split('');
            String[] actnumberCents = reverse(numReversedCents);
            secondHalf = convertInWords(numReversedCents, actnumberCents);
        }

        String SumOFHalves = '';

        if (secondHalf.length() > 4) {
            firstHalf = firstHalf.replace('Only', 'Dollars And ');
            secondHalf = secondHalf.replace('Only', 'Cents Only');
            SumOFHalves = firstHalf + secondHalf;
        } else {
            firstHalf = firstHalf.replace('Only', 'Dollars Only');
            SumOFHalves = firstHalf;
        }

        // IF amount has any value
        if (SumOFHalves.length() > 5) {
            return SumOFHalves;
        } else {
            return '';
        }
    }
    // Method reverse the number
    public List<String> reverse(List<String> strToRev) {
        List<String> revList = new List<String>();
        for (Integer i = strToRev.size() - 1; i >= 0; i--) {
            revList.add(strToRev.get(i));
        }
        revList.add('');
        return revList;
    }

    public String convertInWords(String[] numRev, String[] actnum) {
        List<String> iWords = new List<String> {'Zero', ' One', ' Two', ' Three', ' Four', ' Five', ' Six', ' Seven', ' Eight', ' Nine'};
        List<String> ePlace = new List<String> {' Ten', ' Eleven', ' Twelve', ' Thirteen', ' Fourteen', ' Fifteen', ' Sixteen', ' Seventeen', ' Eighteen', ' Nineteen'};
        List<String> tensPlace = new List<String> {'dummy', ' Ten', ' Twenty', ' Thirty', ' Forty', ' Fifty', ' Sixty', ' Seventy', ' Eighty', ' Ninety' };

        Integer iWordsLength = numRev.size();
        String totalWords = '';
        List<String> inWords = new List<String>();
        for (Integer k = 0; k < iWordsLength; k++) {
            inWords.add('');
        }
        String finalWord = '';
        Integer j = 0;

        // Main For loop
        for (Integer i = 0; i < iWordsLength; i++) {

            if (i == 0) {
                if (actnum[i] == '0' || actnum[i + 1] == '1') {
                    inWords[j] = '';
                } else {
                    inWords[j] = iWords[Integer.valueof(actnum[i])];
                }
                inWords[j] = inWords[j] + ' Only';
            } else if (i == 1) {

                if (actnum[i] == '0') {
                    inWords[j] = '';
                } else if (actnum[i] == '1') {
                    inWords[j] = ePlace[Integer.valueof(actnum[i - 1])];
                } else {
                    inWords[j] = tensPlace[Integer.valueof(actnum[i])];
                }
            } else if (i == 2) {
                if (actnum[i] == '0') {
                    inWords[j] = '';
                } else if (actnum[i - 1] != '0' && actnum[i - 2] != '0') {
                    inWords[j] = iWords[Integer.valueof(actnum[i])] + ' Hundred and';
                } else {
                    inWords[j] = iWords[Integer.valueof(actnum[i])] + ' Hundred';
                }
            } else if (i == 3) {
                if (actnum[i] == '0' || actnum[i + 1] == '1') {
                    inWords[j] = '';
                } else {
                    inWords[j] = iWords[Integer.valueof(actnum[i])];
                }
                if (actnum[i + 1] != '0' || Integer.valueof(actnum[i]) > 0) {
                    inWords[j] = inWords[j] + ' Thousand';
                }
            } else if (i == 4) {

                if (actnum[i] == '0') {
                    inWords[j] = '';
                } else if (actnum[i] == '1') {
                    inWords[j] = ePlace[Integer.valueof(actnum[i - 1])];
                } else {
                    inWords[j] = tensPlace[Integer.valueof(actnum[i])];
                }

            } else if (i == 5) {
                if (actnum[i] == '0' || actnum[i + 1] == '1') {
                    inWords[j] = '';
                } else {
                    inWords[j] = iWords[Integer.valueof(actnum[i])];
                }
                if (actnum[i + 1] != '0' || Integer.valueof(actnum[i]) > 0) {
                    inWords[j] = inWords[j] + ' Hundred Thousand';
                }
            } else if (i == 6) {

                if (actnum[i] == '0') {
                    inWords[j] = '';
                } else if (actnum[i] == '1') {
                    inWords[j] = ePlace[Integer.valueof(actnum[i - 1])];
                } else {
                    inWords[j] = tensPlace[Integer.valueof(actnum[i])];
                }

            } else if (i == 7) {
                if (actnum[i] == '0' || actnum[i + 1] == '1' ) {
                    inWords[j] = '';
                } else {
                    inWords[j] = iWords[Integer.valueof(actnum[i])];
                }
                inWords[j] = inWords[j] + ' Ten Million';
            } else if (i == 8) {

                if (actnum[i] == '0') {
                    inWords[j] = '';
                } else if (actnum[i] == '1') {
                    inWords[j] = ePlace[Integer.valueof(actnum[i - 1])];
                } else {
                    inWords[j] = tensPlace[Integer.valueof(actnum[i])];
                }

            }

            j++;
        }
        // End of For loop

        // Reverse the List
        inWords = reverse(inWords);

        for (Integer i = 0; i < inWords.size(); i++) {
            finalWord += inWords[i];
        }

        return finalWord;
    }


}

 
Best Answer chosen by Adam Rycroft
AnkaiahAnkaiah (Salesforce Developers) 
Hi Adam,

Then you need to insert the test data for account in the test class.

try with below code.
@istest
public class convertNumbersToWordsTest {
    
    static testmethod void convertNumbersToWordsmethod1(){
	  
	  //insert the mandatory fields data for Grant__c
	  Account acc = new account();
	  acc.name = 'test';
	  insert acc;
	
        //insert the mandatory fields data for Grant__c
        Grant__c GT = new Grant__c();
        GT.Name = 'test GT';
        GT.amount__c = 1000;
        GT.Account__c = acc.id;        
        insert GT;
		
        decimal num = GT.amount__c;
        NumberTOWordConvertion nm = new NumberTOWordConvertion();
        
       nm.getNumberTOWordConvertion(num);
        
    }
    
    static testmethod void convertNumbersToWordsmethod2(){
	
		//insert the mandatory fields data for Grant__c
		  Account acc = new account();
		  acc.name = 'test';
		  insert acc;
	
        //insert the mandatory fields data for Grant__c
        Grant__c GT = new Grant__c();
        GT.Name = 'test GT';
        GT.amount__c = 1000000;
		GT.Account__c = acc.id;        
        insert GT;
		
        decimal num = GT.amount__c;
        NumberTOWordConvertion nm = new NumberTOWordConvertion();
        
       nm.getNumberTOWordConvertion(num);
        
    }
    
        static testmethod void convertNumbersToWordsmethod3(){
		
       //insert the mandatory fields data for Grant__c
		Account acc = new account();
		acc.name = 'test';
		insert acc;
        
		//insert the mandatory fields data for Grant__c
        Grant__c GT = new Grant__c();
        GT.Name = 'test GT';
        GT.amount__c = 9999;
		GT.Account__c = acc.id;        
        insert GT;
		
        decimal num = GT.amount__c;
        NumberTOWordConvertion nm = new NumberTOWordConvertion();
        
       nm.getNumberTOWordConvertion(num);
        
    }
    
 static testmethod void convertNumbersToWordsmethod4(){
 
 		//insert the mandatory fields data for Grant__c
		  Account acc = new account();
		  acc.name = 'test';
		  insert acc;
 
       //insert the mandatory fields data for Grant__c 
        Grant__c GT = new Grant__c();
        GT.Name = 'test GT';
        GT.amount__c = 10;
        GT.Account__c = acc.id;       
        insert GT;
		
        decimal num = GT.amount__c;
        NumberTOWordConvertion nm = new NumberTOWordConvertion();
        
       nm.getNumberTOWordConvertion(num);
        
    }
    
  static testmethod void convertNumbersToWordsmethod5(){
  
		//insert the mandatory fields data for Grant__c
		  Account acc = new account();
		  acc.name = 'test';
		  insert acc;  
  
        //insert the mandatory fields data for Grant__c
        Grant__c GT = new Grant__c();
        GT.Name = 'test GT';
        GT.amount__c = 888888888;
        GT.Account__c = acc.id;
        insert GT;
		
        decimal num = GT.amount__c;
        NumberTOWordConvertion nm = new NumberTOWordConvertion();
        
       nm.getNumberTOWordConvertion(num);
        
    }

}

If this helps, Please mark it as best answer.

Thanks!!​​​​​​​
 

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi adam,

try with below code getting 81% coverage of apex class & 100% of the trigger.
@istest
public class convertNumbersToWordsTest {
    
    static testmethod void convertNumbersToWordsmethod1(){
	
        //insert the mandatory fields data for Grant__c
        Grant__c GT = new Grant__c();
        GT.Name = 'test GT';
        GT.amount__c = 1000;
        GT.CloseDate = system.today()+5;
        GT.StageName = 'Qualification';
        
        insert GT;
        decimal num = GT.amount__c;
        NumberTOWordConvertion nm = new NumberTOWordConvertion();
        
       nm.getNumberTOWordConvertion(num);
        
    }
    
    static testmethod void convertNumbersToWordsmethod2(){
	
        //insert the mandatory fields data for Grant__c
        Grant__c GT = new Grant__c();
        GT.Name = 'test GT';
        GT.amount__c = 1000000;
        GT.CloseDate = system.today()+5;
        GT.StageName = 'Qualification';
        
        insert GT;
        decimal num = GT.amount__c;
        NumberTOWordConvertion nm = new NumberTOWordConvertion();
        
       nm.getNumberTOWordConvertion(num);
        
    }
    
        static testmethod void convertNumbersToWordsmethod3(){
        
		//insert the mandatory fields data for Grant__c
        Grant__c GT = new Grant__c();
        GT.Name = 'test GT';
        GT.amount__c = 9999;
        
        insert GT;
        decimal num = GT.amount__c;
        NumberTOWordConvertion nm = new NumberTOWordConvertion();
        
       nm.getNumberTOWordConvertion(num);
        
    }
    
 static testmethod void convertNumbersToWordsmethod4(){
 
       //insert the mandatory fields data for Grant__c 
        Grant__c GT = new Grant__c();
        GT.Name = 'test GT';
        GT.amount__c = 10;
        GT.CloseDate = system.today()+5;
        GT.StageName = 'Qualification';
        
        insert GT;
        decimal num = GT.amount__c;
        NumberTOWordConvertion nm = new NumberTOWordConvertion();
        
       nm.getNumberTOWordConvertion(num);
        
    }
    
  static testmethod void convertNumbersToWordsmethod5(){
  
        //insert the mandatory fields data for Grant__c
        Grant__c GT = new Grant__c();
        GT.Name = 'test GT';
        GT.amount__c = 888888888;
        GT.CloseDate = system.today()+5;
        GT.StageName = 'Qualification';
        
        insert GT;
        decimal num = GT.amount__c;
        NumberTOWordConvertion nm = new NumberTOWordConvertion();
        
       nm.getNumberTOWordConvertion(num);
        
    }

}

If this helps, Please mark it as best answer.

Thanks!!
Adam RycroftAdam Rycroft
Thank you! - I think i'm almost there. One thing I forgot to mention is that the grant__c custom object is the child in a master-detail relationship to the account. Since the account is a required field for the grant record, how can i reference its ID in this code?
AnkaiahAnkaiah (Salesforce Developers) 
Hi Adam,

Then you need to insert the test data for account in the test class.

try with below code.
@istest
public class convertNumbersToWordsTest {
    
    static testmethod void convertNumbersToWordsmethod1(){
	  
	  //insert the mandatory fields data for Grant__c
	  Account acc = new account();
	  acc.name = 'test';
	  insert acc;
	
        //insert the mandatory fields data for Grant__c
        Grant__c GT = new Grant__c();
        GT.Name = 'test GT';
        GT.amount__c = 1000;
        GT.Account__c = acc.id;        
        insert GT;
		
        decimal num = GT.amount__c;
        NumberTOWordConvertion nm = new NumberTOWordConvertion();
        
       nm.getNumberTOWordConvertion(num);
        
    }
    
    static testmethod void convertNumbersToWordsmethod2(){
	
		//insert the mandatory fields data for Grant__c
		  Account acc = new account();
		  acc.name = 'test';
		  insert acc;
	
        //insert the mandatory fields data for Grant__c
        Grant__c GT = new Grant__c();
        GT.Name = 'test GT';
        GT.amount__c = 1000000;
		GT.Account__c = acc.id;        
        insert GT;
		
        decimal num = GT.amount__c;
        NumberTOWordConvertion nm = new NumberTOWordConvertion();
        
       nm.getNumberTOWordConvertion(num);
        
    }
    
        static testmethod void convertNumbersToWordsmethod3(){
		
       //insert the mandatory fields data for Grant__c
		Account acc = new account();
		acc.name = 'test';
		insert acc;
        
		//insert the mandatory fields data for Grant__c
        Grant__c GT = new Grant__c();
        GT.Name = 'test GT';
        GT.amount__c = 9999;
		GT.Account__c = acc.id;        
        insert GT;
		
        decimal num = GT.amount__c;
        NumberTOWordConvertion nm = new NumberTOWordConvertion();
        
       nm.getNumberTOWordConvertion(num);
        
    }
    
 static testmethod void convertNumbersToWordsmethod4(){
 
 		//insert the mandatory fields data for Grant__c
		  Account acc = new account();
		  acc.name = 'test';
		  insert acc;
 
       //insert the mandatory fields data for Grant__c 
        Grant__c GT = new Grant__c();
        GT.Name = 'test GT';
        GT.amount__c = 10;
        GT.Account__c = acc.id;       
        insert GT;
		
        decimal num = GT.amount__c;
        NumberTOWordConvertion nm = new NumberTOWordConvertion();
        
       nm.getNumberTOWordConvertion(num);
        
    }
    
  static testmethod void convertNumbersToWordsmethod5(){
  
		//insert the mandatory fields data for Grant__c
		  Account acc = new account();
		  acc.name = 'test';
		  insert acc;  
  
        //insert the mandatory fields data for Grant__c
        Grant__c GT = new Grant__c();
        GT.Name = 'test GT';
        GT.amount__c = 888888888;
        GT.Account__c = acc.id;
        insert GT;
		
        decimal num = GT.amount__c;
        NumberTOWordConvertion nm = new NumberTOWordConvertion();
        
       nm.getNumberTOWordConvertion(num);
        
    }

}

If this helps, Please mark it as best answer.

Thanks!!​​​​​​​
 
This was selected as the best answer
Adam RycroftAdam Rycroft
Thank you! I was close to getting to the right answer. Thank you so much for your help with this - I'm learning more as I go along too.