Thursday, April 7, 2011

Upload program to create Class

This is an upload program to create Class in SAP PM module. This program uploads the data file in excel format from the presentation server. BDC session is created for uploading the data. Session log can be checked using tcode SM35. Entire program is given as below. Selection Screen *Output Entire Program logic is given as below.

REPORT zpmcipr0002
NO STANDARD PAGE HEADING LINE-SIZE 255.

TYPE-POOLS: truxs.
************************************************************************
* Struture Declaration *
************************************************************************
TYPES: BEGIN OF ty_data,
idef(1) TYPE c, "Indentifier
class TYPE rmclm-class, "Class
klart TYPE rmclm-klart, "Class Type
klbez TYPE rmclm-klbez, "Description
statu TYPE rmclm-statu, "Status
vondt TYPE rmclm-vondt, "Valid from
merkma TYPE rmclm-merkma, "Characteristics
END OF ty_data.
************************************************************************
* Intrnal Table Declaration *
************************************************************************
DATA: it_data TYPE STANDARD TABLE OF ty_data,
it_datah TYPE STANDARD TABLE OF ty_data,
bdcdata TYPE STANDARD TABLE OF bdcdata WITH HEADER LINE,
it_raw TYPE truxs_t_text_data,
ctumode LIKE ctu_params-dismode,
wa_data TYPE ty_data,
wa_datah TYPE ty_data,
l_ascii_list(200) TYPE c OCCURS 0 WITH HEADER LINE,
l_abap_list TYPE abaplist OCCURS 0 WITH HEADER LINE.
************************************************************************
* Constants Declaration *
************************************************************************
CONSTANTS: c_bdcname LIKE apqi-groupid VALUE 'CL01_UPLOAD',
c_tcode LIKE tstc-tcode VALUE 'CL01'.
************************************************************************
* Selection Screen *
************************************************************************
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME.
PARAMETERS : p_file LIKE rlgrap-filename OBLIGATORY .
SELECTION-SCREEN END OF BLOCK b1.
************************************************************************
* At Selection Screen *
************************************************************************
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

CALL FUNCTION 'F4_FILENAME'
EXPORTING
program_name = syst-cprog
dynpro_number = syst-dynnr
field_name = ' '
IMPORTING
file_name = p_file.
************************************************************************
* Start of Selection *
************************************************************************
START-OF-SELECTION.
*Read the data from the file to internl table.
PERFORM read_data.
*Create Class
PERFORM create_class.
*&---------------------------------------------------------------------*
*& Form READ_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM read_data .
DATA: l_date(8) TYPE c,
l_tabix LIKE sy-tabix.
CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
EXPORTING
* I_FIELD_SEPERATOR =
i_line_header = 'X'
i_tab_raw_data = it_raw
i_filename = p_file
TABLES
i_tab_converted_data = it_data[]
* EXCEPTIONS
* CONVERSION_FAILED = 1
* OTHERS = 2
.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
RAISE upload_problem.
ENDIF.
*Begin of insert 22.03.2011
*Convert date format from YYYYMMDD to MMDDYYYY
IF it_data IS NOT INITIAL.
LOOP AT it_data INTO wa_data.
l_tabix = sy-tabix.
l_date+0(2) = wa_data-vondt+6(2).
l_date+2(2) = wa_data-vondt+4(2).
l_date+4(4) = wa_data-vondt+0(4).
wa_data-vondt = l_date.
MODIFY it_data FROM wa_data INDEX l_tabix.
CLEAR: l_tabix,l_date,wa_data.
ENDLOOP.
ENDIF.
*End of insert 22.03.2011
ENDFORM. " READ_DATA
*&---------------------------------------------------------------------*
*& Form CREATE_CLASS
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM create_class .
DATA : l_pos TYPE i,
l_posnr TYPE sposn,
l_postp(20) TYPE c,
l_idnrk(20) TYPE c,
l_menge(20) TYPE c,
l_field(20) TYPE c.
"added by sachin 11.02.2011
TYPES : BEGIN OF ls_data.
INCLUDE TYPE ty_data.
TYPES : idx TYPE i.
TYPES : END OF ls_data.
DATA : lt_data TYPE TABLE OF ls_data.
FIELD-SYMBOLS : ,,.
"end of addition.
**** SORT it_data BY class.
LOOP AT it_data INTO wa_data
WHERE idef = 'H'.
DELETE it_data INDEX sy-tabix.
APPEND wa_data TO it_datah.
CLEAR wa_data.
ENDLOOP.

