Condition expression - how to get REPORT TYPE in condition Expression - jasper-reports

I would like to write expression like this:
{REPORT_TYPE} == "csv" ? "'" + $F{NUMBER_VALUE} : $F{NUMBER_VALUE}
where {REPORT_TYPE} should be xls, csv etc.
Have you any idea how to get report type?

You need to send a parameter from your server which will get these type of format like csv,xls. If this parameter has some value then you can use this expression. e.g. you have a parameter named reportType then you can make a syntax like this.
$P{reportType} ? "'" + $F{NUMBER_VALUE} : $F{NUMBER_VALUE}
if you want report type like csv then you need to give a value to this parameter, otherwise send it as blank string.
If you still get problem let me know.

Related

XML Expression Binding - Proceed Code in conditional operator

I am currently working on a Fiori app. At the moment I try to set a title depending on the value of a property I get from my OData service. Therefore I want to use expression binding with the conditional operator.
So when ${PROPERTIY} has the value "EXAMPLE", it should print the value of OUTPUT_PROPERTY_1. Otherwise, it should print the value of OUTPUT_PROPERTY_2.
XML:
<ObjectListItem title="{= ${PROPERTIY} === 'EXAMPLE' ? '${OUTPUT_PROPERTY_1}' : '${OUTPUT_PROPERTY_2}'}">
Unfortunately, it just prints ${OUTPUT_PROPERTY_1} or ${OUTPUT_PROPERTY_2}, and does not proceed the code to get the actual value of the properties.
Is there any chance to solve this problem or even a good workaround in order to print the actual value of the related property?
Remove the apostrophes around the expression binding syntax:
title="{= ${PROPERTIY} === 'EXAMPLE' ? ${OUTPUT_PROPERTY_1} : ${OUTPUT_PROPERTY_2}}"
Otherwise, '${OUTPUT_PROPERTY_x}' will be treated as a string literal.

Default Value Expression for Optional String Input iReport

I'm using PostgreSQL as DBMS, in my current report I need an optional String parameter to get records by Id, which is a String field.
So I set the Default Value Expression to:
($P{Param} == null || $P{Param}.equals("")) ? "" : "AND id='" + $P{Param} + "'"
When the field is empty the report is created without issues, but when I enter a valid Id the compiler complains:
Caused by: org.postgresql.util.PSQLException: ERROR: syntax error at or near "160E0"   Position: 3126
Just like if I was adding double quotes after and before the value I'm passing. Somebody know how to handle this problem when using String values?
I guess, you've created one Parameter, named Param and in your query you have $P!{Param}. Thing is, default value is set only when parameter stays NULL after prompting, so when you input your ID, ${Param} value is passed into query as it was inputed by user (without AND id=' part).
Try creating second parameter, lets name it $P{input}; set its default value to your expression, "Use as a prompt" value to false. Pass it to your query ($P!{input}). Now, you have your ${Param} for prompting and when it's value is set to the desired ID, $P{input} is set to your query condition.

Getting partial value of a field

How can I grab partial value of field in iReport? for example I got a field
Field A contains = "This is a test script, please ignore"
I would like to remove "This is a test script, " and just display "please ignore" in my report
Is this possible?
You can simply use the standard string operations upon the field in the expression, like substring, charAt etc. to obtain the string of your requirement in the field.
For instance :
$F{myDatafield}.substring(10)
or
$F{myDatafield}.substring(0,10) etc.
Whatever suits your cause.

Display blank value ("") as date in SSRS

