Posted: Tue Sep 04, 2007 5:30 pm Post subject: Build a dynamic select and internel tables
Applies to:
ABAP / BDC
Summary
ABAP Code Sample that uses dynamic programming techniques to build a dynamic select and internal table for Load output conditions.
Created on:6 March 2006
Code:
REPORT zsdb0010_output_conditions
NO STANDARD PAGE HEADING
LINE-SIZE 132
LINE-COUNT 65(0)
MESSAGE-ID zz.
TABLES: t685, "Conditions: Types
t682i, "Access Sequences (Generated Form)
t682z, "Access Sequences (Fields)
dd03l.
*Work area for the inbound file for unix
DATA: BEGIN OF x_inrec,
line(1024) TYPE c,
END OF x_inrec.
*Work area for the inbound file from PC
DATA: BEGIN OF it_xinrec OCCURS 0,
line(1024) TYPE c,
END OF it_xinrec.
* Table for getting the field name and lenght.
DATA: BEGIN OF it_vakey_fields OCCURS 0,
fieldname LIKE t682z-zifna, "Field for condition table
fieldlen(3) TYPE n, "Field lenght
offset(3) TYPE n, "Field Offset
END OF it_vakey_fields.
*processing table for inbound file.
DATA: BEGIN OF x_comm,
parnr LIKE nach-parnr, "Message partner
END OF x_comm.
* final BDC table.
DATA: BEGIN OF it_data OCCURS 0,
vakey LIKE nach-vakey, "Variable key 100 bytes
parnr LIKE nach-parnr, ""Message partner
END OF it_data.
*populating t682z values.
DATA: it_t682z LIKE t682z OCCURS 0 WITH HEADER LINE.
*getting value of the radio button
DATA: v_access_step(2) TYPE n.
* check for the value
DATA: v_step_found TYPE c.
* Get field length
DATA: v_vakey_length TYPE i.
*get number of messages.
DATA: g_lines TYPE i,
* Message
g_msg(100).
* File name
DATA: v_filename TYPE string.
* Get the condition table
DATA: v_cond_table(4) TYPE c.
* Error for subrc checking
DATA: v_error LIKE sy-subrc.
*----------------------------------------------------------------------*
* data definition
*----------------------------------------------------------------------*
* Batchinputdata of single transaction
DATA: bdcdata LIKE bdcdata OCCURS 0 WITH HEADER LINE.
* messages of call transaction
DATA: messtab LIKE bdcmsgcoll OCCURS 0 WITH HEADER LINE.
* Error table
DATA: BEGIN OF it_error OCCURS 0,
vakey LIKE nach-vakey, "Variable key 100 bytes
parnr LIKE nach-parnr, "Message partner
message(100), "Message
END OF it_error.
CONSTANTS: c_usage LIKE t685-kvewe VALUE 'B', "Usage of the
"condition table
c_true(1) TYPE c VALUE 'X', "Check for a record
c_n(1) TYPE c VALUE 'N', "No screen mode
c_s(1) TYPE c VALUE 'S'. "Update mode
************************************************************************
*selection-screen
************************************************************************
SELECTION-SCREEN BEGIN OF BLOCK a01 WITH FRAME TITLE text-001.
PARAMETERS: p_kappl LIKE t685-kappl OBLIGATORY, "Application
p_kschl LIKE nach-kschl OBLIGATORY, "Condition type
p_kotab LIKE t682i-kotabnr OBLIGATORY, "Condition table
p_parvw LIKE NACH-PARVW. "Partner function
PARAMETERS: pr_pc RADIOBUTTON GROUP g1, "PC upload
pr_unix RADIOBUTTON GROUP g1. "Unix Upload
PARAMETERS: p_infile LIKE rlgrap-filename OBLIGATORY. "Input file path
SELECTION-SCREEN END OF BLOCK a01.
* Note: for the Print out Block Validation Not required.
SELECTION-SCREEN BEGIN OF BLOCK a02 WITH FRAME TITLE text-002.
PARAMETERS: p_comm AS CHECKBOX. "Communication
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 1(31) text-s01 FOR FIELD p_ldest.
PARAMETERS: p_ldest LIKE nach-ldest. "Output Device
SELECTION-SCREEN POSITION 75.
PARAMETERS: p_dimme AS CHECKBOX. "Print immediate
SELECTION-SCREEN COMME NT 78(20) text-s08 FOR FIELD p_dimme.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 1(31) text-s02 FOR FIELD p_anzal.
PARAMETERS: p_anzal(2) TYPE n. "Number of
"Messages
SELECTION-SCREEN POSITION 75.
PARAMETERS: p_delet AS CHECKBOX. "Release after
"output
SELECTION-SCREEN COMME NT 78(20) text-s03 FOR FIELD p_delet.
SELECTION-SCREEN END OF LINE.
PARAMETERS: p_dsnam LIKE nach-dsnam, "Spool request
"name
p_dsuf1 LIKE nach-dsuf1, "Suffix1
p_dsuf2 LIKE nach-dsuf2, "Suffix1
p_tdoco LIKE nach-tdocover, "SAP Cover page
p_tdrec LIKE nach-tdreceiver, "Recipient
p_tddiv LIKE nach-tddivision, "Department on
"cover page
p_tdcov LIKE nach-tdcovtitle, "Cover Page Text
p_tdaut LIKE nach-tdautority, "Authorization
p_tdarm LIKE nach-tdarmod. "Storage Mode
SELECTION-SCREEN END OF BLOCK a02.
************************************************************************
*at selection-screen on field.
***********************************************************************
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_infile.
*--- Value Request Functionality for PC File
PERFORM value_request_for_pc_file.
AT SELECTION-SCREEN.
* check for the records in the database
PERFORM read_access_tables.
* Screen validations
PERFORM addl_screen_validations.
***********************************************************************
* START-OF-SELECTION.
***********************************************************************
START-OF-SELECTION.
* Get the field name and field value.
PERFORM determine_variable_key.
* Get the fill from PC or Unix
PERFORM read_input_file.
* Upload records to the database
PERFORM upload_conditions.
************************************************************************
* End of selection.
************************************************************************
END-OF-SELECTION.
* Error check
IF v_error <> 0.
STOP.
ENDIF.
* Get out put of the report
PERFORM write_reoprt.
************************************************************************
* Top-of-page.
************************************************************************
TOP-OF-PAGE.
* Standard header.
CALL FUNCTION 'Z_STEELCASE_HEADER'
* EXPORTING
* HEADING_1 =
* HEADING_2 =
.
*&---------------------------------------------------------------------*
*& Form READ_ACCESS_TABLES
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM read_access_tables.
* Get Data from t685
SELECT SINGLE * FROM t685
WHERE kvewe = c_usage "B
AND kappl = p_kappl "Application
AND kschl = p_kschl. "Condition type
IF sy-subrc<> 0.
MESSAGE e002 WITH 'Incorrect application or output type'(004).
ENDIF.
CLEAR v_access_step.
CLEAR v_step_found.
* Get value of kolnr
SELECT * FROM t682i
WHERE kvewe = c_usage "Usage of the condition table
AND kappl = p_kappl "Application
AND kozgf = t685-kozgf. "Accesssequence
ADD 1 TO v_access_step.
* IF t682i-kolnr = p_kolnr.
IF t682i-kotabnr = p_kotab.
v_step_found = c_true.
EXIT.
ENDIF.
ENDSELECT.
IF sy-subrc<> 0 OR
v_step_found IS INITIAL.
MESSAGE e002 WITH 'Incorrect condition table'(005).
ENDIF.
v_cond_table+0(1) = 'B'.
v_cond_table+1(3) = t682i-kotabnr.
*
SELECT * FROM t682z
INTO TABLE it_t682z
WHERE kvewe = c_usage "Usage of the condition table
AND kappl = p_kappl "Application
AND kozgf = t685-kozgf "Access sequence
* AND kolnr = p_kolnr.
AND kolnr = t682i-kolnr. "Access number
IF sy-subrc<> 0.
MESSAGE e002 WITH 'Unable to find condition table'(006).
ENDIF.
ENDFORM. " READ_ACCESS_TABLES
*&---------------------------------------------------------------------*
*& Form DETERMINE_VARIABLE_KEY
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM determine_variable_key .
DATA: offset(3) TYPE n.
* Get the name of the field and the Length
CLEAR offset.
LOOP AT it_t682z INTO t682z.
SELECT SINGLE * FROM dd03l
WHERE tabname = t682z-qustr " Table name
AND fieldname = t682z-qufna. " Table field
IF sy-subrc <> 0.
EXIT.
ENDIF.
* Checking the lenght of the field and name
v_vakey_length = v_vakey_length + dd03l-intlen. "Internal Length
it_vakey_fields-fieldname = t682z-zifna. " Table name
it_vakey_fields-fieldlen = dd03l-intlen. " Table field
it_vakey_fields-offset = offset. " Off set
APPEND it_vakey_fields.
offset = offset + dd03l-intlen.
ENDLOOP.
ENDFORM. " DETERMINE_VARIABLE_KEY
*&---------------------------------------------------------------------*
*& Form READ_INPUT_FILE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM read_input_file .
* From unix
IF pr_unix = 'X'.
PERFORM open_dataset.
ELSE.
* From PC
PERFORM gui_upload.
ENDIF.
ENDFORM. " READ_INPUT_FILE
************************************************************************
* Subroutines
************************************************************************
*----------------------------------------------------------------------*
* Start new screen *
*----------------------------------------------------------------------*
FORM bdc_dynpro USING program dynpro.
CLEAR bdcdata.
bdcdata-program = program. "Program name
bdcdata-dynpro = dynpro. "Screen number
bdcdata-dynbegin = 'X'. "New Screen
APPEND bdcdata.
ENDFORM. "BDC_DYNPRO
*----------------------------------------------------------------------*
* Insert field *
*----------------------------------------------------------------------*
FORM bdc_field USING fnam fval.
CLEAR bdcdata.
bdcdata-fnam = fnam. "Field name
bdcdata-fval = fval. "Field Value
APPEND bdcdata.
ENDFORM. "BDC_FIELD
*&--------------------------------------------------------------------*
*& Form BDC_TRANSACTION
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*
* -->TCODE text
*---------------------------------------------------------------------*
FORM bdc_transaction USING tcode.
DATA: l_mstring(480).
REFRESH messtab.
CALL TRANSACTION tcode USING bdcdata
MODE c_n "No screen mode
UPDATE c_s "Synchronous update
MESSAGES INTO messtab.
LOOP AT messtab.
* If it says the condition record doesn't exist, that is ok.
* It is going to create a new record.
IF messtab-msgid = 'VK' AND
messtab-msgnr = '021'.
CONTINUE.
ENDIF.
* Populate error records
CLEAR g_lines.
DESCRIBE TABLE messtab LINES g_lines.
READ TABLE messtab INDEX g_lines TRANSPORTING ALL FIELDS.
* Get the error message
PERFORM format_message.
* Get record.
IF NOT g_msg IS INITIAL.
it_error-message = g_msg.
it_error-vakey = it_data-vakey.
it_error-parnr = it_data-parnr.
APPEND : it_error.
CLEAR : it_error.
ENDIF.
* ENDIF.
ENDLOOP.
CLEAR bdcdata.
REFRESH bdcdata.
ENDFORM. "BDC_TRANSACTION
*&---------------------------------------------------------------------*
*& Form upload_conditions
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM upload_conditions .
LOOP AT it_data.
* Checking if the Vakey is empty
CHECK NOT it_data-vakey IS INITIAL.
* Process BDC
PERFORM process_bdc.
ENDLOOP.
ENDFORM. " upload_conditions
*&---------------------------------------------------------------------*
*& Form process_bdc
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM process_bdc .
DATA: bdc_field_name(20).
DATA: vakey_fieldname(20).
DATA: vakey_fieldval(100).
DATA: field_count(3) TYPE n.
CONSTANTS: single_quote TYPE c VALUE ''''.
DATA: cond_rec_exists.
DATA: where_clause TYPE string.
DATA: kappl_temp LIKE nach-kappl. "Application
DATA: text1 TYPE nach-vakey. "variable key
DATA: text2 TYPE string. "variable key
* Get whear clause
CLEAR where_clause.
CONCATENATE 'KAPPL = P_KAPPL AND'
'KSCHL = P_KSCHL'
INTO where_clause
SEPARATED BY space.
LOOP AT it_vakey_fields.
*Get whear clause
CONCATENATE where_clause 'AND'
INTO where_clause
SEPARATED BY space.
* Getting the table and field name.
text1+0(it_vakey_fields-fieldlen) =
it_data-vakey+it_vakey_fields-offset(it_vakey_fields-fieldlen).
* Getting the table and field name.
CONCATENATE single_quote
text1+0(it_vakey_fields-fieldlen)
single_quote
INTO text2.
* Get where clause
CONCATENATE where_clause
it_vakey_fields-fieldname
'='
text2
INTO where_clause
SEPARATED BY space.
ENDLOOP.
* Dynamic select with dynamic where clause
CLEAR cond_rec_exists.
SELECT SINGLE kappl
INTO kappl_temp
FROM (v_cond_table)
WHERE (where_clause).
IF sy-subrc= 0.
cond_rec_exists = 'X'.
ENDIF.
* Passing value to the BDC table
PERFORM bdc_dynpro USING 'SAPMV13B' '0100'.
PERFORM bdc_field USING: 'BDC_OKCODE' '=ANTA',
'RV130-KAPPL' p_kappl,
'RV13B-KSCHL' p_kschl.
PERFORM bdc_dynpro USING 'SAPLV14A' '0100'.
CONCATENATE 'RV13B' t682i-kotabnr
INTO bdc_field_name.
* passing value to the new screen
PERFORM bdc_dynpro USING bdc_field_name '1000'.
* Passing filed name field value
PERFORM bdc_field USING: 'BDC_OKCODE' '=ONLI'.
CLEAR field_count.
LOOP AT it_vakey_fields.
ADD 1 TO field_count.
CONCATENATE 'F' field_count '-LOW'
INTO vakey_fieldname.
vakey_fieldval =
it_data-vakey+it_vakey_fields-offset(it_vakey_fields-fieldlen).
PERFORM bdc_field USING: vakey_fieldname vakey_fieldval.
ENDLOOP.
CONCATENATE '1' t682i-kotabnr
INTO bdc_field_name.
* PERFORM bdc_dynpro USING 'SAPMV13B' bdc_field_name.
** Go to a new page
* PERFORM bdc_field USING: 'BDC_OKCODE' '=NEWP'.
* In the new page we will always populate in the second line
PERFORM bdc_dynpro USING 'SAPMV13B' bdc_field_name.
PERFORM bdc_field USING: 'BDC_OKCODE' '/00'.
IF NOT p_parvw IS INITIAL.
PERFORM bdc_field USING: 'NACH-PARVW(01)' p_parvw.
ENDIF.
IF NOT it_data-parnr IS INITIAL.
PERFORM bdc_field USING: 'RV13B-PARNR(01)' it_data-parnr.
ENDIF.
PERFORM bdc_field USING: 'NACH-SPRAS(01)' 'EN'.
IF cond_rec_exists IS INITIAL.
LOOP AT it_vakey_fields.
CONCATENATE 'KOMB-' it_vakey_fields-fieldname '(01)'
INTO vakey_fieldname.
vakey_fieldval =
it_data-vakey+it_vakey_fields-offset(it_vakey_fields-fieldlen).
PERFORM bdc_field USING: vakey_fieldname vakey_fieldval.
ENDLOOP.
ENDIF.
* Communication required is checked.
IF p_comm = 'X'.
PERFORM bdc_dynpro USING 'SAPMV13B' bdc_field_name.
PERFORM bdc_field USING: 'BDC_OKCODE' '=KOMM',
'RV130-SELKZ(01)' 'X'.
PERFORM bdc_dynpro USING 'SAPMV13B' '0211'.
PERFORM bdc_field USING: 'BDC_OKCODE' '=SICH',
'NACH-LDEST' p_ldest,
'NACH-DIMME' p_dimme,
'NACH-ANZAL' p_anzal,
'NACH-DELET' p_delet,
'NACH-DSNAM' p_dsnam,
'NACH-DSUF1' p_dsuf1,
'NACH-DSUF2' p_dsuf2,
'NACH-TDOCOVER' p_tdoco,
'NACH-TDRECEIVER' p_tdrec,
'NACH-TDDIVISION' p_tddiv,
'NACH-TDCOVTITLE' p_tdcov,
'NACH-TDAUTORITY' p_tdaut.
IF NOT p_tdarm IS INITIAL.
PERFORM bdc_field USING: 'NACH-TDARMOD' p_tdarm.
ENDIF.
ELSE.
PERFORM bdc_field USING: 'BDC_OKCODE' '=SICH'.
ENDIF.
* Call tranastction
PERFORM bdc_transaction USING 'NACR'.
ENDFORM. " process_bdc
*&---------------------------------------------------------------------*
*& Form format_message
*&---------------------------------------------------------------------*
* Get the error message
*----------------------------------------------------------------------*
FORM format_message.
* Calling format_message to format the message
CLEAR g_msg.
CALL FUNCTION 'FORMAT_MESSAGE'
EXPORTING
id =messtab-msgid
lang = sy-langu
no = messtab-msgnr
v1 =messtab-msgv1
v2 =messtab-msgv2
v3 =messtab-msgv3
v4 =messtab-msgv4
IMPORTING
msg = g_msg
EXCEPTIONS
not_found = 1
OTHERS = 2.
IF sy-subrc<> 0.
g_msg = space.
ENDIF.
ENDFORM. " format_message
*&---------------------------------------------------------------------*
*& Form value_request_for_pc_file
*&---------------------------------------------------------------------*
* Description : Value Request Functionality for PC File
*----------------------------------------------------------------------*
FORM value_request_for_pc_file.
CALL FUNCTION 'F4_FILENAME'
EXPORTING
program_name = sy-cprog
dynpro_number = sy-dynnr
field_name = ' '
IMPORTING
file_name = p_infile.
ENDFORM. " value_request_for_pc_file
*&---------------------------------------------------------------------*
*& Form gui_upload
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM gui_upload .
v_filename = p_infile.
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
filename = v_filename
TABLES
data_tab = it_xinrec
EXCEPTIONS
file_open_error = 1
file_read_error = 2
no_batch = 3
gui_refuse_filetransfer = 4
invalid_type = 5
no_authority = 6
unknown_error = 7
bad_data_format = 8
header_not_allowed = 9
separator_not_allowed = 10
header_too_long = 11
unknown_dp_error = 12
access_denied = 13
dp_out_of_memory = 14
disk_full = 15
dp_timeout = 16
OTHERS = 17.
IF sy-subrc<> 0.
v_error = 4.
MESSAGE i002 WITH 'Cannot open input file'(007).
STOP.
ELSE.
LOOP AT it_xinrec INTO x_inrec.
IF sy-subrc <> 0.
EXIT.
ENDIF.
it_data-vakey = x_inrec+0(v_vakey_length).
x_comm = x_inrec+v_vakey_length.
it_data-parnr = x_comm-parnr.
APPEND it_data.
ENDLOOP.
ENDIF.
ENDFORM. " gui_upload
*&---------------------------------------------------------------------*
*& Form open_dataset
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM open_dataset .
OPEN DATASET p_infile FOR INPUT IN TEXT MODE ENCODING DEFAULT.
IF sy-subrc<> 0.
v_error = 4.
MESSAGE i002 WITH 'Cannot open input file'(003).
STOP.
ENDIF.
DO.
READ DATASET p_infile INTO x_inrec.
IF sy-subrc <> 0.
EXIT.
ENDIF.
it_data-vakey = x_inrec+0(v_vakey_length).
x_comm =x_inrec+v_vakey_length.
it_data-parnr = x_comm-parnr.
APPEND it_data.
ENDDO.
CLOSE DATASET p_infile.
ENDFORM. " open_dataset
*&---------------------------------------------------------------------*
*& Form write_reoprt
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM write_reoprt .
WRITE:/ sy-uline.
WRITE:/ sy-vline.
WRITE: 02 'Details of Data'(008),
22 sy-vline,
23 'Message partner'(009),
38 sy-vline,
39 'Partner function'(010),
56 sy-vline,
57 'Message'(015) ,
132 sy-vline.
WRITE:/ sy-uline.
LOOP AT it_error.
WRITE:/ sy-vline.
WRITE: 02 it_error-vakey,
22 sy-vline,
39 it_error-parnr,
56 sy-vline,
57 it_error-message ,
132 sy-vline.
ENDLOOP.
WRITE:/ sy-uline.
ELSE.
WRITE:/ 'All condition records successfully updated'(011).
ENDIF.
ENDFORM. " write_reoprt
*&---------------------------------------------------------------------*
*& Form addl_screen_validations
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
form addl_screen_validations .
DATA: parvw like tpar-parvw.
IF NOT P_PARVW IS INITIAL.
SELECT SINGLE PARVW INTO PARVW
FROM TPAR
WHERE PARVW = P_PARVW.
IF SY-SUBRC <> 0.
MESSAGE E002 WITH 'Invalid partner function'(012).
ENDIF.
ENDIF.
IF P_TDOCO IS INITIAL OR
P_TDOCO = 'X' OR
P_TDOCO = 'D'.
ELSE.
MESSAGE E002 WITH 'Invalid coverpage flag'(013).
ENDIF.
IF NOT P_TDARM IS INITIAL AND
P_TDARM > '3'.
MESSAGE E002 WITH 'Invalid archiving mode'(014).
ENDIF.
endform. " addl_screen_validations
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.