**** SORT : it_datah BY class,
**** it_data BY class.
"added by sachin 11.02.2011
lt_data[] = it_data[].
LOOP AT lt_data ASSIGNING .
ASSIGN COMPONENT 'CLASS' OF STRUCTURE TO .
IF sy-subrc EQ 0.
AT NEW .
CLEAR l_pos.
ENDAT.
l_pos = l_pos + 1.
ASSIGN COMPONENT 'IDX' OF STRUCTURE TO .
IF sy-subrc EQ 0.
= l_pos.
ENDIF.
ENDIF.
ENDLOOP.

CLEAR it_data[].
SORT lt_data BY class idx DESCENDING.

LOOP AT lt_data ASSIGNING .
MOVE-CORRESPONDING TO wa_data.
APPEND wa_data TO it_data.
CLEAR wa_data.
ENDLOOP.
"end of addition.

*Create Session
PERFORM open_session USING c_bdcname.

*perform open_group.
LOOP AT it_datah INTO wa_datah.

PERFORM bdc_dynpro USING 'SAPLCLMO' '0100'.
PERFORM bdc_field USING 'BDC_CURSOR'
'RMCLM-KLART'.
PERFORM bdc_field USING 'BDC_OKCODE'
'/00'.
PERFORM bdc_field USING 'RMCLM-CLASS'
wa_datah-class. "'TST_CLPI_J004'.
PERFORM bdc_field USING 'RMCLM-KLART'
wa_datah-klart. "'002'.
PERFORM bdc_dynpro USING 'SAPLCLMO' '0100'.
PERFORM bdc_field USING 'BDC_OKCODE'
'/00'.

PERFORM bdc_dynpro USING 'SAPLCLMO' '7777'.
PERFORM bdc_field USING 'BDC_OKCODE'
'=MERK'.
PERFORM bdc_field USING 'BDC_CURSOR'
'RMCLM-BISDT'.
PERFORM bdc_field USING 'RMCLM-KLBEZ'
wa_datah-klbez. "'Test Class'.
PERFORM bdc_field USING 'RMCLM-STATU'
wa_datah-statu. "'1'.
*Begin of commenting
* PERFORM bdc_field USING 'RMCLM-KLAGR'
* 'CLP_CLASS'.
*End of commenting
PERFORM bdc_field USING 'RMCLM-VONDT'
wa_datah-vondt. "'15.09.2010'.
PERFORM bdc_field USING 'RMCLM-BISDT'
'31.12.9999'.
PERFORM bdc_field USING 'RMCLM-PRAUS'
'X'.
PERFORM bdc_dynpro USING 'SAPLCLMO' '7777'.
PERFORM bdc_field USING 'BDC_CURSOR'
'RMCLM-MERKMA(03)'.
**** IF l_pos is INITIAL .
**** l_pos = 1.
**** ENDIF.

LOOP AT it_data INTO wa_data
WHERE class = wa_datah-class
AND idef = 'D'.
DELETE it_data INDEX sy-tabix.

"added by sachin 11.02.2011
PERFORM bdc_dynpro USING 'SAPLCLMO' '7777'.
PERFORM bdc_field USING 'BDC_OKCODE'
'=SMEI'.

PERFORM bdc_field USING 'RMCLM-MERKMA(01)'"l_field "'RMCLM-MERKMA(01)'
wa_data-merkma. "'J001CHR02'.
**** l_pos = l_pos + 1.


PERFORM bdc_field USING 'BDC_CURSOR'
'RMCLM-MERKMA(01)'."l_field.
"end of addition.
CLEAR wa_data.


ENDLOOP.

PERFORM bdc_dynpro USING 'SAPLCLMO' '7777'.
PERFORM bdc_field USING 'BDC_OKCODE'
'SAVE'.
*perform bdc_transaction using 'CL01'.
* CALL TRANSACTION 'CL01' USING BDCDATA MODE 'A'.

PERFORM bdc_insert TABLES bdcdata
USING c_tcode.
REFRESH: bdcdata.
CLEAR : wa_datah,l_pos.

