#1 2025-07-07 05:42:29

Kritsanapong
Member
Registered: 2024-07-17

Setting report variant parameter via scripting

Hi,

I am trying to import Jasper Report Schedulers into Report Server.

I have a number of schedulers for a Jasper report where each scheduler is providing different parameters, such as client IDs, to the same report and sent to different clients.
I understand that within Report Server, I would need to create a report and one variant per client where each variant would contain the parameters for each particular client. Then I would need to create one scheduler for each variant to send the reports to each client.

Using the scripting feature, I was able to create a report and a variant. However, I am struggling with setting the parameter to each variant as it seems the parameters are not persisted.

Could you provide guidance on how to correctly assign parameter values to a variant?

The code I used to unsuccessfully assign parameters to a variant is shown below

    def reportParameterService = GLOBALS.getInstance(ReportParameterService);
    def reportService = GLOBALS.getInstance(ReportService);

    def report = reportService.getNodeByPath(reportUnitURI);
    def variantReport = ((JasperReport)report).createNewVariant(report)
    variantReport.setName(label);

    for (item in parameterMap) {
        def definition = variantReport.getParameterDefinitionByKey(item.key);
        if (definition != null) {
            def instance = variantReport.getParameterInstanceFor(definition);
            if (definition.getMode() == Mode.Single) {
                def parameter_value = item.value.get(0);
                def instance_value = new DatasourceParameterData();
                instance_value.setKey(parameter_value);
                instance_value.setValue(parameter_value);
                instance.setSingleValue(instance_value);

                // Tried persisting the ParameterInstance, merging the ParameterDefinition and merging the ReportVariant with no success
                reportParameterService.persist(instance);
                reportParameterService.merge(definition);
                reportService.merge(variantReport);
            } else if (definition.getMode() == Mode.Multi) {
                def instance_value_list = new ArrayList();
                for (parameter_value in item.value) {
                    def instance_value = new DatasourceParameterData();
                    instance_value.setKey(parameter_value);
                    instance_value.setValue(parameter_value);
                    instance_value_list.add(instance_value);
                }
                instance.setMultiValue(instance_value_list);

                // Tried persisting the ParameterInstance, merging the ParameterDefinition and merging the ReportVariant with no success
                reportParameterService.persist(instance);
                reportParameterService.merge(definition);
                reportService.merge(variantReport);
            }
        } 
    }

Offline

#2 Yesterday 09:25:21

IF_André
Moderator
Registered: 2025-07-29

Re: Setting report variant parameter via scripting

Hey,

we need some more informations at this point to help you. Could you please provide the full script. Keep in mind, when executing the script in the terminal use "-c" to commit changes to the db, e.g. "exec -c example.groovy".

Offline

Board footer

Powered by FluxBB