REFRESH IT_MSEG.
CLEAR IT_MSEG.
SELECT * INTO CORRESPONDING FIELDS OF TABLE IT_MSEG
FROM MSEG WHERE MBLNR = TITAB-MBLNR
AND MJAHR = TITAB-MJAHR
AND ZEILE = TITAB-ZEILE.
LOOP AT IT_MSEG.
IF IT_MSEG-SGTXT = ''.
IT_MSEG-SGTXT = '1'.
ELSE.
IT_MSEG-SGTXT = IT_MSEG-SGTXT + 1.
ENDIF.
MODIFY IT_MSEG.
CLEAR IT_MSEG.
ENDLOOP.
CALL FUNCTION 'MB_CHANGE_DOCUMENT'
TABLES
ZMKPF = IT_MKPF
ZMSEG = IT_MSEG.
IF SY-SUBRC = 0.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
WAIT = 'X'.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
ENDIF.
CLEAR IT_MSEG.
SELECT * INTO CORRESPONDING FIELDS OF TABLE IT_MSEG
FROM MSEG WHERE MBLNR = TITAB-MBLNR
AND MJAHR = TITAB-MJAHR
AND ZEILE = TITAB-ZEILE.
LOOP AT IT_MSEG.
IF IT_MSEG-SGTXT = ''.
IT_MSEG-SGTXT = '1'.
ELSE.
IT_MSEG-SGTXT = IT_MSEG-SGTXT + 1.
ENDIF.
MODIFY IT_MSEG.
CLEAR IT_MSEG.
ENDLOOP.
CALL FUNCTION 'MB_CHANGE_DOCUMENT'
TABLES
ZMKPF = IT_MKPF
ZMSEG = IT_MSEG.
IF SY-SUBRC = 0.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
WAIT = 'X'.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
ENDIF.