Problem Description:
Business users often want to look at old output and if you do the normal print preview it actually shows you the output with the latest data and not necessarily the data that was sent initially. E.g. A quote confirmation was sent and then the quote is changed and the pricing changed and now the user wants to see what he quoted the customer and wants to look at the old printout. Here's how you can do it programmatically.
Program Description:
The following is a very quick and dirty (yet it works) program that takes an object key (e.g. Sales Order number), output type (e.g. BA00) as input. It then searches for historical records in NAST and if it finds some goes and looks in SOST and SOOD to see if the output was generated. If it has been then it selects one to display it.
This program can be easily modified to provide an ALV grid of the applicable output that you could then select to display. This program just displays the output for the first entry found.
*&---------------------------------------------------------------------*
*& Report ZCA_SOST
*&---------------------------------------------------------------------*
REPORT zca_sost.
TABLES: sost,
sood,
nast.
DATA: it_sost TYPE STANDARD TABLE OF sost,
wa_sost TYPE sost,
it_sood TYPE STANDARD TABLE OF sood,
wa_sood TYPE sood,
it_data TYPE soxsp2tab,
wa_data TYPE LINE OF soxsp2tab,
it_nast TYPE STANDARD TABLE OF nast,
wa_nast TYPE nast,
formname(40),
repid(40),
g_marked_counter TYPE i,
lx_bcs TYPE REF TO cx_bcs.
SELECT-OPTIONS: so_snd FOR sost-sndart DEFAULT 'INT' OBLIGATORY,
so_kappl FOR nast-kappl DEFAULT 'V1' OBLIGATORY,
so_kschl FOR nast-kschl OBLIGATORY.
PARAMETERS: p_objky TYPE nast-objky OBLIGATORY.
* SOST contains the history of communication records output
* SOOD contains OBJDES which usually has the ORDER NUMBER in it
* NAST contains the output records
SELECT * FROM nast
INTO TABLE it_nast
WHERE kappl IN so_kappl AND
objky EQ p_objky AND
kschl IN so_kschl.
IF sy-subrc = 0.
READ TABLE it_nast INTO wa_nast INDEX 1.
SELECT * FROM sost
INTO TABLE it_sost
WHERE sndart IN so_snd AND
entry_date EQ wa_nast-datvr AND
entry_time EQ wa_nast-uhrvr.
IF sy-subrc = 0.
SORT it_sost BY objtp objno objyr.
SELECT * FROM sood
INTO TABLE it_sood
FOR ALL ENTRIES IN it_sost
WHERE objtp EQ it_sost-objtp AND
objno EQ it_sost-objno AND
objyr EQ it_sost-objyr.
IF sy-subrc = 0.
LOOP AT it_sood INTO wa_sood.
FIND FIRST OCCURRENCE OF p_objky IN wa_sood-objdes.
IF sy-subrc <> 0.
DELETE it_sost
WHERE objtp EQ wa_sood-objtp AND
objno EQ wa_sood-objno AND
objyr EQ wa_sood-objyr.
ELSE.
EXIT.
ENDIF.
ENDLOOP.
READ TABLE it_sost INTO wa_sost INDEX 1. "Just pull the first one
IF sy-subrc = 0.
MOVE-CORRESPONDING wa_sost TO wa_data.
wa_data-mark = 'X'.
APPEND wa_data TO it_data.
TRY.
cl_sndrec_bcs=>display( it_data ).
CATCH cx_bcs INTO lx_bcs.
MESSAGE ID lx_bcs->msgid TYPE 'S' NUMBER lx_bcs->msgno
WITH 'Display document' space space space.
ENDTRY.
ENDIF.
ELSE.
MESSAGE i999(b1) WITH 'No entries found in SOOD'.
ENDIF.
ELSE.
MESSAGE i999(b1) WITH 'No entries found in SOST'.
ENDIF.
ELSE.
MESSAGE i999(b1) WITH 'No entries found in NAST'.
ENDIF.








