How to restrict Multiselect input control values in Jaspersoft 

This blog will teach the reader , how to restrict multiselect input control values in jasper without hampering the performance .

DB : Postgres (foodmart)

Table : customer

Reporting tool : iReport

Server : Jasperserver 5.6

I/P Parameter : country

Requirement : Get All the informations from customer table where country name is either USA or Mexico or Canada

Condition :

  1. you can not select more than 2 value in “country” input parameter
  2. if more than 2 values are selected in “country” input parameter then the main report query should also not get executed. This will unnecessarily hamper the performance.

Solution :

  1. create multiselect input control named as “country”

select distinct country from customer

  1. create a sub report which will display all the informations from customer table on selection of “country” parameter

and give its print when expression as :

$P{country}.size() <= 2

  1. create a text box in the header , write a message init as :

“You can not select more than 2 values in country parameter”

And give its print when expression as :

$P{country}.size() > 2

  1. in the sub report , query will be :

select * from customer

where $X{IN,country,country}

limit 20

  1. publish the report on jasper server.

O/P

# 1 : when 2 values selected in country parameter

lenlt2#2 : when more than 2 values selected in country parameter

lengt3

Thanks,

Rupam Bhardwaj

3 comments

  1. Tapan

    Hi,

    Can you please tell me where to put “$P{country}.size() <= 2" condition

    • Rupam Bhardwaj

      Hi Tapan,

      Go to the subreport Properties section . There you will get one Property called “Print when expression” . Here You have to put “$P{country}.size() <= 2".

      For your reference I have attached screenshot below :

    • Nitin

      you need to put that condition on sub report’s “print when expression”.

Leave a Reply