‎2006 Nov 02 10:09 AM
Please go thru the following function module which will take the <b>costcenter</b> and <b>filename</b> as input and will create an XML file in the location specified with the parameter <b>filename .</b>
I tried with the following input values and got a type casting error.
Input values: Cost centre : *
File Name : c:\Temp\Employee.xml
FUNCTION z_tpg_employee_orgunit_xml.
*"----
""Local Interface:
*" IMPORTING
" VALUE(I_COSTCENTER) LIKE BAPIEMPLB-COSTCENTER DEFAULT ''
*" VALUE(I_FILENAME) TYPE STRING
*"----
Data declaration
Structures and internal tables
data ls_ressource TYPE typ_s_ressource.
data lt_ressource TYPE typ_t_ressource.
DATA lt_personal_data TYPE TABLE OF bapip0002b.
DATA ls_personal_data LIKE bapip0002b.
DATA lt_root_objects TYPE TABLE OF hrrootob.
data lt_root_tab TYPE TABLE OF gdstr.
data ls_root_objects TYPE hrrootob.
data ls_root_tab TYPE gdstr.
data ls_entry TYPE struc.
data ls_object TYPE objec.
data ls_root TYPE gdstr. "#EC NEEDED
DATA lt_orgunits TYPE TABLE OF typ_s_org.
DATA ls_orgunits TYPE typ_s_org.
DATA lt_communication TYPE typ_t_communication.
DATA lt_orgassignment TYPE TABLE OF bapip0001b.
DATA ls_orgassignment TYPE bapip0001b.
DATA lt_rate TYPE typ_t_rate.
DATA ls_rate TYPE typ_s_rate.
DATA ls_ppwla TYPE pbwla.
DATA lt_ppwla TYPE TABLE OF pbwla.
DATA ls_i0007 TYPE p0007.
DATA ls_return_0007 TYPE bapiret1.
Local helpfields
DATA lf_plvar LIKE objec-plvar.
data lf_contr_block_nr TYPE hrrhas-cb_nr.
data lf_lines TYPE sy-tabix.
DATA lf_begda TYPE sy-datum.
DATA lf_endda TYPE sy-datum.
DATA lf_return_code TYPE i.
Infotype structures
INFOTYPES 0008.
INFOTYPES 0007.
Field Symbols
FIELD-SYMBOLS: <ls_ressource> TYPE typ_s_ressource.
----
CLEAR gf_parent.
REFRESH gt_xml.
get current plan
CALL FUNCTION 'RH_GET_PLVAR'
IMPORTING
plvar = lf_plvar.
if sy-subrc ne 0.
lf_plvar = '01'.
endif.
get personal data
CALL FUNCTION 'BAPI_EMPLOYEE_GETDATA'
EXPORTING
costcenter = i_costcenter
TABLES
org_assignment = lt_orgassignment
personal_data = lt_personal_data
communication = lt_communication.
LOOP AT lt_orgassignment INTO ls_orgassignment.
clear ls_rate.
fill structure for orgunit read
MOVE 'O' TO ls_root_objects-otype.
MOVE ls_orgassignment-org_unit TO ls_root_objects-objid.
APPEND ls_root_objects TO lt_root_objects.
MOVE: ls_orgassignment-perno TO ls_ressource-perno,
ls_orgassignment-org_unit TO ls_ressource-org_unit,
ls_orgassignment-position TO ls_ressource-position.
APPEND ls_ressource TO lt_ressource.
IF ls_orgassignment-to_date = '99991231'.
get amount
CALL FUNCTION 'HR_GET_TOTAL_AMOUNT_P0008'
EXPORTING
pernr = ls_orgassignment-perno
IMPORTING
amount = ls_rate-amount_month
currency = ls_rate-waers
EXCEPTIONS
OTHERS = 1.
REFRESH p0008.
CALL FUNCTION 'HR_READ_INFOTYPE'
EXPORTING
pernr = ls_orgassignment-perno
infty = '0008'
begda = sy-datum
endda = sy-datum
TABLES
infty_tab = p0008
EXCEPTIONS
OTHERS = 1.
IF sy-subrc <> 0.
ENDIF.
get amount per hour
READ TABLE p0008 INDEX 1.
if not p0008-divgv is initial.
ls_rate-amount_hour = ls_rate-amount_month / p0008-divgv.
else.
refresh p0007.
CALL FUNCTION 'HR_READ_INFOTYPE'
EXPORTING
pernr = ls_orgassignment-perno
infty = '0007'
begda = sy-datum
endda = sy-datum
TABLES
infty_tab = p0007
EXCEPTIONS
OTHERS = 1.
if sy-subrc is initial.
ls_rate-amount_hour = ls_rate-amount_month / p0007-MOSTD.
endif.
endif.
get amount month
CALL FUNCTION 'HR_GET_TOTAL_AMOUNT_P0008'
EXPORTING
pernr = ls_orgassignment-perno
frequency = '01'
IMPORTING
amount = ls_rate-amount_month
currency = ls_rate-waers
EXCEPTIONS
OTHERS = 1.
MOVE: ls_orgassignment-to_date TO ls_rate-to_date,
ls_orgassignment-from_date TO ls_rate-from_date,
ls_orgassignment-perno TO ls_rate-perno.
APPEND ls_rate TO lt_rate.
ENDIF.
ENDLOOP.
SORT lt_root_objects BY objid.
DELETE ADJACENT DUPLICATES FROM lt_root_objects.
fill buffer with org objects
CALL FUNCTION 'STRUCTURE_BUILD_MULTIPLE_ROOTS'
EXPORTING
plvar = lf_plvar
wegid = 'O-O_DOWN'
sbegd = sy-datum
sendd = sy-datum
cbflag = 'X'
tflag = 'X'
IMPORTING
cb_nr = lf_contr_block_nr
TABLES
root_objects = lt_root_objects
root_tab = lt_root_tab
EXCEPTIONS
root_not_found = 1
wegid_not_found = 2
OTHERS = 3.
DESCRIBE TABLE lt_root_tab LINES lf_lines.
IF lf_lines > 1.
DELETE lt_root_tab INDEX 1.
ENDIF.
LOOP AT lt_root_tab INTO ls_root_tab.
CLEAR: ls_entry,
ls_orgunits.
get texts for root objects
CALL FUNCTION 'RH_READ_OBJECT'
EXPORTING
plvar = lf_plvar
otype = ls_root_tab-otype
objid = ls_root_tab-objid
begda = ls_root_tab-sbegd
endda = ls_root_tab-sendd
IMPORTING
short = ls_orgunits-org_short
stext = ls_orgunits-org_long
EXCEPTIONS
not_found = 1
OTHERS = 2.
ls_orgunits-otype = ls_root_tab-otype.
ls_orgunits-orgunit = ls_root_tab-objid.
ls_orgunits-level = 1.
get all subordinate objects
WHILE ls_entry-subrc = 0.
CALL FUNCTION 'STRUCTURE_ENTRY_GET'
EXPORTING
index = ls_root_tab-pstru
cb_nr = lf_contr_block_nr
IMPORTING
object = ls_object
root = ls_root
entry = ls_entry
EXCEPTIONS
OTHERS = 1.
IF ls_entry-subrc = 0.
MOVE: ls_root_tab-objid TO ls_orgunits-parent,
ls_object-otype TO ls_orgunits-otype,
ls_object-objid TO ls_orgunits-orgunit,
ls_object-short TO ls_orgunits-org_short,
ls_object-stext TO ls_orgunits-org_long,
ls_entry-level TO ls_orgunits-level.
APPEND ls_orgunits TO lt_orgunits.
ENDIF.
ENDWHILE.
ENDLOOP.
IF lt_orgunits IS INITIAL.
APPEND ls_orgunits TO lt_orgunits.
ENDIF.
DELETE ADJACENT DUPLICATES FROM lt_orgunits.
Check if any person is leader of orgunit
LOOP AT lt_ressource ASSIGNING <ls_ressource>.
DATA: ls_objects TYPE hrobject,
lt_objects TYPE TABLE OF hrobject,
lt_it001 TYPE TABLE OF string.
REFRESH lt_objects.
MOVE: '01' TO ls_objects-plvar,
'S' TO ls_objects-otype,
<ls_ressource>-position TO ls_objects-objid.
APPEND ls_objects TO lt_objects.
CALL FUNCTION 'RH_READ_INFTY_1001'
EXPORTING
subty = 'A012'
TABLES
i1001 = lt_it001
objects = lt_objects
EXCEPTIONS
nothing_found = 1
wrong_condition = 2
OTHERS = 3.
IF sy-subrc IS INITIAL.
<ls_ressource>-leadpos = 'X'.
ELSE.
CLEAR <ls_ressource>-leadpos.
ENDIF.
ENDLOOP.
start creating of xml stream
CLEAR gt_xml.
gs_xml-line = '<Request>'.
APPEND gs_xml TO gt_xml.
CLEAR gs_xml.
gs_xml-line = ' <ProcessHRDataSAP>'.
APPEND gs_xml TO gt_xml.
CLEAR gs_xml.
gs_xml-line = ' <HumanResources>'. "#EC NOTEXT
APPEND gs_xml TO gt_xml.
CLEAR gs_xml.
LOOP AT lt_personal_data INTO ls_personal_data.
PERFORM insert_personal_data USING lt_communication
lt_rate
CHANGING ls_personal_data
gt_xml.
ENDLOOP.
gs_xml-line = ' </HumanResources>'. "#EC NOTEXT
APPEND gs_xml TO gt_xml.
CLEAR gs_xml.
gs_xml-line = ' <OrganisationStructure>'. "#EC NOTEXT
APPEND gs_xml TO gt_xml.
LOOP AT lt_orgunits INTO ls_orgunits.
PERFORM insert_orgassignment_data USING ls_orgunits
CHANGING gt_xml.
ENDLOOP.
DESCRIBE TABLE lt_orgunits LINES lf_lines.
IF lf_lines > 1.
CLEAR gs_xml.
gs_xml-line = ' </OrganisationUnit>'. "#EC NOTEXT
APPEND gs_xml TO gt_xml.
ENDIF.
CLEAR gs_xml.
gs_xml-line = ' </OrganisationStructure>'. "#EC NOTEXT
APPEND gs_xml TO gt_xml.
Resource Mapping
CLEAR gs_xml.
gs_xml-line = ' <ResourceMapping>'. "#EC NOTEXT
APPEND gs_xml TO gt_xml.
LOOP AT lt_ressource INTO ls_ressource.
PERFORM insert_ressource_data USING ls_ressource
CHANGING gt_xml.
ENDLOOP.
CLEAR gs_xml.
gs_xml-line = ' </ResourceMapping>'. "#EC NOTEXT
APPEND gs_xml TO gt_xml.
CLEAR gs_xml.
gs_xml-line = ' </ProcessHRDataSAP>'. "#EC NOTEXT
APPEND gs_xml TO gt_xml.
CLEAR gs_xml.
The last tag must be the root closing tag --*
gs_xml-line = '</Request>'.
APPEND gs_xml TO gt_xml.
CLEAR gs_xml.
In case no file path is provided
IF i_filename IS INITIAL.
i_filename = 'C:\XML\EMPLOYEE.XML'.
ENDIF.
Drop the XML-FILE in Temp file folder
CALL METHOD cl_gui_frontend_services=>gui_download
EXPORTING
filename = 'C:\TEMP\EMPLOYEE.XML'
filetype = 'ASC'
CHANGING
data_tab = gt_xml
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 = 24.
IF sy-subrc <> 0.
ENDIF.
copy the XML-file from temp file folder to the filename
CALL METHOD cl_gui_frontend_services=>file_copy
EXPORTING
source = 'C:\TEMP\EMPLOYEE.XML'
destination = i_filename
overwrite = 'X'
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
wrong_parameter = 3
disk_full = 4
access_denied = 5
file_not_found = 6
destination_exists = 7
unknown_error = 8
path_not_found = 9
disk_write_protect = 10
drive_not_ready = 11
OTHERS = 12
.
IF sy-subrc <> 0.
ENDIF.
delete the temp file
CALL METHOD cl_gui_frontend_services=>file_delete
EXPORTING
filename = 'C:\TEMP\EMPLOYEE.XML'
CHANGING
rc = lf_return_code
EXCEPTIONS
file_delete_failed = 1
cntl_error = 2
error_no_gui = 3
file_not_found = 4
access_denied = 5
unknown_error = 6
OTHERS = 7 .
IF sy-subrc <> 0.
ENDIF.
ENDFUNCTION.
Import Parameters for the above function module:
1) I_COSTCENTER like BAPIEMPLB-COSTCENTER default *.
2) I_FILENAME TYPE STRING
Can anyone explain the reason for that error and give the necessary corrections in the code. When I give some dummy value for the costcenter field, the xml file is created without any data in it. Can you correct this?
Thanks in advance,
Srinivas.
‎2006 Nov 02 10:44 AM
U have used datatypes like typ_s_ressource...
r they defined in TOP include? What is the exact declaration of the same?
‎2006 Nov 02 12:02 PM
hi,
the data types like typ_s_ressource are defined.
they are declared in the include LZTPGTOP.
The declaration of typ_s_ressource is:
types: begin of typ_s_ressource,
perno type hrsobid-sobid,
org_unit type objid,
position type hrsobid-sobid,
leadpos type c,
end of typ_s_ressource.
If u have any questions, you can ask me, but try to provide a solution.
Thanks,
Srinivas.