SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

Calling smartforms dynamically using dynamic function param



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Smartforms, SapScripts, PDF
View previous topic :: View next topic  
Author Message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1639

PostPosted: Tue Jan 15, 2008 1:51 pm    Post subject: Calling smartforms dynamically using dynamic function param Reply with quote

Calling smartforms dynamically using dynamic function parameters
Ravi Shankar Rajan

In this article I would like to talk about a custom class created by me which can be used to call smartforms dynamically.I am using SAP dynamic function tables PARAMETER-TABLE and EXCEPTION-TABLE for this purpose.This class also provides the option of printing the smartform in PDF format.

The various features of the above class are
1.This class can be used to call any smartform irrespective of the number of "Importing","Exporting" or "Tables" parameters required by the smartform.

2.If a smartform is been called from a standard SAP transaction then NAST values can be passed to the class.Based on the NAST values the "CONTROL_PARAMETERS","OUTPUT OPTIONS",recipient and sender details are determined automatically by the class and passed to the called smartform.

3.The class also provides an option of printing the form in PDF format.

PART 1 - Creation of class

1. Create a class ZDYNAMIC_SF_PRINT in SE24

Class Properties
Instantiation : Public
Forward Declarations
Type Group : ABAP

Private Instance Attributes
FORM_NAME type TDSFNAME
PROGRAM type SYREPID
NAST type NAST
JOB_OUTPUT_INFO type SSFCRESCL

Methods of the class

1. CONSTRUCTOR

Parameters
I_FORM_NAME type TDSFNAME
I_PROGRAM type SYREPID
I_NAST type I_NAST

2. PRINT_FORM

Parameters

Importing
CONVERT_TO_PDF type xfeld default space
FILENAME type string

Changing
PTAB type ABAP_FUNC_PARMBIND_TAB
ETAB type ABAP_FUNC_EXCPBIND_TAB

Exceptions
NO_FORM
NO_FUNCTION_MODULE
UNKNOWN_EXCEPTION
INVALID_FILENAME
ERR_MAX_LINEWIDTH
ERR_FORMAT
ERR_CONV_NOT_POSSIBLE
ERR_BAD_OTF
FILE_WRITE_ERROR

3.Source Code

CONSTRUCTOR METHOD

Code:
METHOD constructor.
  me->form_name = i_form_name.
  me->program = i_program.
  MOVE-CORRESPONDING i_nast TO me->nast.
ENDMETHOD.                    "


PRINT_FORM METHOD

Code:
METHOD print_form.
  DATA :
  v_fm_name TYPE rs38l_fnam,
  v_returncode TYPE sysubrc,
  lwa_itcpo TYPE itcpo,
  v_tddevice TYPE tddevice,
  v_recipient TYPE swotobjid,
  v_sender TYPE swotobjid.

*Smartform function module parameters
  DATA : control_parameters TYPE ssfctrlop,
  output_options TYPE ssfcompop,
  mail_recipient TYPE swotobjid,
  mail_sender TYPE swotobjid.
*Workareas for PARAMETER-TABLE and EXCEPTION-TABLE
  DATA :
  ptab_line TYPE abap_func_parmbind,
  etab_line TYPE abap_func_excpbind.
*For PDF Output
  DATA :
  v_binfilesize TYPE i,
  lt_pdf_tab TYPE STANDARD TABLE OF tline,
  lt_otf TYPE STANDARD TABLE OF itcoo,
  file_size TYPE i.



*First find the FM available for the form.
  CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
  EXPORTING formname = form_name
* variant = ' '
* direct_call = ' '
  IMPORTING fm_name = v_fm_name
  EXCEPTIONS no_form = 1
  no_function_module = 2
  OTHERS = 3.
  IF sy-subrc <> 0.
    CASE sy-subrc.
      WHEN '1'.
        RAISE no_form.
        EXIT.
      WHEN '2'.
        RAISE no_function_module.
        EXIT.
      WHEN '3'.
        RAISE unknown_exception.
        EXIT.
      WHEN OTHERS.
        RAISE unknown_exception.
        EXIT.
    ENDCASE.
  ENDIF.

  IF NOT nast IS INITIAL.
*Nast values are supplied
    IF NOT program IS INITIAL.
