Link to home
Start Free TrialLog in
Avatar of Shaun Wingrin
Shaun WingrinFlag for South Africa

asked on

Jasper iReports and conditional statement in field expression

Say, I want to padd the Integer CurrentInvNo with one or two leading zeros. This doesn't work. Any ideas?

$F{AccNo}+iif(F${CurrentInvNo} > 9, iif($F{CurrentInvNo} > 99,'0','0'),'00')+$F{CurrentInvNo}
Avatar of James0628
James0628

"This doesn't work" is just a tad vague.  Does it give you an error?  Does it produce the wrong results?  Does it make your computer sing show tunes?  :-)  A little more info would be helpful.

 Having said that, and with the disclaimer that I don't know the first thing about Jasper iReports, I do see one error in your formula.

 The middle iif should read:

 iif($F{CurrentInvNo} > 99,'','0')

 Which would make the formula:

$F{AccNo}+iif(F${CurrentInvNo} > 9, iif($F{CurrentInvNo} > 99,'','0'),'00')+$F{CurrentInvNo}


 If you were getting a leading 0 on 3 digit numbers, that was probably why.

 I'm assuming that CurrentInvNo is always positive.  If not, you'd need separate tests for negative values, or you could use an absolute value function, if there is one, when testing the values.

 James
Avatar of Shaun Wingrin

ASKER

Jasper won't complie this statement.
  Compiling to file... C:\Documents and Settings\admin\My Documents\Voip SW\iReports\Savings Analysis Report & Management Invoice.jasper
Errors compiling C:\Documents and Settings\admin\My Documents\Voip SW\iReports\Savings Analysis Report & Management Invoice.jasper!
Compilation exceptions: com.jaspersoft.ireport.designer.compiler.ErrorsCollector@998ba2  net.sf.jasperreports.engine.JRException: Errors were encountered when compiling report expressions class file: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed, calculator_Savings32Analysis32Report323832Management32Invoice_1263887050185_910313: 191: unexpected token: F @ line 191, column 181. 1 error      at net.sf.jasperreports.compilers.JRGroovyCompiler.compileUnits(JRGroovyCompiler.java:88)     at net.sf.jasperreports.engine.design.JRAbstractCompiler.compileReport(JRAbstractCompiler.java:188)     at net.sf.jasperreports.engine.JasperCompileManager.compileReport(JasperCompileManager.java:215)     at net.sf.jasperreports.engine.JasperCompileManager.compileReportToFile(JasperCompileManager.java:131)     at com.jaspersoft.ireport.designer.compiler.IReportCompiler.run(IReportCompiler.java:510)     at org.openide.util.RequestProcessor$Task.run(RequestProcessor.java:572)     at org.openide.util.RequestProcessor$Processor.run(RequestProcessor.java:997)
Compilation running time: 1,329
OK.  In the middle of all of that, I see

unexpected token: F @ line 191, column 181


 Meanwhile, looking at your code again, I noticed another apparent problem.  After the first iif you have F${CurrentInvNo}, but in other places in the line, you have $F.

 Still just guessing here, but maybe the error on token "F" is because you have F$ in that one spot, instead of $F.  Or possibly it's the other way around, and each $F should be F$ .  Either way, I'm guessing that they should all be the same.

 James
Good point but not. I'm trying to find if Jasper support iif and what correct syntax is....

  Compiling to file... C:\Documents and Settings\admin\My Documents\Voip SW\iReports\Savings Analysis Report & Management Invoice.jasper
Compilation running time: 2,640!
  Filling report...
  Locale: English (South Africa)
  Time zone: Default
Error filling print... Error evaluating expression :      Source text : $F{AccNo}+$F{CurrentInvNo}+$F{AccNo}+$F{AccNo}+iif($F{CurrentInvNo} > 9, iif($F{CurrentInvNo} > 99,'','0'),'00')+$F{CurrentInvNo}
net.sf.jasperreports.engine.fill.JRExpressionEvalException: Error evaluating expression :      Source text : $F{AccNo}+$F{CurrentInvNo}+$F{AccNo}+$F{AccNo}+iif($F{CurrentInvNo} > 9, iif($F{CurrentInvNo} > 99,'','0'),'00')+$F{CurrentInvNo}      at net.sf.jasperreports.engine.fill.JREvaluator.evaluate(JREvaluator.java:193)      at net.sf.jasperreports.engine.fill.JRCalculator.evaluate(JRCalculator.java:570)      at net.sf.jasperreports.engine.fill.JRCalculator.evaluate(JRCalculator.java:538)      at net.sf.jasperreports.engine.fill.JRFillElement.evaluateExpression(JRFillElement.java:868)      at net.sf.jasperreports.engine.fill.JRFillTextField.evaluateText(JRFillTextField.java:368)      at net.sf.jasperreports.engine.fill.JRFillTextField.evaluate(JRFillTextField.java:353)      at net.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(JRFillElementContainer.java:256)      at net.sf.jasperreports.engine.fill.JRFillBand.evaluate(JRFillBand.java:482)      at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillBandNoOverflow(JRVerticalFiller.java:421)      at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillPageHeader(JRVerticalFiller.java:385)      at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillPageBreak(JRVerticalFiller.java:1913)      at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillColumnBreak(JRVerticalFiller.java:1940)      at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillDetail(JRVerticalFiller.java:724)      at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportContent(JRVerticalFiller.java:272)      at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:114)      at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:923)      at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:826)      at net.sf.jasperreports.engine.fill.JRFiller.fillReport(JRFiller.java:59)      at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:417)      at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:247)      at com.jaspersoft.ireport.designer.compiler.IReportCompiler.run(IReportCompiler.java:858)      at org.openide.util.RequestProcessor$Task.run(RequestProcessor.java:572)      at org.openide.util.RequestProcessor$Processor.run(RequestProcessor.java:997)  Caused by: groovy.lang.MissingMethodException: No signature of method: Savings32Analysis32Report323832Management32Invoice_1263907738794_781923.iif() is applicable for argument types: (java.lang.Boolean, java.lang.String, java.lang.String) values: {false, "", "0"}      at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:54)      at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:59)      at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytecodeAdapter.java:169)      at Savings32Analysis32Report323832Management32Invoice_1263907738794_781923.invokeMethod(calculator_Savings32Analysis32Report323832Management32Invoice_1263907738794_781923)      at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodOnCurrentN(ScriptBytecodeAdapter.java:82)      at Savings32Analysis32Report323832Management32Invoice_1263907738794_781923.evaluate(calculator_Savings32Analysis32Report323832Management32Invoice_1263907738794_781923:191)      at net.sf.jasperreports.engine.fill.JREvaluator.evaluate(JREvaluator.java:182)      ... 22 more 
Print not filled. Try to use an EmptyDataSource...
 
 > Good point but not. I'm trying to find if Jasper
 > support iif and what correct syntax is....

 Oh well.  It was a shot in the dark, although I really did think I might be on to something.

 I've looked around jasperforge.org some, but haven't been able to find specific documentation on "formulas" like that (which I suppose may be your problem).  If the underlying language is Java, then maybe that's really where you need to be looking to see if anything like iif is supported.

 FWIW, looking at one of the Jasper examples, I see the following, which looks kind of similar to your line:


