Error when Sending Email

Hi,
https://doc.cuba-platform.com/manual-7.1/sending_emails_recipe.html
I followed the steps mentioned in above link for sending emails.
I am getting error when tapping on create or edit options.

Hi @nevil.christopher

Your problem is that you are trying to apply new annotations (for version 7+ screens) to legacy screen (AbstractEditor is a legacy editor screen). @UiDescriptor annotation is not supported on legacy screens. To specify a descriptor for legacy screen, you need to specify it in the screens.xml file (see documentation for details).

To solve, you need to either create a legacy screen correctly (using CUBA Studio API) or slightly change the implementation of StandardEditor screen and use the functions of the new API.

An example of using the new screen API:

@UiController("sample_NewsItem.edit")
@UiDescriptor("news-item-edit.xml")
@EditedEntityContainer("newsItemDc")
@LoadDataBeforeShow
public class NewsItemEdit extends StandardEditor<NewsItem> {

    // Indicates that a new item was created in this editor
    private boolean justCreated;

    @Inject
    protected EmailService emailService;
    @Inject
    protected Dialogs dialogs;

    // This method is invoked when a new item is initialized
    @Subscribe
    public void onInitEntity(InitEntityEvent<NewsItem> event) {
        justCreated = true;
    }

    @Subscribe(target = Target.DATA_CONTEXT)
    public void onPostCommit(DataContext.PostCommitEvent event) {
        if (justCreated) {
            // If a new entity was saved to the database, ask a user about sending an email
            dialogs.createOptionDialog()
                    .withCaption("Email")
                    .withMessage("Send the news item by email?")
                    .withType(Dialogs.MessageType.CONFIRMATION)
                    .withActions(
                            new DialogAction(DialogAction.Type.YES) {
                                @Override
                                public void actionPerform(Component component) {
                                    sendByEmail();
                                }
                            },
                            new DialogAction(DialogAction.Type.NO)
                    )
                    .show();
        }
    }

    // Queues an email for sending asynchronously
    private void sendByEmail() {
        NewsItem newsItem = getEditedEntity();
        EmailInfo emailInfo = new EmailInfo(
                "john.doe@company.com,jane.roe@company.com", // recipients
                newsItem.getCaption(), // subject
                null, // the "from" address will be taken from the "cuba.email.fromAddress" app property
                "com/company/demo/templates/news_item.txt", // body template
                Collections.singletonMap("newsItem", newsItem) // template parameters
        );
        emailService.sendEmailAsync(emailInfo);
    }
}

Regards,
Gleb

2 Likes