*Program name is supplied
      CALL FUNCTION 'WFMC_PREPARE_SMART_FORM'
      EXPORTING
      pi_nast = nast
* PI_ADDR_KEY = ' '
* PI_COUNTRY = ' '
      pi_repid = program
      pi_screen = ' '
      IMPORTING
      pe_returncode = v_returncode
      pe_itcpo = lwa_itcpo
      pe_device = v_tddevice
      pe_recipient = v_recipient
      pe_sender = v_sender
      .
      IF v_returncode = 0.
        MOVE-CORRESPONDING lwa_itcpo TO output_options.
        control_parameters-device = v_tddevice.
        control_parameters-no_dialog = 'X'.
        control_parameters-preview = 'X'.
        control_parameters-getotf = lwa_itcpo-tdgetotf.
        control_parameters-langu = nast-spras.
        mail_recipient = v_recipient.
        mail_sender = v_sender.
      ENDIF.
    ENDIF.
  ENDIF.

*Now check if PDF output is required
  IF convert_to_pdf = 'X'.
    control_parameters-getotf = 'X'.
    IF filename IS INITIAL.
      RAISE invalid_filename.
    ENDIF.
  ELSE.
    control_parameters-getotf = space.
  ENDIF.

*Now fill the PARAMETER-TABLE and EXCEPTION-TABLE of the dynamic function module.
  READ TABLE ptab INTO ptab_line WITH KEY name = 'CONTROL_PARAMETERS'.
  IF sy-subrc NE 0.
    ptab_line-name = 'CONTROL_PARAMETERS'.
    ptab_line-kind = abap_func_exporting.
    GET REFERENCE OF control_parameters INTO ptab_line-value.
    INSERT ptab_line INTO TABLE ptab.
  ENDIF.

  READ TABLE ptab INTO ptab_line WITH KEY name = 'MAIL_RECIPIENT'.
  IF sy-subrc NE 0.
    ptab_line-name = 'MAIL_RECIPIENT'.
    ptab_line-kind = abap_func_exporting.
    GET REFERENCE OF mail_recipient INTO ptab_line-value.
    INSERT ptab_line INTO TABLE ptab.
  ENDIF.

  READ TABLE ptab INTO ptab_line WITH KEY name = 'MAIL_SENDER'.
  IF sy-subrc NE 0.
    ptab_line-name = 'MAIL_SENDER'.
    ptab_line-kind = abap_func_exporting.
    GET REFERENCE OF mail_sender INTO ptab_line-value.
    INSERT ptab_line INTO TABLE ptab.
  ENDIF.

  READ TABLE ptab INTO ptab_line WITH KEY name = 'OUTPUT_OPTIONS'.
  IF sy-subrc NE 0.
    ptab_line-name = 'OUTPUT_OPTIONS'.
    ptab_line-kind = abap_func_exporting.
    GET REFERENCE OF output_options INTO ptab_line-value.
    INSERT ptab_line INTO TABLE ptab.
  ENDIF.

  READ TABLE ptab INTO ptab_line WITH KEY name = 'JOB_OUTPUT_INFO'.
  IF sy-subrc NE 0.
    ptab_line-name = 'JOB_OUTPUT_INFO'.
    ptab_line-kind = abap_func_importing.
    GET REFERENCE OF job_output_info INTO ptab_line-value.
    INSERT ptab_line INTO TABLE ptab.
  ENDIF.


*Fill the EXCEPTION-TABLE
  READ TABLE etab INTO etab_line WITH TABLE KEY name = 'FORMATTING_ERROR'.
  IF sy-subrc NE 0.
    etab_line-name = 'FORMATTING_ERROR'.
    etab_line-value = 1.
    INSERT etab_line INTO TABLE etab.
  ENDIF.
  READ TABLE etab INTO etab_line WITH TABLE KEY name = 'INTERNAL_ERROR'.
  IF sy-subrc NE 0.
    etab_line-name = 'INTERNAL_ERROR'.
    etab_line-value = 2.
    INSERT etab_line INTO TABLE etab.
  ENDIF.

  READ TABLE etab INTO etab_line WITH TABLE KEY name = 'SEND_ERROR'.
  IF sy-subrc NE 0.
    etab_line-name = 'SEND_ERROR'.
    etab_line-value = 3.
    INSERT etab_line INTO TABLE etab.
  ENDIF.

  READ TABLE etab INTO etab_line WITH TABLE KEY name = 'USER_CANCELED'.
  IF sy-subrc NE 0.
    etab_line-name = 'USER_CANCELED'.
    etab_line-value = 4.
    INSERT etab_line INTO TABLE etab.
  ENDIF.

  READ TABLE etab INTO etab_line WITH TABLE KEY name = 'OTHERS'.
  IF sy-subrc NE 0.
    etab_line-name = 'OTHERS'.
    etab_line-value = 5.
    INSERT etab_line INTO TABLE etab.
  ENDIF.

