Invoke custom service from button on Edit screen

Hello CUBA team,

This is driving me crazy. I’ve got a service which I created because it needs to parse data in the same fashion on both the Browse and Edit screens (my client wants the same functionality on both screens). The problem I’m currently facing is that I can’t get the action set up correctly on the Edit screen. Since I can’t find a nice way to set up the action on the Edit screen, I’ve been following this.

Quick background information:
The service takes the class data container, creates an HTML string and then sends back a byte[] array. I’m sending this back as a return value because the ExportDisplay option doesn’t seem to be available in the service.

The problem I’m facing is that I’m not sure how to call the service from a button in the Edit controller. I keep getting syntax errors.

@Subscribe
    public void onInit(InitEvent event) {
        printSummaryBtn.setAction(new BaseAction("Print Summary") {
            String jobNameAndNumber = jobDc.getItem().getJobName() + "_" + jobDc.getItem().getJobNumber();
            @Override
            public void actionPerform(Component component) {
                byte[] bytes = printJobService.printSummary(Job jobDc);
                exportDisplay.show(new ByteArrayDataProvider(bytes), jobNameAndNumber + ".html", ExportFormat.HTML);
            }
        });
    }

I’m sure I’ve misinterpreted the help files, but I would really appreciate some guidance. It’s entirely possible that I’m going about this all wrong. This is just the way that I’ve been lead to by the documentation I could find.

Thanks

Hi Adam!
In the method call, you need to pass jobDc.getItem()
(and not a container like in your case)

Regards,
Elena

There is also the following option for processing a button click:

  1. Add a button from the Сomponent Palette to the screen
  2. In Component Inspector open Handlers tab and click on ClickEvent or use ALT+Enter quick fix to subscribe to this event
  3. This method allows you to perform any actions you need

image

image

image

Regards,
Elena

Hello Elena,

Thank you for this response. I think I was definitely making it more difficult than it needed to be.

However, after adding the ClickEvent Handler, all looks fine in the code and the application runs, but I cannot load the Edit screen. I receive the following error:

Screen Shot 2020-06-19 at 1.26.30 PM

Since this is outside of any table, I have no idea how to add the action that it’s telling me it needs.

Never mind. The XML file kept showing errors wherever I seemed to place the tag, but once I put it in between the and tags, it seemed to accept it. I’m all set. Thanks again!

1 Like