Replacing null value with 0 in onCreate method

jverly01
edited February 11, 2022 in Analytics #1
I'm using some simple java to override the value of an aggregate 'SUM' in the onCreate method:<br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>//Code for sub-report totals
importPackage(Packages.java.lang);
var lastValue = this.getValue();
reportContext.setPersistentGlobalVariable("tot_workhours", new Float(lastValue));</pre>
<br />
But there are occasions where the field I'm aggregating on will not have any values (e.g. the mechanic didn't work last week), so the summation is null. I've tried different ways to add some testing on the value, but I'm just not getting it. <br />
<br />
Any ideas on how to replace a null value within the code above with a 0 value?

Comments

  • kclark
    kclark E
    edited December 31, 1969 #2
    What else have you tried? Do you need it to have a 0 value? If it's just for display reasons you can solve that using something like<br />
    <br />
    <pre class='_prettyXprint _lang-auto _linenums:0'>
    if(this.value == null) {
    this.value = "0"
    }
    </pre>
    Warning No formatter is installed for the format ipb
  • jverly01
    edited November 13, 2012 #3
    --removed due to duplication
  • jverly01
    edited December 31, 1969 #4
    Yes the value needs to be zero. I used the same statement you referenced and the code looked like this:<br />
    <br />
    <pre class='_prettyXprint _lang-auto _linenums:0'>importPackage(Packages.java.lang);
    if(this.value == null) {
    this.value = "0"
    }
    var lastValue = this.getValue();
    reportContext.setPersistentGlobalVariable("tot_workhours", new Float(lastValue));</pre>
    <br />
    But that doesn't appear to work either. I'm wondering if I should test for null values on the field I'm trying to aggregate against so the aggregate function either has a series of values to evaluate or a zero.
  • jverly01
    edited December 31, 1969 #5
    FIXED - The solution was using the method I showed above and then testing for NULL data in the denominator of the calculated field. This alleviated the need to do a NULL to zero value conversion on both fields and gave the roll down total to work.

    Thanks for everyone's help.