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

Casting null currency or decimal fields to 0 in APEX??

Is there an easy way to cast any null decimal/currency values of null to 0.00 so as to avoid a null pointer exception?


keepOLI.previous_price__c = keepOLI.Previous_price__c + i.Previous_price__c;


I want to say that if i.Previous_Price__c == null then cast to 0 without having to write an if statement for this, and for every other number that might come up null in the class.


I also want to avoid changing the Previous_price__c to default 0 and then going back and setting millions of null records to 0.


Is there a trick I'm missing here?


Or should I just write something like this


public long ifnull(long num ) {
if (num == null) num = 0;
return num;

 and then change my code to be this


keepOLI.previous_price__c = ifnull(keepOLI.Previous_price__c) + ifnull(i.Previous_price__c);





Yes, that method is the only solution.