Friday 12 May 2023

Create an OData service by passing a parameter and get Smart form as a PDF

Introduction


This blog shows the gateway project using OData service to obtain an output as a PDF by using Smart form. The objective is to pass the parameter as input and obtain an output as a PDF using Smart Form.

Requirement: Pass the Invoice Document number (Belnr) as input and obtain Plant, Purchase document number, Item number, Fiscal year, Material Number, and Reference number as an output through OData Service by using the smart form.

◉ Below structure is used as a sample source of our data model.

SAP ABAP Career, SAP ABAP Skills, SAP ABAP Jobs, SAP ABAP Learning, SAP ABAP Tutorial and Material, SAP ABAP Guides, SAP ABAP Certification

◉ To create an OData service go to SAP Gateway Service Builder (transaction SEGW). Below I made an OData service named ZDEMO_ODATA_01 and saved it in the Local object.

SAP ABAP Career, SAP ABAP Skills, SAP ABAP Jobs, SAP ABAP Learning, SAP ABAP Tutorial and Material, SAP ABAP Guides, SAP ABAP Certification

◉ Create a data model by importing DDIC structure ‘ZDEMO_STR_01’.

SAP ABAP Career, SAP ABAP Skills, SAP ABAP Jobs, SAP ABAP Learning, SAP ABAP Tutorial and Material, SAP ABAP Guides, SAP ABAP Certification

◉ Select the required fields, set the primary key, and click Finish. Below I take Belnr as a key.

SAP ABAP Career, SAP ABAP Skills, SAP ABAP Jobs, SAP ABAP Learning, SAP ABAP Tutorial and Material, SAP ABAP Guides, SAP ABAP Certification

◉ The entity types and entity sets will be created. Make sure the Media button in the entity type is enabled.

SAP ABAP Career, SAP ABAP Skills, SAP ABAP Jobs, SAP ABAP Learning, SAP ABAP Tutorial and Material, SAP ABAP Guides, SAP ABAP Certification

◉ Generate the OData project and save it in a local object.

SAP ABAP Career, SAP ABAP Skills, SAP ABAP Jobs, SAP ABAP Learning, SAP ABAP Tutorial and Material, SAP ABAP Guides, SAP ABAP Certification

◉ Go to RUNTIME ARTIFACTS => ZCL_ZDEMO_ODATA_01_DPC_EXT. Redefine the method named /IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_STREAM.

SAP ABAP Career, SAP ABAP Skills, SAP ABAP Jobs, SAP ABAP Learning, SAP ABAP Tutorial and Material, SAP ABAP Guides, SAP ABAP Certification

◉ By using IT_KEY_TAB, Fetch the Invoice document number given by the user. Using That, Fetch data from the Database table and move it to the Overall structure.

    DATA: ls_str     TYPE zdemo_str_01,
          ls_str_itm TYPE zdemo_str_02.

    IF it_key_tab IS NOT INITIAL.
      DATA(lv_key_tab) = VALUE #( it_key_tab[ 1 ]-value ).

      SELECT belnr, gjahr, buzei,
             ebeln, matnr, werks,
             xblnr
        FROM rseg
        INTO TABLE @DATA(lt_ztab)
             WHERE belnr = @lv_key_tab.

      IF lt_ztab IS NOT INITIAL.
        DATA(ls_header) = VALUE #( lt_ztab[ 1 ] ).
        ls_str-invoicedocnum = ls_header-belnr.
        ls_str-plant         = ls_header-werks.

        LOOP AT lt_ztab INTO DATA(ls_ztab).
          ls_str_itm-purdocnum = ls_ztab-ebeln.
          ls_str_itm-fisyear   = ls_ztab-gjahr.
          ls_str_itm-itemcode  = ls_ztab-buzei.
          ls_str_itm-material  = ls_ztab-matnr.
          ls_str_itm-refnumber =  ls_ztab-xblnr.
          APPEND ls_str_itm TO ls_str-item.
          CLEAR ls_str_itm.
        ENDLOOP.
     ENDIF.
    ENDIF.

