Application Sever AL11 File Programs - 1





Scenario:
We have an Application Server File in AL11 Directory and now we need to split that file into two based on some lookup or logic.
Things to know:
1.       Open Data set and Read Dataset Concepts
2.       Input Section F4 help for selecting AL11 Directory file
3.       CL_RSAN_UT_APPSERV_FILE_WRITER & CL_RSAN_UT_APPSERV_FILE_READER Files
4.       Splitting the string

What is Open Data Set and Read Data set?
If we want to do any Application server file operations like read, write or modify first we need to make a link to that file by opening the file for further operations. So in this aspect ABAP provides us the statement
OPEN DATASET FNAME
Where FNAME is the file name along with its path. If the system is able to open the file sy-subrc is zero else its 8. The Additions if we want to open the file to read is
OPEN DATASET FNAME FOR INPUT IN TEXT MODE ENCODING DEFAULT.
In Text Mode specifies to open the file in text mode and encoding specifies what code pages/ what character representation to be used when opening.
Since our system is Unicode the encoding default opens in Unicode mode.
Once we open the dataset we need to read the file for that we use
READ DATASET FNAME INTO WA.
NOTE: WA IS OF TYPE STRING.
Entire row of the file is stored as string.
Now we have a requirement to check the Transaction Number (CRM_OBJ_ID ) is present in the info object Service Request to Activity Mapping BCSSA040. All the Transaction Numbers present in this object will be considered as closed and those need to be split into separate file.
So the code would look like this.
OPEN DATASET FNAME FOR INPUT IN TEXT MODE ENCOINDG DEFAULT.
IF SY-SUBRC IS INITIAL.
DO.
READ DATASET FNAME INTO WA.
IF SY-SUBRC IS NOT INITIAL.
EXIT.
ENDIF.
(OUR LOGIC ).
Logic: split the file into their respective work area fields ( WA fields).
Read the lookup table for existence of transaction no if yes move that to one file else move it to second file.
How to Split the String?
SPLIT WA AT into
Respective fields (f1, f2 f3 f4…fn ).
- is the field separator in your file.
Now we read the itab with transaction no if there is a hit or not if yes
Move it to itab1 else move it to itab2 ( itab1 and itab2 are of type string tables ).
If the file size is huge do not buffer all records into itab and then move the program dumps due to memory issue.
Instead check the count of itab’s and if crosses say one lakh move it to desired path and desired file name using the class
CL_RSAN_UT_APPSERV_FILE_WRITER-> APPSERVER_FILE_WRITE
By this way we can achieve our requirement of splitting the files.
INPUT Help for Application Server File:
PARAMETERS P_SOURCE TYPE dxfile-filename.
We use parameters to provide option
Inorder to populate the fields from f1 help we use
AT SELECTION-SCREEN ON VALUE-REQUEST FOR
The two events ON HELP-REQUEST and ON VALUE-REQUEST are triggered by the screen events POH and POV of a selection screen if, for the input field of a parameter para or one of the input fields of a selection criterion selcrit, the field help F1 or the input help F4 was called
(POH eq Process on Help,  PHV eq process on Value )
Then use FM as below to read the values from Application Server.
                 CALL FUNCTION '/SAPDMC/LSM_F4_SERVER_FILE'
*                  EXPORTING
*                    DIRECTORY              = ' '
*                    FILEMASK               = ' '
                  
IMPORTING
                    SERVERFILE             = P_SOURCE
                  
EXCEPTIONS
                    CANCELED_BY_USER       = 
1
                    
OTHERS                 2
                           .
                 
IF sy-subrc <> 0.
* Implement suitable error handling here
                   else.
                 ENDIF.
Actual Program:


*
*======================================================================*
*
* OVERVIEW
*
* This report is used to read the file ZISPK_INS.csv form
* (Directory: /xifiledata/BW_Files) and splits in to two
* based on the input date and the user status

REPORT  zispk_insgt_acti_splt_new MESSAGE-ID zinsactsplt." NEW MESSAGE CLASS ZINSACTSPLT CREATED FOR THIS

DATA: GV_WTAB1 
TYPE string,
      GT_it1 
TYPE STANDARD TABLE OF string,
      GT_it2 
TYPE STANDARD TABLE OF string,
      GV_zodays(10) 
TYPE c,
      GV_zh0247(10) 
TYPE c,
      GW_WA_SP      
TYPE zbw_zispk_insgt_acti,
      Gv_it1     
TYPE i,
      Gv_it2     
TYPE i.