*Call the smartform function module
  CALL FUNCTION v_fm_name
    PARAMETER-TABLE
      ptab
    EXCEPTION-TABLE
      etab.

*Convert to PDF if required
  lt_otf[] = job_output_info-otfdata[].
  IF LINES( lt_otf ) GT 0.
    CALL FUNCTION 'CONVERT_OTF'
    EXPORTING
    format = 'PDF'
    max_linewidth = 132
* ARCHIVE_INDEX = ' '
* COPYNUMBER = 0
* ASCII_BIDI_VIS2LOG = ' '
* PDF_DELETE_OTFTAB = ' '
    IMPORTING
    bin_filesize = v_binfilesize
* BIN_FILE =
    TABLES
    otf = lt_otf
    lines = lt_pdf_tab
    EXCEPTIONS
    err_max_linewidth = 1
    err_format = 2
    err_conv_not_possible = 3
    err_bad_otf = 4
    OTHERS = 5
    .
    IF sy-subrc <> 0.
*Raise exceptions in case OTF is not generated
      CASE sy-subrc.
        WHEN 1.
          RAISE err_max_linewidth.
        WHEN 2.
          RAISE err_format.
        WHEN 3.
          RAISE err_conv_not_possible.
        WHEN 4.
          RAISE err_bad_otf.
        WHEN OTHERS.
          RAISE unknown_exception.
      ENDCASE.
    ELSE.
*Download the converted PDF data to your local PC.
      CALL FUNCTION 'GUI_DOWNLOAD'
      EXPORTING
      bin_filesize = v_binfilesize
      filename = filename
      filetype = 'BIN'
* APPEND = ' '
* WRITE_FIELD_SEPARATOR = ' '
* HEADER = '00'
* TRUNC_TRAILING_BLANKS = ' '
* WRITE_LF = 'X'
* COL_SELECT = ' '
* COL_SELECT_MASK = ' '
* DAT_MODE = ' '
* CONFIRM_OVERWRITE = ' '
* NO_AUTH_CHECK = ' '
* CODEPAGE = ' '
* IGNORE_CERR = ABAP_TRUE
* REPLACEMENT = '#'
* WRITE_BOM = ' '
* TRUNC_TRAILING_BLANKS_EOL = 'X'
* WK1_N_FORMAT = ' '
* WK1_N_SIZE = ' '
* WK1_T_FORMAT = ' '
* WK1_T_SIZE = ' '
      IMPORTING
      filelength = file_size
      TABLES
      data_tab = lt_pdf_tab
* FIELDNAMES =
      EXCEPTIONS
      file_write_error = 1
      no_batch = 2
      gui_refuse_filetransfer = 3
      invalid_type = 4
      no_authority = 5
      unknown_error = 6
      header_not_allowed = 7
      separator_not_allowed = 8
      filesize_not_allowed = 9
      header_too_long = 10
      dp_error_create = 11
      dp_error_send = 12
      dp_error_write = 13
      unknown_dp_error = 14
      access_denied = 15
      dp_out_of_memory = 16
      disk_full = 17
      dp_timeout = 18
      file_not_found = 19
      dataprovider_exception = 20
      control_flush_error = 21
      OTHERS = 22
      .
      IF sy-subrc NE 0.
        RAISE file_write_error.
      ENDIF.
    ENDIF.
  ENDIF.
ENDMETHOD.                    "


PART 2 - Sample program

This code below is a sample program where this class has been used