ENDLOOP.
* Close the session
PERFORM close_session.
* Process the session
PERFORM bdc_submit.
*perform close_group.
ENDFORM. " CREATE_CLASS

*----------------------------------------------------------------------*
* Start new screen *
*----------------------------------------------------------------------*
FORM bdc_dynpro USING program dynpro.
CLEAR bdcdata.
bdcdata-program = program.
bdcdata-dynpro = dynpro.
bdcdata-dynbegin = 'X'.
APPEND bdcdata.
ENDFORM. "bdc_dynpro

*----------------------------------------------------------------------*
* Insert field *
*----------------------------------------------------------------------*
FORM bdc_field USING fnam fval.
CLEAR bdcdata.
bdcdata-fnam = fnam.
IF fnam CS 'MENGE'.
bdcdata-fval = fval.
CONDENSE bdcdata-fval.
ELSE.
bdcdata-fval = fval.
ENDIF.
APPEND bdcdata.
ENDFORM. "bdc_field

*&---------------------------------------------------------------------*
*& Form open_session *
*&---------------------------------------------------------------------*
FORM open_session USING p_sess.
CALL FUNCTION 'BDC_OPEN_GROUP'
EXPORTING
group = p_sess
keep = 'X'
user = sy-uname
* importing
* qid =
EXCEPTIONS
client_invalid = 1
destination_invalid = 2
group_invalid = 3
group_is_locked = 4
holddate_invalid = 5
internal_error = 6
queue_error = 7
running = 8
system_lock_error = 9
user_invalid = 10
OTHERS = 11.
ENDFORM. "open_session

*&---------------------------------------------------------------------*
*& Form bdc_insert *
*&---------------------------------------------------------------------*
FORM bdc_insert TABLES bdcdata
STRUCTURE bdcdata
USING v_tcode.
CALL FUNCTION 'BDC_INSERT'
EXPORTING
tcode = v_tcode
TABLES
dynprotab = bdcdata
EXCEPTIONS
internal_error = 1
not_open = 2
queue_error = 3
tcode_invalid = 4
printing_invalid = 5
posting_invalid = 6
OTHERS = 7.
ENDFORM. "bdc_insert

*&---------------------------------------------------------------------*
*& Form close_session
*&---------------------------------------------------------------------*
FORM close_session .
CALL FUNCTION 'BDC_CLOSE_GROUP'
EXCEPTIONS
not_open = 1
queue_error = 2
OTHERS = 3.
ENDFORM. " close_session

*&---------------------------------------------------------------------*
*& Form bdc_submit
*&---------------------------------------------------------------------*
* Process the sessions created
*----------------------------------------------------------------------*
FORM bdc_submit .
SUBMIT rsbdcsub AND RETURN EXPORTING LIST TO MEMORY
WITH mappe = c_bdcname
WITH z_verarb = 'X'
WITH fehler = ' '.

COMMIT WORK AND WAIT.

* Retrieve the output from memory and place into l_abap_list
CLEAR l_abap_list.
FREE l_abap_list.

CALL FUNCTION 'LIST_FROM_MEMORY'
TABLES
listobject = l_abap_list
EXCEPTIONS
not_found = 1
OTHERS = 2.
IF sy-subrc EQ 0.
CLEAR l_ascii_list.
FREE l_ascii_list.
CALL FUNCTION 'LIST_TO_ASCI'
EXPORTING
list_index = -1
TABLES
listasci = l_ascii_list
listobject = l_abap_list
EXCEPTIONS
empty_list = 1
list_l_indexex_invalid = 2
OTHERS = 3.
IF sy-subrc NE 0.
* MESSAGE W368 WITH TEXT-006.
ELSE.
LOOP AT l_ascii_list.
WRITE: / l_ascii_list.
ENDLOOP.
SKIP.
WRITE: / text-002 , '"',c_bdcname,'"', text-003.
ENDIF.
ENDIF.

ENDFORM. " bdc_submit

*&---------------------------------------------------------------------*
*& Form write.
*&---------------------------------------------------------------------*
FORM write.
LOOP AT l_ascii_list.
WRITE: / l_ascii_list.
ENDLOOP.
SKIP.
WRITE: / text-004 , '"',c_bdcname,'"', text-005.
ENDFORM. "WRITE

No comments:

Post a Comment