Step by Step example of simple BSP application - Part 2

This example further develops the application in Step by Step example of simple BSP application - Part 1 so that new customers can be added to the datbase table.

The example also demonstrates how to use an application class to store business logic for the entire application

1. Assign an application class to the application

All business logic for the BSP application, has been encapsulated in the class Z_CL_KUNDER, which is an ordinary ABAP class.

Assign this class to the BSP application as an application class. Click here to see how.

 

2. Create a new BSP page for adding new customers

Create a new page called CreateCustomer.htm. This will add a new customer to table ZKUNDER, which ahs the follwing fields:

The new customers are added by calling method kunde_addnew in class Z_CL_KUNDER which is the application class of the BSP application.

If the customer name allready exists, an error message is shown on the page, else the message Customer created is shown.

2.1. Create page layout

The page should look like this when it is finished:

The page is consists of a form the contains to tables to hold the fields and the buttons, and code tos show messsages. The code for the layout:

<%@page language="abap" %>
<%@extension name="htmlb" prefix="htmlb" %>
<html>
<title>Create new Customer</title>
<h1>Create new Customer</h1>
<body>
  <form method = "post" name="x">
    <table border="1">
      <tr>
        <td>Customer name:</td>
        <td>
          <input type="text" size="50"  maxlength="50" name="zzkunnavn">
        </td>
      </tr>
      <tr>
        <td>Currency</td>
        <td>
          <input type="text" size="3"  maxlength="3" name="zzcurr">
        </td>
      </tr>
  </table>
  <table border="1">
     <tr>
       <td>
         <input type=submit name="onInputProcessing(save)" value="Save">
       </td>
       <td>
        <input type=submit name="onInputProcessing(cancel)" value="Cancel">
       </td>
    </table>
  </form>
<%
  if return_code = 1.
%>
<!-- Show error message if customer allready exists -->
<p><font color="#FF0000"><strong>Customer allready exists</strong></font></p>
<%
  else.
%>
<p><font ><strong>Customer created </strong></font></p>
<%
  endif.
%>
</body>
</html>

 

2.2 Create attributes

To transferre fields from the form to an event, the fields must be declared as Page Attributes. The form uses to input fields zzkunnavn and zzcurr . Define these to fields as Page Attributes. Also create the return code from craeting the customer return_code.

 

2.3 Code the event handler OnInputProcessing

When the user presses the Save button, the new customer is created, if he presses the Cancel button he returns to the Main menu.

The new customer is created calling the method kunde_addnew in the application class, that throws the exception zcx_cl_kunder_allready_exists if the customer name allready exists in the database table.

If an error occures (Customer name allready exists). the Page Attribute return_code is set to 1.

DATA: l_kunde TYPE zkunder,
      o_ex_allready_exists TYPE REF TO zcx_cl_kunder_allready_exists.


CASE event_id.
  WHEN 'save'.
    CLEAR l_kunde.
    return_code = 0. 
    
*   Sets value of parameters, i.e. gets value from screen
    navigation->set_parameter( name = 'zzkunnavn').
    navigation->set_parameter( name = 'zzcurr').

*   Gets value of parameter, so can be moved into abap field
*   Note: zzkunnavn and zzcurr are declared in page attributes
    l_kunde-zzkunnavn = navigation->get_parameter( name = 'zzkunnavn').
    l_kunde-zzcurr = navigation->get_parameter( name = 'zzcurr').

    TRY.
        CALL METHOD application->kunde_addnew
          EXPORTING
            pkunde = l_kunde.
      CATCH zcx_cl_kunder_allready_exists INTO o_ex_allready_exists.
*      An error has occured, set return_code to 1    
       return_code = 1.
    ENDTRY.

  WHEN 'cancel'.
    navigation->next_page('TOMAIN').

ENDCASE.

        

 

 

 

 

 

 

 

 

 

 

 

Converted from CHM to HTML with chm2web Standard 2.7 (unicode)