I'm working on function module and I'm in trouble with my loop statement. My problem is, when nothing is found in this loop (this case needs to be handled) the program ends directly to endfunction and my else condition on bottom is not triggered.
Can someone explain me how to run my code even if no result is found in my loop? I'm hard stuck.
Thanks :)
LOOP AT it_p0015 INTO ls_p0015 WHERE
subty = iv_sty AND
betrg = iv_amount AND
endda = iv_end_date AND
begda = iv_start_date.
CALL FUNCTION 'ENQUEUE_EPPRELE'
EXPORTING
pernr = iv_employeepernr "is_request-employeepernr
infty = '0015'
subty = iv_sty
EXCEPTIONS
foreign_lock = 1
system_failure = 2
OTHERS = 3.
IF sy-subrc <> 0.
RETURN.
ENDIF.
CALL FUNCTION 'HR_INFOTYPE_OPERATION' " Delete IT0015
EXPORTING
infty = ls_p0015-infty
number = ls_p0015-pernr
subtype = ls_p0015-subty
validityend = iv_end_date
validitybegin = iv_start_date
record = ls_p0015
operation = 'DEL'
IMPORTING
return = ls_return.
IF ls_return IS NOT INITIAL.
APPEND ls_return TO et_message.
ELSE.
ls_return-type = 'S'.
ls_return-message = 'Infotype 0015 advance payment deduction deleted.'(010).
APPEND ls_return TO et_message.
ENDIF.
CALL FUNCTION 'DEQUEUE_EPPRELE'
EXPORTING
pernr = iv_employeepernr
infty = '0015'
subty = iv_sty.
ELSE.
ls_return-type = 'E'.
ls_return-message = 'No records found in infotype 0015.'(036).
APPEND ls_return TO et_message.
ENDIF.
ENDLOOP.
ENDIF.
ENDFUNCTION
First, you could always see if sy-tabix has a value set, that's an indicator of the current line in the loop. Personally, if you're struggling I would just set a variable in the loop that's set so you know if it ran or not.
i.e.