Form Calling from SAP Script

*&---------------------------------------------------------------------*
*& Report ZSD_PICKING_FORM
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zsd_picking_form.
***read shipping notes

* To call from from SAP Script:
*/:   DEFINE &ITEM_TEXT& = ' '
*/:   DEFINE &LV_VBELN& = '0080000103'
*/:   PERFORM CALL_FROM_SCRIPT IN PROGRAM ZSD_PICKING_FORM
*/:   USING &LV_VBELN&
*/:   CHANGING &ITEM_TEXT&
*/:   ENDPERFORM
* To display the variable use the standard:
* =     &ITEM_TEXT&

* TEST
PERFORM unit_test.

* Unit test - emulates the SAP Script call
FORM unit_test.

  TYPES: form_params_ty TYPE STANDARD TABLE OF itcsy
          WITH NON-UNIQUE KEY name.

  DATA: lv_vbeln TYPE thead-tdname,
        lv_text  TYPE string.

  lv_vbeln = '0080000103'.

* construct the calling parameters
  DATA(in_params) = VALUE form_params_ty(
    ( name = `LV_VBELN`     value = lv_vbeln ) ).
  DATA(out_params) = VALUE form_params_ty(
    ( name = `LV_TEXT`     value = ' ' ) ).

* make the actual call
  PERFORM call_from_script TABLES in_params
                                  out_params.
* display the result
  LOOP AT out_params ASSIGNING FIELD-SYMBOL(<fs>).
    WRITE:/ <fs>-name, <fs>-value.
  ENDLOOP.
ENDFORM.

* this function is called from the script
* the data is passed in and out in TABLES
* the tables contain two columns: KEY and VALUE
FORM call_from_script TABLES  in_params  STRUCTURE itcsy
                              out_params STRUCTURE itcsy.

  DATA: lv_vbeln TYPE thead-tdname,
        lv_convert_veln TYPE vbeln,
        lv_text  TYPE string.

  " get the values of the keys from the IN table
  READ TABLE in_params WITH KEY name = 'LV_VBELN'.
  IF sy-subrc EQ 0.
    lv_vbeln = in_params-value.
    UNPACK lv_vbeln TO lv_convert_veln. "ADD LEADING ZEROS ACCORGING TO FM REQUREMENTS
    lv_vbeln = lv_convert_veln.
  ENDIF.


  " get the data
  PERFORM get_text USING lv_vbeln CHANGING lv_text.


  " put the data into the OUT table
  " export output parameters in the OUT table
  LOOP AT out_params ASSIGNING FIELD-SYMBOL(<fs>).
    IF <fs>-name = 'LV_TEXT'.
      <fs>-value = lv_text.
      EXIT.
    ENDIF.
  ENDLOOP.
ENDFORM.

* pass VBELN and a STRING
FORM get_text   USING in_vbeln
                CHANGING out_text.

  DATA: lv_id     TYPE thead-tdid VALUE 'TX03',
        lv_object TYPE thead-tdobject VALUE 'VBBK',
        lv_lang   TYPE thead-tdspras,
        lt_lines  TYPE TABLE OF tline,
        lv_text   TYPE string.

  lv_lang = sy-langu.

* try to get the texts
  CALL FUNCTION 'READ_TEXT'
    EXPORTING
      id                      = lv_id
      language                = lv_lang
      name                    = in_vbeln
      object                  = lv_object
    TABLES
      lines                   = lt_lines
    EXCEPTIONS
      id                      = 1
      language                = 2
      name                    = 3
      not_found               = 4
      object                  = 5
      reference_check         = 6
      wrong_access_to_archive = 7
      OTHERS                  = 8.

* if anything is fpund, concatenate all texts and return them
  IF sy-subrc = 0.
    FIELD-SYMBOLS <fs_text> TYPE tline.

    CLEAR lv_text.
    LOOP AT lt_lines ASSIGNING <fs_text>.
      CONCATENATE lv_text <fs_text>-tdline INTO lv_text RESPECTING BLANKS SEPARATED BY ' '.
    ENDLOOP.

    out_text = lv_text.
  ENDIF.
ENDFORM.