Ask Reuben
Slightly Different Reports
How can I generate a report twice with a simple variation on the second report i.e printing OFFICE COPY on the second copy?
How can I use an environment variable in my report?
When producing documents, sometimes you need to generate multiple copies, and you need to annotate each copy so that you know which is the official document or if it is a copy. This should not take two or more .4rp files, nor should it require hitting the database twice. In a report sometimes you may have a need to include an environment variable in either the output or as part of the logic. The solution to both involves the same technique. At runtime when your report is produced you can read an environment variable by using a method getEnvironmentVariable that is part of the Runtime class you can include in an RTL expression. Using the Reports project in the Genero Studio Tutorial and Samples, you can very quickly modify OrderReport.4rp to add a WordBox in the top right corner of the Page Header …
… and then in the Text property of the new Word Box, enter an RTL expression using the Runtime.getEnvironmentVariable method. In the example I have used
Runtime.getEnvironmentVariable("MY_OFFICECOPY")=="COPY"?"OFFICE COPY":""
… which says if the Environment Variable “MY_OFFICECOPY” has a value “COPY” then the Text property of this WordBox is “OFFICE COPY”. If it isn’t then the text property is blank.
To quickly set the environment variable at runtime…
- right-click on the Application Node
- select Advanced Properties
- select Environment variables
- add a User variable with the appropriate name and value.
In this case, the name is “MY_OFFICECOPY” and the value is “COPY” to match the logic in my RTL expression.
Now when you run the report, you should see the result of the RTL expression reading the environment variable in your report. In this instance the words OFFICE COPY appearing in the top right corner.
What environment variables you use and what values your logic uses are upto you. The important thing to note is that you can get the environment field value by using the Runtime class in your RTL. I would encourage you to note the other methods that are also available in this class.
At the beginning I said you can produce two slightly different copies without hitting your data twice. The quick example I went through above was going through a REPORT each time. To hit the database once, you would use fgl_report_createProcessLevelDataFile to hit the database once and produce a data file, and then call fgl_report_runReportFromProcessLevelDataFile twice using this same data file. You would change the value of the environment variable (if necessary using FGL_SETENV()) as required between each running of the report. If you have not seen that concept of generating and creating multiple reports from the same data file, have a read here.