Select template for Report

Hi,
is it possible to select a template for reporting while starting the report from a screen.

Maybe I have different templates for one report and want to select the template by a value name from a table.

Sorry for my bad english.

Thanks a lot
Torsten

2 Likes

Hi,
From screens you can print reports using the TablePrintFormAction, EditorPrintFormAction or RunReportAction as described in the documenation.
A report could be printed only with the default template.
But you can create several reports which differ by template and choose between them.
When the PrintFormAction is launched on the screen and multiple applicable for the entity reports exist, the platform offers to chose the desired one in a dialog (see the attached screenshot).

The second way to run reports is to launch them programmatically using the ReportGuiManager. It is more complicated, but provides an ability to specify the template.
The report and template selection should be manually implemented (f.e. by adding a pair of LookupFields to screen).

Look to the following code:

  1. It is a controller of the ā€˜Customerā€™ browse screen
  2. The report is printed by the reportGuiManager.printReport()
  3. The method receives the template code (ā€œCustom_templateā€) as a parameter
  4. The ā€˜runReportā€™ may be invoked by button or any other way.

public class CustomerBrowse extends AbstractLookup {

    @Inject
    private DataService dataService;

    @Inject
    private Table<Customer> customersTable;

    protected ReportGuiManager reportGuiManager = AppBeans.get(ReportGuiManager.class);

    public void runReport(Component source) {

        Map<String, Object> reportParams = new HashMap<>();
        reportParams.put("entity", customersTable.getSingleSelected());

        LoadContext<Report> lContext = new LoadContext<>(Report.class);
        lContext.setQueryString("select r from report$Report r where r.id = '3ef3a9ed-0eb3-cc2a-be2b-7f175a25bab0' ");
        Report report = dataService.load(lContext);

        String templateCode = "Custom_template";

        reportGuiManager.printReport(report, reportParams, templateCode, null);
    }
}

ReportSelection

1 Like

Thanks a lot!

Hi,

I am getting null point exception while running this code.
What should be r.id in the query string . Will it be same or it may differ?
Below is the error log.

    java.lang.NullPointerException
	at com.haulmont.cuba.core.app.DataManagerBean.reload(DataManagerBean.java:104)
	at com.haulmont.reports.ReportingBean.reloadEntity(ReportingBean.java:606)
	at com.haulmont.reports.ReportingBean.createReport(ReportingBean.java:200)
	at com.haulmont.reports.ReportServiceBean.createReport(ReportServiceBean.java:45)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
	at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:85)
	at com.haulmont.cuba.core.sys.ServiceInterceptor.aroundInvoke(ServiceInterceptor.java:117)
	at sun.reflect.GeneratedMethodAccessor139.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:629)
	at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:618)
	at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:168)
	at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
	at com.sun.proxy.$Proxy260.createReport(Unknown Source)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at com.haulmont.cuba.core.sys.remoting.LocalService

Hi,
In the sample, the certain report which I have got in my database is launched (selecting it by ID).

ID of any database record could be got from System Information in Table (mouse right click on the corresponding row).

Hi Rostislav,
I want to create a multiple reports and using a different template.how to do that.

Hi Rostislav,

public class CustomerBrowse extends AbstractLookup {

@Inject
private DataService dataService;

@Inject
private Table<Customer> customersTable;

protected ReportGuiManager reportGuiManager = AppBeans.get(ReportGuiManager.class);

public void runReport(Component source) {

    Map<String, Object> reportParams = new HashMap<>();
    reportParams.put("entity", customersTable.getSingleSelected());

    LoadContext<Report> lContext = new LoadContext<>(Report.class);
    lContext.setQueryString("select r from report$Report r where r.id = '3ef3a9ed-0eb3-cc2a-be2b-7f175a25bab0' ");
    Report report = dataService.load(lContext);

    String templateCode = "Custom_template";

    reportGuiManager.printReport(report, reportParams, templateCode, null);
}

} iā€™m trying to using this operation And iā€™m followed code follows.

Look to the following code:

It is a controller of the ā€˜Customerā€™ browse screen
The report is printed by the reportGuiManager.printReport()
The method receives the template code (ā€œCustom_templateā€) as a parameter
The ā€˜runReportā€™ may be invoked by button or any other way.

And i need a service class bean or donā€™t wantā€¦