Code:
*----------------------------------------------------------------------*
* Report SF_EXAMPLE_01
*----------------------------------------------------------------------*
* Printing of documents using Smart Forms
*----------------------------------------------------------------------*
report sf_example_01.

DATA: carr_id TYPE sbook-carrid,
fm_name TYPE rs38l_fnam,
ref_sf TYPE REF TO zdynamic_sf_print,
ptab TYPE abap_func_parmbind_tab,
ptab_line TYPE abap_func_parmbind,
etab TYPE abap_func_excpbind_tab,
etab_line TYPE abap_func_excpbind,
v_file TYPE string.



PARAMETER: p_custid TYPE scustom-id DEFAULT 1.
SELECT-OPTIONS: s_carrid FOR carr_id DEFAULT 'LH' TO 'LH'.
PARAMETER: p_form TYPE tdsfname DEFAULT 'SF_EXAMPLE_01'.
PARAMETER : disp_pdf AS CHECKBOX, "Display smartform as PDF
filename LIKE rlgrap-filename DEFAULT 'C:\CUSTOMERS.PDF'.
DATA: customer TYPE scustom,
bookings TYPE ty_bookings,
connections TYPE ty_connections,
user_flag TYPE xfeld VALUE 'X'.

* get data
SELECT SINGLE * FROM scustom INTO customer WHERE id = p_custid.
CHECK sy-subrc = 0.
SELECT * FROM sbook INTO TABLE bookings
WHERE customid = p_custid
AND carrid IN s_carrid
ORDER BY PRIMARY KEY.
SELECT * FROM spfli INTO TABLE connections
FOR ALL ENTRIES IN bookings
WHERE carrid = bookings-carrid
AND connid = bookings-connid
ORDER BY PRIMARY KEY.

*Fill the dynamic parameters

ptab_line-name = 'CUSTOMER'.
ptab_line-kind = abap_func_exporting.
GET REFERENCE OF customer INTO ptab_line-value.
INSERT ptab_line INTO TABLE ptab.

ptab_line-name = 'BOOKINGS'.
ptab_line-kind = abap_func_exporting.
GET REFERENCE OF bookings INTO ptab_line-value.
INSERT ptab_line INTO TABLE ptab.

ptab_line-name = 'CONNECTIONS'.
ptab_line-kind = abap_func_exporting.
GET REFERENCE OF connections INTO ptab_line-value.
INSERT ptab_line INTO TABLE ptab.

ptab_line-name = 'USER_SETTINGS'.
ptab_line-kind = abap_func_exporting.
GET REFERENCE OF user_flag INTO ptab_line-value.
INSERT ptab_line INTO TABLE ptab.

*Create the class object
CREATE OBJECT ref_sf
EXPORTING
i_form_name = p_form
* I_PROGRAM =
* I_NAST =
.

IF disp_pdf = space. "Smartform will be printed.
  CALL METHOD ref_sf->print_form
* EXPORTING
* CONVERT_TO_PDF = SPACE
* FILENAME =
  CHANGING
  ptab = ptab
  etab = etab
  EXCEPTIONS
  no_form = 1
  no_function_module = 2
  unknown_exception = 3
  invalid_filename = 4
  err_max_linewidth = 5
  err_format = 6
  err_conv_not_possible = 7
  err_bad_otf = 8
  file_write_error = 9
  OTHERS = 10
  .

ELSE.
  CLEAR v_file.
  v_file = filename.
*Smartform will be displayed as PDF
  CALL METHOD ref_sf->print_form
    EXPORTING
      convert_to_pdf        = 'X'
      filename              = v_file
    CHANGING
      ptab                  = ptab
      etab                  = etab
    EXCEPTIONS
      no_form               = 1
      no_function_module    = 2
      unknown_exception     = 3
      invalid_filename      = 4
      err_max_linewidth     = 5
      err_format            = 6
      err_conv_not_possible = 7
      err_bad_otf           = 8
      file_write_error      = 9
      OTHERS                = 10.
ENDIF.


Note : The dynamic parameters can be of the folllowing types :


1.abap_func_exporting: exporting
2.abap_func_importing: importing
3.abap_func_tables : tables
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Smartforms, SapScripts, PDF All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG.
SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG.
Every effort is made to ensure content integrity. Use information on this site at your own risk.