◉ Call smart form ZDEMO_SFM_01 by using Function module SSF_FUNCTION_MODULE_NAME.

    DATA: lv_fm_name   TYPE rs38l_fnam,
          ls_cntl_prmt TYPE ssfctrlop,
          ls_op_opt    TYPE ssfcompop,
          ls_ssfcrescl TYPE ssfcrescl.

        CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
          EXPORTING
            formname           = 'ZDEMO_SFM_01'
*           VARIANT            = ' '
*           DIRECT_CALL        = ' '
          IMPORTING
            fm_name            = lv_fm_name
          EXCEPTIONS
            no_form            = 1
            no_function_module = 2
            OTHERS             = 3.

        IF sy-subrc <> 0.
* Implement suitable error handling here
        ENDIF.

        ls_cntl_prmt-langu  = 'EN'.
        ls_cntl_prmt-getotf = abap_true.
        ls_cntl_prmt-preview = space.
        ls_cntl_prmt-no_dialog = abap_true.

        ls_op_opt-tddest    = 'LOCL'.
        ls_op_opt-xdfcmode  = abap_true.
        ls_op_opt-tdnewid   = abap_true.
        ls_op_opt-tdimmed   = abap_true.

        CALL FUNCTION lv_fm_name "'/1BCDWB/SF00000080'
          EXPORTING
            control_parameters = ls_cntl_prmt
            output_options     = ls_op_opt
            user_settings      = 'X'
            e_structure        = ls_str
          IMPORTING
*           DOCUMENT_OUTPUT_INFO       =
            job_output_info    = ls_ssfcrescl
*           JOB_OUTPUT_OPTIONS =
          EXCEPTIONS
            formatting_error   = 1
            internal_error     = 2
            send_error         = 3
            user_canceled      = 4
            OTHERS             = 5.

        IF sy-subrc <> 0.
* Implement suitable error handling here
        ENDIF.
 
◉ Convert the OTF file to a PDF file using the function module CONVERT_OTF.
 
       DATA: lt_otf      TYPE TABLE OF itcoo,
              lv_bin_file TYPE xstring,
              lt_lines    TYPE TABLE OF tline,
              gs_stream   TYPE ty_s_media_resource.

        REFRESH lt_otf[].
        lt_otf[] = ls_ssfcrescl-otfdata[].
        CLEAR : lv_bin_file.

        CALL FUNCTION 'CONVERT_OTF'
          EXPORTING
            format                = 'PDF'
          IMPORTING
            bin_file              = lv_bin_file
          TABLES
            otf                   = lt_otf
            lines                 = lt_lines
          EXCEPTIONS
            err_max_linewidth     = 1
            err_format            = 2
            err_conv_not_possible = 3
            err_bad_otf           = 4
            OTHERS                = 5.
        IF sy-subrc <> 0.
          RETURN.
        ENDIF.

        gs_stream-mime_type = 'application/pdf'.
        gs_stream-value     = lv_bin_file.

        CALL METHOD me->copy_data_to_ref
          EXPORTING
            is_data = gs_stream
          CHANGING
            cr_data = er_stream.
 
◉ Register a service for that OData project using transaction code /IWFND/MAINT_SERVICE and navigate to GUI Gateway using transaction code /IWFND/GW_CLIENT.

SAP ABAP Career, SAP ABAP Skills, SAP ABAP Jobs, SAP ABAP Learning, SAP ABAP Tutorial and Material, SAP ABAP Guides, SAP ABAP Certification

◉ Select the entity set and fill URL as given below:

URL: /sap/opu/odata/SAP/ZDEMO_ODATA_01_SRV/InvoiceDocNumberSet('5105600762')/$value.
(Note: $value used to get the raw value of a property. It used to trigger get stream method).

OUTPUT :

SAP ABAP Career, SAP ABAP Skills, SAP ABAP Jobs, SAP ABAP Learning, SAP ABAP Tutorial and Material, SAP ABAP Guides, SAP ABAP Certification

No comments:

Post a Comment