Implementing .after Custom Event Handlers

Objective

After completing this lesson, you will be able to use .after event handlers to enrich outbound data

Registering .after Event Handlers

Having discussed an example of a before handler above, let's next look at the implementation of an after handler.

After handlers are executed after the on handlers and are often used to enrich outbound data.

As an example, we will create an after handler for our CatalogService: We want to grant a discount of 11% for books with a stock of more than 200. For this purpose, the character string " -- 11% Discount!" is to be appended to the title of all books whose stock exceeds 200 via an after handler.

We proceed in the same way as with the implementation of the before handler. In other words, we create a file called cat-service.js in the srv folder of our project, as this folder also contains the file cat-service.cds, which is used to define the CatalogService (see following figure).

Analogous to what we discussed with the before handler, we create a subclass of cds.ApplicationService in the cat-service.js file, which we call CatalogService.

To manipulate the book title, we now need an after handler, which should be registered for READ operations on the Books entity. To do this, we overwrite the inherited init() method in the created implementation class (see following figure).

To register an after handler in the init() method, call the inherited after() method via this.after(). The after() method has the following interface. A detailed description of the individual parameters can be found in the CAP documentation.

Code Snippet
12345
function after ( event : string | string[] | '*', entity? : CSN definition | CSN definition[] | string | string[] | '*', handler : function )

In the example shown, we register the named function this.grantDiscount for READ operations on the Books entity. To do this, we proceed analogously to the registration of the before handler discussed above.

Enriching Outbound Data

Now let's explore how the grantDiscount function, which has been registered as an after handler, can be implemented on the CatalogService class.

After handlers receive two arguments:

  • results

    the outcomes of the on handler which ran before

  • req

    an instance of cds.Request

For our example, we only need the results parameter, which is an array of the books retrieved (see following figure).

We implement a loop over the array with the books and append the character string " -- 11% Discount!" to the title of the books whose stock is greater than 200.

Demonstration & Exercise: Provide a .after Event Handler

Note

As exercise, carry out the step-by-step instructions in the following demonstration yourself in the SAP Business Application Studio.

As a starting point for the exercise, use the outcome of the previous exercise Provide a .before Event Handler if you have successfully completed it. Alternatively, you can also use the branch 11_.before_event_handler from the following GitHub repository as a starting point:

https://github.com/SAP-samples/cap-development-learning-journey

The complete implementation of the simulation can be found in the 12_.after_event_handler branch of the GitHub repository.

Detailed information on the content of the repository and how to use it can be found here.

Watch the video to see how to provide a .after event handler.

Log in to track your progress & complete quizzes