Quantcast
Channel: SCN : Blog List - ABAP Development
Viewing all articles
Browse latest Browse all 943

Creating a Smartform and Access it via ABAP Program

$
0
0

Hi at all!

 

The next topic I want to cover is a short introduction on how to create a Smartform and call it from a program. For this you have to have an understanding of Smartforms in general.

 

However lets go through the steps starting at creating the smartform up to calling it from your program.


Have fun and if you have any question please feel free to ask!!

 

Regards Patrick.


1.) Call Transaction SMARTFORMS and create a new Form. Prepare it in order to get your data into the form.

 

As you can see on the Screenshot I have already prepared a Smartform named "ZSF_CAR_BOOKING".

 

02_Smartform.PNG

 

The first important step is to prepare data elements in the Form attribute. Therefore in the Tree View an the right side of the window select Form Interface. There we have several tabs, but to keep this simple we just focus on the Import Tab. There you specify the data you need in your form.

In this case we have some different fields of tables which are needed in the form (marked yellow in the screenshot below).

 

03_Smartform_input.PNG

 

As soon as this step is finished we can continue to create so called Windows were we place the data later on. Therefore we make a right click on

%PAGE1 New Page and Create a new Window. In this case I created 3 Windows and one Graphic element for the Company Logo.


As soon as this is finished you can see the finished components in the left tree view. The screenshot below shows how this now looks like. Moreover you can see that %WINDOW1 contains no text but a Address Component. This is a very simple way of adding a company address to your Smartform. The Main Window component also contains 1 Element, namely a Text Element.


04_Smartform_components.PNG


Now comes the point where the "MAGIC" happens. As already mentioned we create some text elements in the corresponding windows (alternatively also tables and loops can be created, but that is not the context of this blog).  The screenshot below shows how to turn on the Field List. With the aid of this field list we can now add our variables to the corresponding text elements.

 

The yellow button (outlined red on the top left of the picture) is the Field List Button and opens the Field List Window in the lower left. The second highlighted button is to insert the variables into the Text element. It is important that you use this button to add variables, otherwise it will not work!

The field list displays all possible variables which can be used in your form. Moreover there are the System Fields which enable you to insert fields like page number, date etc.

 

 

The next thing you have to look for is that you insert the variables enclosed in &. An example is shown in the picture below. As it can be seen the button Insert Field opens a new dialog where you can enter the desired variables you want to display in this form.

 

 

Now you can save your Smartform and activate it. When it is activated, a function module is automatically generated.





2.) Now the Form should be called in your program right? Now this step shows the code with the explanation of how this is done (in the most easiest way). And don't worry about were the data is coming from this is just a snippet of code.

No formatting of the code is used in order to make copying easier for you.                  



*&---------------------------------------------------------------------*
*& Report  ZLP_CALL_SMARTFORM
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ZLP_CALL_SMARTFORM.

DATA:
*     we need that parameter to get the name of the function module
*     created when the Smartform is activated       
      fm_name TYPE rs38l_fnam,
     
*     those are the structures where the different information is stored    
      ls_cars         TYPE ZCARS,
      ls_customers    TYPE ZCUSTOMERS,
      ls_reservations TYPE ZRESERVATIONS,
      days            TYPE I,
      costs           TYPE I,
      mess            TYPE CHAR200.

*     here I calculate how long the car has been rent and moreover the costs
*     are then calculated depending on the category of the car.
      days = ls_reservations-date_to - ls_reservations-date_from.

      IF ls_cars-category = 'A'.
          costs = days * 300.
      ELSEIF ls_cars-category = 'B'.
          costs = days * 200.
      ELSEIF ls_cars-category = 'C'.
          costs = days * 115.
      ENDIF.

*      ----------------------------------------------------------------------
*     This function module call is used to retrieve the name of the Function
*     module generated when the SMARTFORM is activated
*     the export parameter 'formname' needs to be provided with the name of your
*     desired smartform
      CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
         EXPORTING
           formname                 = 'ZSF_CAR_BOOKING'
*          VARIANT                  = ' '
*          DIRECT_CALL              = ' '
        IMPORTING
          fm_name                  = fm_name
        EXCEPTIONS
          no_form                  = 1
          no_function_module       = 2
          OTHERS                   = 3
                 .
      IF sy-subrc <> 0.
       MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
               WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
     ENDIF.

*      now this function module is called and all the parameters
*      which you want to have in your smartform have to be provided
*      here. Error handling for parameters which are not provided is
*      usually done in the Smartform itself with an alternative or some other
*      processing logic.
        CALL FUNCTION fm_name
         EXPORTING
            mandt                 = sy-mandt
            name                 = ls_customers-name
            car                     = ls_cars-car
            category             = ls_cars-category
            license_plate        = ls_cars-license_plate
            reservation_number   = ls_reservations-reservation_id
            date_from            = ls_reservations-date_from
            date_to              = ls_reservations-date_to
            costsrate            = costs
            days_rental          = days
         EXCEPTIONS
           formatting_error           = 1
           internal_error             = 2
           send_error                 = 3
           user_canceled              = 4
           OTHERS                     = 5.

        IF sy-subrc <> 0.
        MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
                WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
        ENDIF.


Viewing all articles
Browse latest Browse all 943

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>