Jump to content
We've recently updated our Privacy Statement, available here ×

How can I return a variable to a report from a subreport?


aklingel

Recommended Posts

 I'm using iReport 3.7.6.

I'm trying to return a Collection of certain values that have been encountered in the subreport back to the parent report.

I have the Collection set up as a variable called "parts" in both the main report and the subreport.

Using the "Return Values" box in the Properties of the subreport object, I attempt to set up the destination variable and the subreport variable. The destination variable is no problem. But in the subreport variable dropdown box, the only options are:

PAGE_NUMBER
COLUMN_NUMBER
REPORT_COUNT
PAGE_COUNT
COLUMN_COUNT

My "parts" variable is nowhere to be seen. Why is that?

Anyway, it lets me type in the variable name. I test this by typing in a bad name and trying to run. Failed as expected, good.

So I type in "parts".

I compile and run the main report, runs just fine.

Except here is the problem. I have a scriptlet attached to the main report where I want to check the contents of that "parts" variable in the afterPageInit method. It is always null!

That means the assignment is not working. I am checking the values in both the subreport and the parent. The "parts" variable has a value in the subreport, but it is always null in the parent.  What am I doing wrong?
 

Code:
       // In the parent report scriptlet	@Override	public void afterPageInit() throws JRScriptletException {		Collection<String> parts = (Collection<String>)getVariableValue("parts");                // parts IS ALWAYS NULL!!!		if (parts!=null) {			System.out.println("parts size in parent is " + parts.size());						if (parts.size()>0) {				Iterator<String> iter = parts.iterator();				String page = getVariableValue("PAGE_NUMBER").toString();				System.out.println("page is " + page);				while (iter.hasNext()) {					System.out.println(iter.next());				}			}		}	}        // In the subreport scriptlet	@Override	public void afterDetailEval() throws JRScriptletException {				Set parts = (Set<String>) getVariableValue("parts");		String part = "";				// Grab all of the parts and put them in a set		for (int i=1; i <= 15; i++) {			try {				part = getFieldValue("Part" + i).toString();				if ((part==null) || (part=="")) {					continue;				}				System.out.println("Adding " + part + " to set.");				parts.add(part);				} catch (Exception e) {				// There is no such part, no need to check the rest				break;			}		}		setVariableValue("parts",(Collection)parts);		System.out.println("parts size in detail is " + parts.size());               // parts is NEVER null!			}
Link to comment
Share on other sites

  • Replies 1
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

On Part A of your question, I recall a previous thread which talked about this and something to the effect that the main report will not list the custom variables you created in a subreport and will only list the system defined parameters, not really an issue, as you found just type the name of the subreport variable and as long as you typed the name correctly it will work just fine.

On Part B about the variable returned always being null.  I have run into this in the past and noticed that if you set the calculationtype to "system" for the variable definition on the main report it returns fine.  Whether this will correct your issue or what the technical reason behind this would be I can't answer but give it a try and see it it helps.

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...