Getting Spool Request Number for the Output type

refer to:http://wiki.sdn.sap.com/wiki/display/ABAP/Getting+Spool+Request+Number+for+the+Output+type

I came across this query several times to how to find the spool request number when you know the Output Type. Untill when I require the same I didn't try to think deep on it.So, I thought of putting the solution here for others also.

Whenever a print is issued, there is a corresponding entry in the table NAST along with that there is a entry in the table CMFP also. This entry is stored as an error message management og type I.

The below mention class fetch the value of spool and convert it to PDF and download.

Class Technical Name: ZCL_CREATE_PDF_SPOOL

Class Description: Convert Spool Request to PDF and Download

Class Long Description:

This class will fetch the spool request number for the given output type and download it in PDF format.

Condition Handle -

This Class can be called either by passing the Spool Number (I_SPOOL) or by passing Application Type (I_KAPPL), Object Key (I_OBJKY), Condition Type (I_KSCHL) and Run Date (I_DATE).

If all the values are passed to the class SPOOL NUMBER has the preference over the others.

Class Instantiation:  Public

Super Class: ZCL_CREATE_PDF_SPOOL

Class Attributes:

W_ERROR    Instance          Public Type   FLAG             General Flag                                                                                                 

W_SPOOL     Instance          Public Type   RSPOID         Spool request number                                                                                                                                   

C_WFMC       Constant        Public Type   CM_APLID   Error Management

C_I                  Constant        Public Type   MSGTY          Message number    

C_VN             Constant        Public Type   ARBGB          Application Area   

C_342             Constant        Public Type   MSGNR         Message number     

Class Methods: The following methods will be defined in the class:

Method Technical Name: GET_SPOOL_NUMBER

Method Description: Get Condition record to fetch the spool. This method will be called inside the method GET_PDF_FROM_SPOOL.

Declaration Level/Vision:  Instance/Private

Importing Parameters:  

I_KAPPL       Type   KAPPL                 

Application Type. This will determine the Output Type.
I_OBJKY        Type   OBJKY

Object Key. This will determine the unique key to extract records from NAST table along with KAPPL, KSCHL & DATE.
I_KSCHL       Type   KSCHL

Condition Type. This will determine the condition type for the given output type.
I_RUNDAT   Type   DATS

Creation Date. This will give the date of creation of entry in NAST table.
Exporting Parameters:

E_ERROR      Type   FLAG

Return Message. Return E for Error and S for Success.
E_SPOOL      Type   RSPOID

Spool Number. Return Spool Number.
Exceptions: None

________________________________________________________________

Method Technical Name: GET_PDF_FROM_SPOOL

Method Description: Get the PDF from the Spool number

Declaration Level/Vision:  Instance/Public

Importing Parameters: 

I_KAPPL       Type   KAPPL           OPTIONAL

Application Type. This will determine the Output Type.
I_OBJKY        Type   OBJKY           OPTIONAL

Object Key. This will determine the unique key to extract records from NAST table along with KAPPL, KSCHL & DATE.
I_KSCHL       Type   KSCHL           OPTIONAL

Condition Type. This will determine the condition type for the given output type.
I_SPOOL       Type   RSPOID         OPTIONAL

Spool Number. Spool Number to convert and download the PDF file.
I_DATE          Type   DATS              OPTIONAL

Creation Date. This will give the date of creation of entry in NAST table.
I_PATH          Type   STRING

Application Server Path. Path to download the file.
Exporting Parameters:

E_RETURN   Type   FLAG

Return Message. Return E for Error and S for Success.
Exceptions: None

Code for Method GET_SPOOL_NUMBER
METHOD get_spool_number.

**--Types Declaration
  TYPES: BEGIN OF type_nast,
         objky  TYPE na_objkey,
         erdat  TYPE dats,
         eruhr  TYPE tims,
         datvr  TYPE dats,
         uhrvr  TYPE tims,
         cmfpnr TYPE na_cmf_nr,
         END OF type_nast.

**--Table Type Declaration
  TYPES: tt_nast TYPE STANDARD TABLE OF type_nast.

**--Internal Table Declaration
  DATA: tl_nast TYPE tt_nast.

**--Field Symbols Declaration
  FIELD-SYMBOLS:<l_nast> TYPE type_nast.

**--Data Declaration
  DATA: wl_cmfpnr TYPE na_cmf_nr, "Error Management Number
        wl_temp   TYPE symsgv.

**--Check if the Importing Parameters are initial

  IF ( i_kappl IS INITIAL AND
       i_objky IS INITIAL AND
       i_kschl IS INITIAL ).

**--Retrun Error Message
    e_error = c_e.        "Return Error Message
    EXIT.

  ENDIF.

**--Extract Error Management Number from NAST table for the combination of
**--Aplication Type, Output Type & Object Key

  SELECT objky           "Object Key
         erdat           "Date of creation
         eruhr           "time of Creation
         datvr           "Date of Process
         uhrvr           "Time of Process
         cmfpnr          "Error Management Number
         FROM nast
         INTO TABLE tl_nast
         WHERE kappl = i_kappl
         AND   objky = i_objky
         AND   kschl = i_kschl
         AND   ( erdat = i_rundat
         OR      datvr = i_rundat ).

  IF sy-subrc = 0.

    SORT tl_nast BY datvr uhrvr DESCENDING.

    READ TABLE tl_nast ASSIGNING <l_nast> INDEX 1.

    IF sy-subrc = 0.
      wl_cmfpnr = <l_nast>-cmfpnr.
    ENDIF.

