How to use the next 'initial' cost center in infotype 0027
I need some help in posting in infotype 0027 which should be fairly easy to most of you. As you guys know, there are a total of 25 cost centers available (KST01-KST25) for this infotype. What I want to do is check from KST01 if it exists with a valid entry at a specific date (BEGDA and ENDDA will be provided as well) all the way up to KST25 and choose the first cost center field that is INITIAL. This is used before 'BAPI_COSTCENTER_CREATEMULTIPLE' so that if KST01 up to KST05 all have values on the same BEGDA & ENDDA, how can I have KST06 chosen? Thanks in advance for your help!
Instead you can just declare a work area ..
data : p_0027 type pa0027.
data : lv_KOSTL type KOSTL.
data : lv_index(2) type n.
data : lv_cost(12).
field-symbols : <lv_kostl> type any.
select single * from pa0027 into p_0027.
do 25 times
varying lv_KOSTL from p_0027-KST01 next p_0027-KST02.
lv_index = lv_index + 1.
IF lv_KOSTL is initial.
concatenate 'KST' lv_index into lv_cost.
assign component lv_cost of structure p_0027 to <lv_kostl>.
IF sy-subrc = 0.
<lv_kostl> = '100'. " <-- Assign any value here