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

Variable Expression issue


mmedici

Recommended Posts

 I have a variable expression which is subtracting two numbers.  I want to modify this report so that it only displays the negative answer.  meaning I only want to see the resulting answers which are a negative number.  Anyone have any suggestions?  

Code:
	<variable name="STK_RTOT" class="java.lang.String" resetType="Group" incrementType="Group" incrementGroup="SOHNUM_0" resetGroup="ITMREF_0" calculation="Sum">		<variableExpression><![CDATA[$F{TOTSTK} - $V{STK_CNT}]]></variableExpression>		<initialValueExpression><![CDATA[0]]></initialValueExpression>	</variable>
Link to comment
Share on other sites

  • Replies 18
  • Created
  • Last Reply

Top Posters In This Topic

 If I understood you correctly, you want to do something like what I have put in the code below.

It should print the number if less than 0, otherwise print ""

It uses the short if this?then do this:else do this syntax.

I hope that helps.

Code:
($F{TOTSTK} - $V{STK_CNT} < 0)?$F{TOTSTK} - $V{STK_CNT}:""
Link to comment
Share on other sites

 When i try and insert that printwhenexpression i get an error of:

 

Error loading the report template:

Org.xml.sax.SAXParseException:cvc-complex-type.2.4.a: Invalid content was found starting with the element 'printWhenExpression'. One '{variableExpression, initialValueExpression}' is expected. 

 

any thoughts on this error?

Link to comment
Share on other sites

 What I was recommending was actually bypassing the printWhenExpression. Please review the code below and see if it works. There isn't anything wrong with printWhenExpression, but because of your mentioning your beginning with this, I meant to give you a string to replace your existing string with.

printWhenExpression needs to return evaluate to true/false, and is like a switch that enables printing or not printing. What I gave to you always prints, just it prints nothing if the result is 0 or greater.

I hope this helps.

Cheers,

CHgsd

Code:
	<variable name="STK_RTOT" class="java.lang.String" resetType="Group" incrementType="Group" incrementGroup="SOHNUM_0" resetGroup="ITMREF_0" calculation="Sum">		<variableExpression><![CDATA[($F{TOTSTK} - $V{STK_CNT} < 0)?$F{TOTSTK} - $V{STK_CNT}:""]]></variableExpression>		<initialValueExpression><![CDATA[0]]></initialValueExpression>	</variable>
Link to comment
Share on other sites

This worked really well.  However, is there a way for it not to show the rows that are positive?

 

meaning, it ran the report and shows an anser for all the items which are negative, and deosn't show anything if it is positive.  However it shows the row, is there a way to remove the row or item if the answer is negative?

Link to comment
Share on other sites

 Anyone have any other suggestions on this?

 

i think a printwhenexpression may be the answer i am looking for, problem is when i google how to use the expression i don't see much information out there on it.

Anyone know how to make that expression work in this instance? 

Link to comment
Share on other sites

If it can wait a few hours I can write the block of code for you, having the full report wouldn't hurt, but just the code for the element in question and perhaps surrounding elements would suffice.

I can get to this in probably 4-6hours.

Link to comment
Share on other sites

 I am including below the relevant portion of your jrxml file. This should do what you want by only printing the textField if $V{STK_RTOT} is less than 0. If it evaluates to 0 or greater, the textfield should be absent.

Code:
				<textField>					<reportElement x="610" y="1" width="96" height="18">						<printWhenExpression><![CDATA[$V{STK_RTOT}<0]]></printWhenExpression>					</reportElement>					<textElement textAlignment="Center" verticalAlignment="Middle"/>					<textFieldExpression class="java.lang.String"><![CDATA[$V{STK_RTOT}]]></textFieldExpression>				</textField>
Link to comment
Share on other sites

The problem lies not in what I gave you, but what you already had. You are casting that variable as a string, when it is not.

Please change the class="java.lang.String" in the block below to something more appropriate, most likely java.math.BigDecimal in your case. I suspect that will solve your problem.

Cheers,

CHgsd

Code:
	<variable name="STK_RTOT" class="java.lang.String" resetType="Group" incrementType="Group" incrementGroup="SOHNUM_0" resetGroup="ITMREF_0" calculation="Sum">		<variableExpression><![CDATA[$F{TOTSTK} - $V{STK_CNT}]]></variableExpression>		<initialValueExpression><![CDATA[0]]></initialValueExpression>	</variable>
Link to comment
Share on other sites

When you say "item" what do you mean. The textField will not be printed with the block I gave to you.

If you mean some parent element you need to use that same printWhenExpression for the given "item". For example if you do not want the detail band printed when that condition is met, put the printWhenExpression in the detail band.

Does that make more sense?

Cheers,

CHgsd

Link to comment
Share on other sites

 Ok, i put the printwhenexpression within the reportelement part of each column and it has made the information disappear which is great.

 

however, it leaves empty rows where that information was.

 

meaning the report comes out 152 pages, with a lot of empty or blank rows.  If we consolidated the rows to where there was only text then the report would only take 3 pages or so

 

by the way, thank you again for all of your help. 

Link to comment
Share on other sites

  • 2 weeks later...

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...