DATA: GC_fname(1000),
"  VALUE '/xifiledata/BW_Files/ZISPK_INS.CSV',
      GC_fname1(1000) 
VALUE '/xifiledata/BW_Files/ZISPK_INS_CLOSED.CSV',
      GC_fname2(1000) 
VALUE '/xifiledata/BW_Files/ZISPK_INS1.CSV'.

DATA: GS_ZCRM_OBJ_ID 
TYPE /BI0/OICRM_OBJ_ID.

SELECT-OPTIONS: S_ACTNO 
FOR GS_ZCRM_OBJ_ID NO INTERVALS.
PARAMETERS P_SOURCE TYPE dxfile-filename.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_SOURCE.

                 
CALL FUNCTION '/SAPDMC/LSM_F4_SERVER_FILE'
*                  EXPORTING
*                    DIRECTORY              = ' '
*                    FILEMASK               = ' '
                  
IMPORTING
                    SERVERFILE             = P_SOURCE
                  
EXCEPTIONS
                    CANCELED_BY_USER       = 
1
                    
OTHERS                 2
                           .
                 
IF sy-subrc <> 0.
* Implement suitable error handling here
                   else.
                 ENDIF.
START-OF-SELECTION.
TYPES: 
BEGIN OF TY_ACTNO,
  /BIC/BCSSA041 
TYPE /BIC/OIBCSSA041,
  
END OF TY_ACTNO.

*RANGES: GR_CRM_OBJ_ID FOR ZBW_ZISPK_INSGT_ACTI-CRM_OBJ_I D.
CHECK P_SOURCE IS NOT INITIAL.
DATA: GT_ITAB_ACTNO 
TYPE STANDARD TABLE OF TY_ACTNO,
      GW_WA_ACTNO 
TYPE TY_ACTNO.

DATA: GW_WA_TAB 
TYPE /BIC/PBCSSA043,
      GT_ITAB_CLOSED 
TYPE STANDARD TABLE OF /BIC/PBCSSA048,
      GT_ITAB_OPEN 
TYPE STANDARD TABLE OF /BIC/PBCSSA043.

SELECT /BIC/BCSSA041 FROM /BIC/PBCSSA040
  
INTO TABLE GT_ITAB_ACTNO WHERE /BIC/BCSSA040 NE ' ' AND OBJVERS = 'A'.
  
IF SY-SUBRC IS INITIAL.
    
"NOTHING TO MENTION
    ELSE.
  ENDIF.

DELETE ADJACENT DUPLICATES FROM S_ACTNO COMPARING LOW.

LOOP AT S_ACTNO.
  GW_WA_ACTNO-/BIC/BCSSA041 = S_ACTNO-LOW.
  
APPEND GW_WA_ACTNO TO GT_ITAB_ACTNO.
ENDLOOP.

DELETE FROM /BIC/PBCSSA048 WHERE OBJVERS = 'A'.
IF SY-SUBRC IS INITIAL.
"NOTHING TO MENTION
ELSE.
ENDIF.
DELETE FROM /BIC/PBCSSA043 WHERE OBJVERS = 'A'.
IF SY-SUBRC IS INITIAL.
"NOTHING TO MENTION
ELSE.
ENDIF.

DELETE ADJACENT DUPLICATES FROM GT_ITAB_ACTNO COMPARING /BIC/BCSSA041.
SORT GT_ITAB_ACTNO.

GC_fname = P_SOURCE.

DELETE DATASET GC_fname1." DELETE THE CLOSED STATUS ONE BEFORE PLACING ELSE THEY KEEP ADDING UP
* TO READ THE DATA FROM THE INFOSPOKE FILE ZISPK_INS.CSV
OPEN DATASET GC_fname FOR INPUT IN TEXT MODE ENCODING DEFAULT.
IF sy-subrc IS INITIAL.
  
READ DATASET GC_fname INTO GV_wtab1.
  
WHILE sy-subrc EQ 0.
    
