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
Sanjeev SFDCSanjeev SFDC 

Please help me to deply this trigger

Hi  I'm Trying  deploy this trigger but it is showing exception can any one will help in this
Trigger

  1. trigger convertNumbersToWords on Transaction__c(before update) {
  2.     String[] units=new String[]{'',' One',' Two',' Three',' Four',' Five',' Six',' Seven',' Eight',' Nine'};
  3.     String[] teen=new String[]{' Ten',' Eleven',' Twelve',' Thirteen',' Fourteen',' Fifteen',' Sixteen',' Seventeen',' Eighteen',' Nineteen'};
  4.     String[] tens=new String[]{' Twenty',' Thirty',' Fourty',' Fifty',' Sixty',' Seventy',' Eighty',' Ninety'};
  5.     String[] maxs=new String[]{'','',' Hundred',' Thousand',' Lakh',' Crore'};
  6.     For(Transaction__c T : trigger.new){
  7.     if(t.Net_Supplementary_Amount__c >0){
  8.       t.Net_Suppl_Interset_words__c =convert(integer.valueof(t.Net_Supplementary_Amount__c)) ;}
  9.        else{
  10.               t.Net_Suppl_Interset_words__c=' ';
  11.           }  
  12.       
  13.        t.Net_Supplementary_Brokerage_in_words__c =convert(integer.valueof(t.Net_Suppl_Brokerage__c)) ;
  14.        t.Net_Interest_till_31st_March_in_words__c =convert(integer.valueof(t.Interest_pre_31_Mar__c));
  15.        t.Net_Interest_post_31st_March_in_words__c =convert(integer.valueof(t.Intrst_post_31_Mar__c));
  16.        t.Net_Brokerage_Amt_in_Words__c =convert(integer.valueof(t.Net_Brokerage_INR__c));
  17.       
  18.     }
  19.   
  20.    
  21.     public string digitsToWord(string val){
  22.         String Convertedtext='';
  23.         list<string> Vallist=val.split('');
  24.                 string sss=Vallist.remove(0);
  25.         for(integer i=Vallist.size()-1;i>=0;i--){
  26.             integer index=integer.valueof(Vallist[i]);
  27.             if(i==0&&index>1&&Vallist.size()>1)
  28.                  Convertedtext=tens[index-2]+Convertedtext;
  29.             else if(i==0&&index==1&&Vallist.size()==2){
  30.                 integer sum=0;
  31.                 for(integer j=0;j<2;j++){
  32.                      sum=(sum*10)+integer.valueof(Vallist[j]);
  33.                 }
  34.                 return teen[sum-10]; 
  35.             }
  36.             else{
  37.                
  38.                  if(index>0)
  39.                      Convertedtext=units[index]+Convertedtext;
  40.             }
  41.         }
  42.         return Convertedtext;
  43.     }
  44.    
  45.    public String convert(integer n) {
  46.        if(n==0)
  47.            return 'Zero';
  48.         if(n>999999999)
  49.             return 'More then 100 corers ';
  50.        string amount=string.valueOf(n);
  51.       
  52.        integer positioning=1;
  53.        boolean IsHundred=false;
  54.        string Convertedtext='';
  55.        while(amount.length()>0){
  56.            if(positioning==1){
  57.                if(amount.length()>=2){
  58.                    String C=amount.substring(amount.length()-2,amount.length());
  59.                    amount=amount.substring(0,amount.length()-2);
  60.                    Convertedtext+=digitsToWord(C);
  61.                } else if(amount.length()==1){
  62.                     Convertedtext+=digitsToWord(amount);
  63.                     amount='';
  64.                }positioning++;
  65.            }
  66.            else if(positioning==2){
  67.                String C=amount.substring(amount.length()-1,amount.length());
  68.                amount=amount.substring(0,amount.length()-1);
  69.                if(Convertedtext.length()>0&&digitsToWord(C)!=''){
  70.                    Convertedtext=(digitsToWord(C)+maxs[positioning]+' and')+Convertedtext;
  71.                    IsHundred=true;
  72.                }
  73.                else{
  74.                    if(digitsToWord(C)!='')
  75.                      Convertedtext=(digitsToWord(C)+maxs[positioning])+Convertedtext;IsHundred=true;
  76.                }
  77.                positioning++;
  78.            }
  79.            else if(positioning>2){
  80.                if(amount.length()>=2){
  81.                    String C=amount.substring(amount.length()-2,amount.length()); amount=amount.substring(0,amount.length()-2);
  82.                    if(!IsHundred&&Convertedtext.length()>0)
  83.                        Convertedtext=digitsToWord(C)+maxs[positioning]+' and'+Convertedtext;
  84.                    else{ if(digitsToWord(C)=='') {} else Convertedtext=digitsToWord(C)+maxs[positioning]+Convertedtext; }
  85.                } else if(amount.length()==1){
  86.                    if(!IsHundred&&Convertedtext.length()>0)
  87.                        Convertedtext=digitsToWord(amount)+maxs[positioning]+' and'+Convertedtext; 
  88.                    else{ if(digitsToWord(amount)==''){}else Convertedtext=digitsToWord(amount)+maxs[positioning]+Convertedtext;   amount='';  }
  89.                   
  90.                }
  91.                 positioning++;
  92.            }
  93.         }
  94.       
  95.    return Convertedtext;
  96.    }
  97.    
  98. }
  99.  
  100.  
  101.  
  102.  

Failure Message: "System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, convertNumbersToWords: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.convertNumbersToWords: line 55, column 1 Trigger.convertNumber...
 
Prabu MahalingamPrabu Mahalingam
Hi Sanjeev,
Is the error thrown only during deployment.?

If so I think some of your test calss is faling.

Let me know.

Sanjeev SFDCSanjeev SFDC
but it is failng in all the prevous  test classes on this object
Prabu MahalingamPrabu Mahalingam
Hi Sanjeev SFDC,

From the code I understand that the issue is because of the variable amount in line 55.

What actually happens is the amount variable some how assigned null. so when you use length() method on this null value it throws that error.

so to fix this issue add a if condition before while loop


line 54:  If(amount!=null)   // New line
line 55:   while(amount.length()>0){


after this change run all the test calsses and try deploying it.

Thanks
Sanjeev SFDCSanjeev SFDC
thanks prabhu its working
Prabu MahalingamPrabu Mahalingam
Welcom Sanjeev SFDC,

Can you Mark it as best asnwer so that it would be helpful for others?