Using a Report Custom Function

Saturday 26 October 2013 @ 4:56 pm

I did something for the first time this week. I wrote a report custom function to use in production report.  I think of Custom Functions as being primarily useful if you have a repository and can use it once across reports.  I haven’t run into a situation where a single report merits having its own function. But I found a perfect example in a recent project.  In this report we need to convert total seconds into a string that looks like 12:45 [hh:mm]. The formula is easy and is even posted on my “formulas” page. But by the time the report was done we had used the same logic in 140 different formulas. And after that we discovered that our logic had to also account for negative times. This meant changing a whole bunch of formulas. It was at that point that I decided to write it as a function and update all the formulas to use the function.  That way, if we found another improvement, we will only have to make the change in one place.

And now that it is done we can transfer the function to other reports that need the same logic. Even without a repository it is a worthwhile.  It makes the formulas much cleaner.  This is the function:

Function (numberVar TotalSec)
NumberVar Hours := Truncate (Abs( TotalSec / 3600)) ;
NumberVar Minutes := Truncate (Remainder ( TotalSec , 3600) / 60) ;

 (if Totalsec < 0 then '-' else '') +
Totext ( Hours , '00' ) + ':' +
Totext ( Abs(Minutes) , '00' )

And here is what it looks like when used:

SecondsToTimeString ({@ActiveSeconds})

(For examples of my most popular formulas, please visit the FORMULAS page on my website.)

Leave a Reply

Recrystallize Pro