"There are " + String.valueOf($V{REPORT_COUNT}) + " orders on this report, with a total freight of " + new DecimalFormat("0.00").format($V{FreightSumReport}.doubleValue())



 Maybe DecimalFormat is what you need to be looking at, in order to format CurrentInvNo to "000".  Still just making guesses.  :-)

 The Jasper example that I was looking at is FirstJasper.jrxml .  If it's included in the Jasper "package", then you may already have it, but I found it on:

 http://jasperforge.org/website/jasperreportswebsite/trunk/samples.html?group_id=252


 James
Tx 4 the effort, however the decimal point will look out of place as need Invoice NO: 013 for example.
Yes java could be the clue.... or someone who know iReports...
ASKER CERTIFIED SOLUTION
Avatar of James0628
James0628

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Tx. I did try but still not. Asking moderator.
Error filling print... Error evaluating expression :      Source text : $F{AccNo}+$F{CurrentInvNo}+$F{AccNo}+$F{AccNo}+DecimalFormat("000").format($F{CurrentInvNo})
net.sf.jasperreports.engine.fill.JRExpressionEvalException: Error evaluating expression :      Source text : $F{AccNo}+$F{CurrentInvNo}+$F{AccNo}+$F{AccNo}+DecimalFormat("000").format($F{CurrentInvNo})      at net.sf.jasperreports.engine.fill.JREvaluator.evaluate(JREvaluator.java:193)      at net.sf.jasperreports.engine.fill.JRCalculator.evaluate(JRCalculator.java:570)      at net.sf.jasperreports.engine.fill.JRCalculator.evaluate(JRCalculator.java:538)      at net.sf.jasperreports.engine.fill.JRFillElement.evaluateExpression(JRFillElement.java:868)      at net.sf.jasperreports.engine.fill.JRFillTextField.evaluateText(JRFillTextField.java:368)      at net.sf.jasperreports.engine.fill.JRFillTextField.evaluate(JRFillTextField.java:353)      at net.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(JRFillElementContainer.java:256)      at net.sf.jasperreports.engine.fill.JRFillBand.evaluate(JRFillBand.java:482)      at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillBandNoOverflow(JRVerticalFiller.java:421)      at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillPageHeader(JRVerticalFiller.java:385)      at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillPageBreak(JRVerticalFiller.java:1913)      at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillColumnBreak(JRVerticalFiller.java:1940)      at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillDetail(JRVerticalFiller.java:724)      at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportContent(JRVerticalFiller.java:272)      at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:114)      at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:923)      at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:826)      at net.sf.jasperreports.engine.fill.JRFiller.fillReport(JRFiller.java:59)      at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:417)      at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:247)      at com.jaspersoft.ireport.designer.compiler.IReportCompiler.run(IReportCompiler.java:858)      at org.openide.util.RequestProcessor$Task.run(RequestProcessor.java:572)      at org.openide.util.RequestProcessor$Processor.run(RequestProcessor.java:997)  Caused by: groovy.lang.MissingMethodException: No signature of method: Savings32Analysis32Report323832Management32Invoice_1263971210466_105582.DecimalFormat() is applicable for argument types: (java.lang.String) values: {"000"}      at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:54)      at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:59)      at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytecodeAdapter.java:169)      at Savings32Analysis32Report323832Management32Invoice_1263971210466_105582.invokeMethod(calculator_Savings32Analysis32Report323832Management32Invoice_1263971210466_105582)      at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodOnCurrentN(ScriptBytecodeAdapter.java:82)      at Savings32Analysis32Report323832Management32Invoice_1263971210466_105582.evaluate(calculator_Savings32Analysis32Report323832Management32Invoice_1263971210466_105582:191)      at net.sf.jasperreports.engine.fill.JREvaluator.evaluate(JREvaluator.java:182)      ... 22 more 
Print not filled. Try to use an EmptyDataSource...
 
Still don't know if iif (,,) is valid in Jasper