Posted: Tue Jan 15, 2008 1:51 pm Post subject: Calling smartforms dynamically using dynamic function param
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
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.
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 =
.
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.