"aim is to split the record and check for its conditions and assign to diff tables

    
SPLIT GV_wtab1 AT 'Ð' INTO " split the string into coresponding fields for structure ZBW_ZISPK_INSGT_ACTI
    GW_WA_SP-crm_stsma
    GW_WA_SP-crm_obj_id
    GW_WA_SP-crm_usstat
    GW_WA_SP-crm_cat
    GW_WA_SP-bp_activit
    GW_WA_SP-bpartner2
    GW_WA_SP-bp_respper
    GW_WA_SP-/bic/zpremply
    GW_WA_SP-/bic/zch_deptt
    GW_WA_SP-crm_prctyp
    GW_WA_SP-crm_pbwdat
    GW_WA_SP-/bic/zchst_dat
    GW_WA_SP-/bic/zchestdt
    GW_WA_SP-crm_rsn_co
    GW_WA_SP-crm_rsn_gr
    GW_WA_SP-crm_rsn_ty
    GW_WA_SP-crm_itcrby
    GW_WA_SP-/bic/zchsrce
    GW_WA_SP-/bic/zchrsdtm
    GW_WA_SP-/bic/zchrspdt
    GW_WA_SP-/bic/zchst_tim
    GW_WA_SP-0bp_activit__zchcusseg
    GW_WA_SP-/bic/zch_rpt
    GW_WA_SP-/bic/zch_cmplt
    GW_WA_SP-bp_contper
    GW_WA_SP-/bic/zchrrc_dt
    GW_WA_SP-/bic/zchactsc
    GW_WA_SP-/bic/zchfucmn
    GW_WA_SP-/bic/zchsumry
    GW_WA_SP-/bic/zchactn
    GW_WA_SP-/bic/zchrslve
    GW_WA_SP-/bic/zchad40dt
    GW_WA_SP-/bic/zchdd40fl
    GW_WA_SP-/bic/zchfpldt
    GW_WA_SP-/bic/zchpd40dt
    GW_WA_SP-/bic/zzpred
    GW_WA_SP-/bic/zzsucc
    GW_WA_SP-/bic/zh0227
    GW_WA_SP-/bic/zh0228
    GW_WA_SP-/bic/zh0229
    GW_WA_SP-/bic/zh0230
    GW_WA_SP-/bic/zh0231
    GW_WA_SP-/bic/zh0232
    GW_WA_SP-/bic/zh0224
    GW_WA_SP-/bic/zh0225
    GW_WA_SP-/bic/zh0226
    GW_WA_SP-/bic/zh0233
    GW_WA_SP-addr_line0
    GW_WA_SP-addr_line1
    GW_WA_SP-addr_line2
    GW_WA_SP-addr_line3
    GW_WA_SP-addr_line4
    GW_WA_SP-addr_line5
    GW_WA_SP-addr_line6
    GW_WA_SP-addr_line7
    GW_WA_SP-addr_line8
    GW_WA_SP-addr_line9
    GW_WA_SP-addr_short
    GW_WA_SP-addr_sh_s
    GW_WA_SP-building20
    GW_WA_SP-cityp_code
    GW_WA_SP-city_1
    GW_WA_SP-city_2
    GW_WA_SP-city_code
    GW_WA_SP-country
    GW_WA_SP-/bic/zh0218
    GW_WA_SP-/bic/zh0219
    GW_WA_SP-/bic/zh0234
    GW_WA_SP-/bic/zh0235
    GW_WA_SP-/bic/zh0236
    GV_zodays
" supports only type c
    GW_WA_SP-/bic/zktext
    GW_WA_SP-/bic/zh0239
    GW_WA_SP-/bic/zcomm
    GW_WA_SP-/bic/zh0240
    GW_WA_SP-/bic/zh0241
    GW_WA_SP-/bic/zroottxt
    GW_WA_SP-/bic/zh0242
    GW_WA_SP-/bic/zh0243
    GW_WA_SP-/bic/zh0244
    GW_WA_SP-/bic/zh0245
    GW_WA_SP-/bic/zh0246
    GV_zh0247
" supports only type c
    GW_WA_SP-/bic/zfstactdt
    GW_WA_SP-/bic/zomb_sett
    GW_WA_SP-TEL_NUMDIS
    GW_WA_SP-FAX_NUMDIS
    GW_WA_SP-EMAIL_ADDR
    GW_WA_SP-/BIC/ZCOMMADD
    GW_WA_SP-/BIC/ZSECNUM.

    GW_WA_SP-/bic/zh0247 = GV_zh0247.
" supports only type c
    GW_WA_SP-/bic/zodays = GV_zodays.
" supports only type c


* CHECK THE DATA FROM THE TABLE /BIC/PBCSSA040, IF ITS EXISTING THEN THIS GOES TO CLOSED FILE
* ELSE THIS GOES TO OPEN FILE (ORIGINAL FILE )

  
READ TABLE GT_ITAB_ACTNO WITH KEY
   /BIC/BCSSA041 = GW_WA_SP-CRM_OBJ_ID 
BINARY SEARCH
   
TRANSPORTING NO FIELDS.

    
IF SY-SUBRC IS INITIAL.

      
MOVE-CORRESPONDING GW_WA_SP TO GW_WA_TAB.
      GW_WA_TAB-OBJVERS = 