**--Select Message Text 1 from CMFP table based on the error management number
    SELECT SINGLE msgv1  "Message Text
           FROM cmfp
           INTO wl_temp
           WHERE aplid = c_wfmc
           AND   nr    = wl_cmfpnr
           AND   arbgb = c_vn
           AND   msgty = c_i
           AND   msgnr = c_342.

**--If the corresponding data found
    IF sy-subrc = 0.
**--Pass Spool Request Number to Corresponding export Parameter
      e_spool = wl_temp.
**--Return success message
      e_error = c_s.
    ELSE.
**--If no data found return error message
      e_error = c_e.
    ENDIF.
  ENDIF.

ENDMETHOD.

Code for Method GET_PDF_FROM_SPOOL
METHOD get_pdf_from_spool.

**--Data Declaration
  DATA: wl_numbytes   TYPE i,                            "Bytecount
        wl_pdfspoolid TYPE tsp01-rqident,                "Spool Id
        wl_jobname    TYPE tbtcjob-jobname,              "Job Name
        wl_jobcount   TYPE tbtcjob-jobcount,             "Job Count
        wl_fullpath   TYPE string.                         "File Path

**--Constants Declaration
  CONSTANTS: c_path TYPE rlgrap-filename VALUE 'C:\temp\file.pdf'.

**--Internal Table & wORK aREA dECLARATION
  DATA: tl_pdf  TYPE STANDARD TABLE OF tline,
        wal_pdf TYPE tline.

**--Clear Variable
  CLEAR w_spool.

**--Check if the Spool Number (Importing) is initial
  IF i_spool IS INITIAL.

**--Check if the Spool is initial then APPlication Type
**--Output Type, Object Key & Date are not initial
    IF ( NOT i_kappl IS INITIAL AND
         NOT i_objky IS INITIAL AND
         NOT i_kschl IS INITIAL AND
         NOT i_date  IS INITIAL ).

**--Call method GET_SPOOL_NUMBER of the same class to fetch spool number
      CALL METHOD me->get_spool_number
        EXPORTING
          i_kappl  = i_kappl
          i_objky  = i_objky
          i_kschl  = i_kschl
          i_rundat = i_date
        IMPORTING
          e_error  = w_error
          e_spool  = w_spool.

**--Check Return Message
      IF w_error = c_e.
**--Return Error Message
        e_return = w_error.
        EXIT.
      ENDIF.

    ENDIF.

  ELSE.
**--If successful pass the apool number to local variable
    w_spool = i_spool.

  ENDIF.

**--Check Spool Number is inital
  IF NOT w_spool IS INITIAL.

**--Call FM CONVERT_OTFSPOOLJOB_2_PDF to convert the spool to PDF

    CALL FUNCTION 'CONVERT_OTFSPOOLJOB_2_PDF'
      EXPORTING
        src_spoolid              = w_spool
        no_dialog                = ' '
      IMPORTING
        pdf_bytecount            = wl_numbytes
        pdf_spoolid              = wl_pdfspoolid
        btc_jobname              = wl_jobname
        btc_jobcount             = wl_jobcount
      TABLES
        pdf                      = tl_pdf
      EXCEPTIONS
        err_no_otf_spooljob      = 1
        err_no_spooljob          = 2
        err_no_permission        = 3
        err_conv_not_possible    = 4
        err_bad_dstdevice        = 5
        user_cancelled           = 6
        err_spoolerror           = 7
        err_temseerror           = 8
        err_btcjob_open_failed   = 9
        err_btcjob_submit_failed = 10
        err_btcjob_close_failed  = 11.

**--Check if the PDF Return table is initial
    IF tl_pdf IS NOT INITIAL.
**--Pass Download file path
      wl_fullpath = c_path.
*--Check if the call is in Foreground or Background
      IF sy-batch NE 'X'.

**--Call FM GUI_DOWNLOAD to download the file in Foreground/Presentation Server

        CALL METHOD cl_gui_frontend_services=>gui_download
          EXPORTING
            bin_filesize = wl_numbytes
            filename     = wl_fullpath
            filetype     = 'BIN'
          IMPORTING
            filelength   = wl_numbytes
          CHANGING
            data_tab     = tl_pdf.

**--Return Success message if File downloaded to given path
        IF sy-subrc = 0.
          e_return = c_s.
        ENDIF.

      ELSE.
**--Call OPEN Dataset to download in Background/Aplication Server
        OPEN DATASET wl_fullpath FOR OUTPUT IN BINARY MODE.

        IF sy-subrc = 0.
          CLEAR: wal_pdf.
**--Trnasfer contents to detination file
          LOOP AT tl_pdf INTO wal_pdf.
            TRANSFER wal_pdf TO wl_fullpath.
            CLEAR: wal_pdf.
          ENDLOOP.
**--Close the file
          CLOSE DATASET wl_fullpath.
**--Return Success mesage
          e_return = c_s.
        ELSE.
**--Return Error Message
          e_return = c_e.
        ENDIF.
      ENDIF.
    ENDIF.
  ELSE.
**--Return Error Message
    e_return = c_e.
  ENDIF.

ENDMETHOD.

你可能感兴趣的:(C++,c,C#)