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

System.TypeException: Invalid decimal: null


I am getting the error System.TypeException: Invalid decimal: null after running a test class. The test class (and others) that is failing is inserting an Account and the error is referencing the line a.Annualized_ACV__c = Decimal.valueOf(str); in the snippet of my before update Account trigger code below:

for(Account a :{         
    for (AggregateResult ar : lstOpptyAnnualACV){             
       if(lstOpptyAnnualACV != NULL){                   
          String str = '' + lstOpptyAnnualACV[0].get('sum');                 
          a.Annualized_ACV__c = Decimal.valueOf(str);                           

Any advice on how to fix this? Note the trigger code is working fine and is updating the Annualized_ACV__c correctly through the UI. The error occurs when running test classes that are inserting Accounts

Balaji BondarBalaji Bondar

You can add null check before typecase to make sure that exception will not occer:
for(Account a :{         
    for (AggregateResult ar : lstOpptyAnnualACV){             
       if(lstOpptyAnnualACV != NULL){                   
          String str = '' + lstOpptyAnnualACV[0].get('sum');                 
          if(str != Null)
            a.Annualized_ACV__c = Decimal.valueOf(str);                           
Important :
If this is what you were looking for then please mark it as a "SOLUTION" or You can Click on the "Like" Button if this was beneficial for you.

Thanks for your resonse. I tried your suggestion with the null check but the error still persists. I think the error I am getting is similar to the one discussed at however my error is not shown in the VF page as that post mentions but when running a test class.

Any other code suggestions?