'A'.
      GW_WA_TAB-/BIC/BCSSA042 = GW_WA_SP-CRM_STSMA.
      GW_WA_TAB-/BIC/BCSSA043 = GW_WA_SP-CRM_OBJ_ID.
      GW_WA_TAB-/BIC/BCSSA045 = GW_WA_SP-/BIC/ZH0244.
      GW_WA_TAB-/BIC/BCSSA046 = GW_WA_SP-/BIC/ZH0245.
      GW_WA_TAB-/BIC/BCSSA047 = GW_WA_SP-/BIC/ZH0246.

      
APPEND GW_WA_TAB TO GT_ITAB_CLOSED.

      
CLEAR GW_WA_TAB.

      
APPEND GV_wtab1 TO GT_it2." if satisfies move to it2 in path /xifiledata/BW_Files/ZISPK_INS_CLOSED.CSV
      
CLEAR GV_wtab1.
      
FREE GV_wtab1.
      
CLEAR GV_it2.
      Gv_it2 = lines( GT_it2 ).
      
IF Gv_it2 > 10000." IF THE LINES CROSSES 10K TAKE INTO A FILE
        
PERFORM F0002."CALL SUB ROUTINE TO PLACE IT INTO THE DIR /xifiledata/BW_Files/ZISPK_INS_CLOSED.CSV
        
MODIFY /BIC/PBCSSA048 FROM TABLE GT_ITAB_CLOSED.
        
IF SY-SUBRC IS INITIAL.
          ELSE.
        ENDIF.
        
CLEAR GT_it2[].
        
FREE GT_IT2[].
        
CLEAR GT_ITAB_CLOSED[].
        
FREE GT_ITAB_CLOSED[].
      ENDIF.

    ELSE.

      
MOVE-CORRESPONDING GW_WA_SP TO GW_WA_TAB.
      GW_WA_TAB-OBJVERS = 
'A'.
      GW_WA_TAB-/BIC/BCSSA042 = GW_WA_SP-CRM_STSMA.
      GW_WA_TAB-/BIC/BCSSA043 = GW_WA_SP-CRM_OBJ_ID.
      GW_WA_TAB-/BIC/BCSSA045 = GW_WA_SP-/BIC/ZH0244.
      GW_WA_TAB-/BIC/BCSSA046 = GW_WA_SP-/BIC/ZH0245.
      GW_WA_TAB-/BIC/BCSSA047 = GW_WA_SP-/BIC/ZH0246.

      
APPEND GW_WA_TAB TO GT_ITAB_OPEN.

      
CLEAR GW_WA_TAB.

      
APPEND GV_wtab1 TO GT_it1."else to it1 in path /xifiledata/BW_Files/ZISPK_INS.CSV
      
CLEAR GV_wtab1.
      
FREE GV_wtab1.
      
CLEAR GV_it1.
      Gv_it1 = lines( GT_it1 ).
      
IF GV_it1 > 10000." IF THE LINES CROSSES 10K TAKE INTO A FILE
        
PERFORM F0001."CALL SUB ROUTINE TO PLACE IT INTO THE DIR /xifiledata/BW_Files/ZISPK_INS1.CSV ( TEMPORARY )
        
MODIFY /BIC/PBCSSA043 FROM TABLE GT_ITAB_OPEN.
        
IF SY-SUBRC IS INITIAL.
          ELSE.
        ENDIF.
        
CLEAR GT_it1[].
        
FREE GT_IT1[].
        
CLEAR GT_ITAB_OPEN[].
        
FREE GT_ITAB_OPEN[].
      ENDIF.
    ENDIF.

    
READ DATASET GC_fname INTO GV_wtab1.
  ENDWHILE.
ELSE.

  
MESSAGE e001 WITH GC_fname.

ENDIF.

CLOSE DATASET GC_fname.
CLEAR GV_wtab1.
CLEAR GW_WA_SP.

IF NOT GT_it2[] IS INITIAL." THERE COULD BE RECORDS WHICH MIGHT GET LEFT OVER

  
PERFORM F0002.
  
MODIFY /BIC/PBCSSA048 FROM TABLE GT_ITAB_CLOSED.
  
IF SY-SUBRC IS INITIAL.
    ELSE.
  ENDIF.

ENDIF.

IF NOT GT_it1[] IS INITIAL." THERE COULD BE RECORDS WHICH MIGHT GET LEFT OVER

  
PERFORM F0001.
  