I have the column DiscontinuedDate with either a datetime or a blank value. I used the expression
FormatDateTime(Fields!DiscontinuedDate.Value, DateFormat.ShortDate)
To show the date time as just a date but then when the value is blank it shows as an error with the following message "Conversion from string "" to type 'Date' is not valid."
So i've been trying to use an IIF expression like the below:
=IIF(Fields!DiscontinuedDate.Value is "", "", FormatDateTime(Fields!DiscontinuedDate.Value, DateFormat.ShortDate))
I've tried a few variations but they all bring back the same error. Any ideas?
Thanks,
Adam
Your issue is that SSRS IIf expressions do not short circuit, so whenever you have a blank string your code will still be trying the FormatDateTime conversion, so you get this error even with your check.
You can add some logic to stop the empty string being evaluated in the FormatDateTime expression by using another IIf to change it to a NULL value, which won't fail:
=IIF(Fields!DiscontinuedDate.Value = ""
, ""
, FormatDateTime(IIf(Fields!DiscontinuedDate.Value = ""
, Nothing
, Fields!DiscontinuedDate.Value)
, DateFormat.ShortDate))
That solves your immediate issue, but assuming the underlying data is text based, I would also recommend looking at your underlying data and using explicit DateTime type data types instead of strings to prevent these at the lowest possible level.

Text input through SSRS parameter including a Field name

I have a SSRS "statement" type report that has general layout of text boxes and tables. For the main text box I want to let the user supply the value as a parameter so the text can be customized, i.e.
Parameters!MainText.Value = "Dear Mr.Doe, Here is your statement."
then I can set the text box value to be the value of the parameter:
=Parameters!MainText.Value
However, I need to be able to allow the incoming parameter value to include a dataset field, like so:
Parameters!MainText.Value = "Dear Mr.Doe, Here is your [Fields!RunDate.Value] statement"
so that my report output would look like:
"Dear Mr.Doe, Here is your November statement."
I know that you can define it to do this in the text box by supplying the static text and the field request, but I need SSRS to recognize that inside the parameter string there is a field request that needs to be escaped and bound.
Does anyone have any ideas for this? I am using SSRS 2008R2
Have you tried concatenating?
Parameters!MainText.Value = "Dear Mr.Doe, Here is your" & [Fields!RunDate.Value] & "statement"
There are a few dramatically different approaches. To know which is best for you will require more information:
Embedded code in the report. Probably the quickest to
implement would be embedded code in the report that returned the
parameter, but called String.Replace() appropriately to substitute
in dynamic values. You'll need to establish some code for the user for which strings will be replaced. Embedded code will get you access to many objects in the report. For example:
Public Function TestGlobals(ByVal s As String) As String
Return Report.Globals.ExecutionTime.ToString
End Function
will return the execution time. Other methods of accessing parameters for the report are shown here.
1.5 If this function is getting very large, look at using a custom assembly. Then you can have a better authoring experience with Visual Studio
Modify the XML. Depending on where you use
this, you could directly modify the .rdl/.rdlc XML.
Consider other tools, such as ReportBuilder. IF you need to give the user
more flexibility over report authoring, there are many tools built
specifically for this purpose, such as SSRS's Report Builder.
Here's another approach: Display the parameter string with the dataset value already filled in.
To do so: create a parameter named RunDate for example and set Default value to "get values from a query" and select the first dataset and value field (RunDate). Now the parameter will hold the RunDate field and you can use it elsewhere. Make this parameter hidden or internal and set the correct data type. e.g. Date/Time so you can format its value later.
Now create the second parameter which will hold the default text you want:
Parameters!MainText.Value = "Dear Mr.Doe, Here is your [Parameters!RunDate.Value] statement"
Not sure if this syntax works but you get the idea. You can also do formatting here e.g. only the month of a Datetime:
="Dear Mr.Doe, Here is your " & Format(Parameters!RunDate.Value, "MMMM") & " statement"
This approach uses only built-in methods and avoids the need for a parser so the user doesn't have to learn the syntax for it.
There is of course one drawback: the user has complete control over the parameter contents and can supply a value that doesn't match the report content - but that is also the case with the String Replace method.
And just for the sake of completeness there's also the simplistic option: append multiple parameters: create 2 parameters named MainTextBeforeRunDate and MainTextAfterRunDate.
The Textbox value expression becomes:
=Parameters!MainTextBeforeRunDate.Value & Fields!RunDate.Value & Parameters!MainTextAfterRunDate.Value.
This should explain itself. The simplest solution is often the best, but in this case I have my doubts. At least this makes sure your RunDate ends up in the final report text.