MODIFY /BIC/PBCSSA043 FROM TABLE GT_ITAB_OPEN.
  
IF SY-SUBRC IS INITIAL.
    ELSE.
  ENDIF.

ENDIF.

message i004.


DELETE DATASET GC_fname." DELETE THE ORIGINAL FILE
CLEAR: GT_it1[],GT_IT2[], GV_wtab1.
FREE: GT_IT1[], GT_IT2[].
OPEN DATASET GC_fname2 FOR INPUT IN TEXT MODE ENCODING DEFAULT.
IF SY-SUBRC IS INITIAL.
DO.
  
READ DATASET GC_fname2 INTO GV_wtab1.
  
IF sy-subrc NE 0.
    EXIT.
  ENDIF.

  
APPEND GV_wtab1 TO GT_it1.
  
CLEAR Gv_it1.
  Gv_it1 = lines( GT_it1 ).
  
IF Gv_it1 > 10000." IN BLOCKS OF 10K
    
PERFORM F0003.
    
CLEAR GT_it1[].
  ENDIF.
  
CLEAR GV_wtab1.
ENDDO.
ELSE.
  
MESSAGE e001 WITH GC_fname2.
ENDIF.

CLOSE DATASET GC_fname2.

IF NOT GT_IT1[] IS INITIAL."THERE COULD BE RECORDS WHICH MIGHT GET LEFT OVER

  
PERFORM F0003.

ENDIF.

DELETE DATASET GC_fname2.

message I005.

FREE: GT_it1[], GT_it2[].
*&---------------------------------------------------------------------*
*&      Form  CREATE_FILE_IT1
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_IT1  text
*----------------------------------------------------------------------*
FORM F0001.

  DATA:
     lv_lines 
TYPE i.

  
CALL METHOD cl_rsan_ut_appserv_file_writer=>appserver_file_write" placing file in given path
    
EXPORTING
      i_filename      = 
'/xifiledata/BW_Files/ZISPK_INS1.CSV'
      i_data_tab      = GT_it1
  
IMPORTING
    e_lines_written = lv_lines
  
EXCEPTIONS
    open_failed     = 
1
    write_failed    = 
2
    close_failed    = 
3
    
OTHERS          4
          .
  
IF sy-subrc <> 0.
* Implement suitable error handling here
    
MESSAGE e003 WITH GC_fname2.

  ENDIF.
ENDFORM.                    
" CREATE_FILE_IT1
*&---------------------------------------------------------------------*
*&      Form  CREATE_FILE_IT2
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <-- font="" nbsp="" p2="" text="">
*----------------------------------------------------------------------*
FORM F0002 .

  DATA:
      lv_lines 
TYPE i.

  
CALL METHOD cl_rsan_ut_appserv_file_writer=>appserver_file_write" placing file in given path
    
EXPORTING
      i_filename      = 
'/xifiledata/BW_Files/ZISPK_INS_CLOSED.CSV'
      i_data_tab      = GT_it2
  
IMPORTING
    e_lines_written = lv_lines
  
EXCEPTIONS
    open_failed     = 
1
    write_failed    = 
2
    close_failed    = 
3
    
OTHERS          4
          .
  
IF sy-subrc <> 0.
* Implement suitable error handling here
    
MESSAGE e001 WITH GC_fname1.

  ENDIF.
ENDFORM.                    
" CREATE_FILE_IT2
*&---------------------------------------------------------------------*
*&      Form  CREATE_FILE_IT3
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <-- font="" nbsp="" p2="" text="">
*----------------------------------------------------------------------*
FORM F0003 .
  DATA:
      lv_lines 
TYPE i.

  
CALL METHOD cl_rsan_ut_appserv_file_writer=>appserver_file_write" placing file in given path
    
EXPORTING
      i_filename      = 
'/xifiledata/BW_Files/ZISPK_INS.CSV'
      i_data_tab      = GT_it1
  
IMPORTING
    e_lines_written = lv_lines
  
EXCEPTIONS
    open_failed     = 
1
    write_failed    = 
2
    close_failed    = 
3
    
OTHERS          4
          .
  
IF sy-subrc <> 0.
* Implement suitable error handling here
    
MESSAGE e003 WITH GC_fname.

  ENDIF.
ENDFORM.                    
" CREATE_FILE_IT3



Screen shots:

F1 on P_SOURCE give
 Drill through and select the file
Two files created one with ZISPK_INS ( where there is no hit of transaction numbers in look up tables ) and ZISPK_CLOSED when there is  hit.


No comments:

Post a Comment