Monday, May 16, 2016

xml tag checking using call transformation in ABAP (t-code: XSLT_TOOL)



Transforming data from xml tag to internal table with checking (whether the tag exits or not)

<tt:cond s-check="not-initial(ref('QUANTITY'))">
<Quantity tt:value-ref="QUANTITY"/>
</tt:cond>

<tt:cond s-check="initial(ref('QUANTITY'))">
<Quantity tt:value-ref="QUANTITY"/>
</tt:cond>


Transforming data from xml tag to internal table without checking-

 <Quantity tt:value-ref="QUANTITY"/>


Quantity  = xml tag
QUANTITY = internal table field

xml parsing T-code using Call Transformation


XML parsing using call transformation

CALL TRANSFORMATION t-code : XSLT_TOOL

  CALL TRANSFORMATION Z_TransformationName
          SOURCE XML xmlstr_src
          RESULT
          in_asn = it_table. (internal table)



go to link for details:  

http://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/2011fbe7-3285-2e10-b281-fec5ef0c225b?QuickLink=index&overridelayout=true&51552492473450

Thursday, April 21, 2016

what is abs, sign, floor, ceil, trunc and frac in abap






FunctionfuncReturn value
absAbsolute value of argument arg
signSign of argument arg: -1, if the value of arg is negative; 0, if the value of arg is 0; 1, if the value of arg is positive
ceilSmallest integer that is not less than the value of the argument arg is
floorLargest integer that is not greater than the value of the argument arg is
truncValue of the integer part of the argument arg
fracValue of the decimal places of the argument arg



DATA n TYPE p DECIMALS 2.
DATA M TYPE p DECIMALS 2 VALUE '-3.55'.
n = ABS( M ).  WRITE:  'ABS: ', n.
n = SIGN( M ). WRITE: / 'SIGN: ', n.
n = CEIL( M ). WRITE: / 'CEIL: ', n.
n = FLOOR( M ). WRITE: / 'FLOOR:', n.
n = TRUNC( M ). WRITE: / 'TRUNC:', n.
n = FRAC( M ). WRITE: / 'FRAC: ', n.

output: 

ABS:               3.55
SIGN:              1.00-
CEIL:              3.00-
FLOOR:          4.00-
TRUNC:          3.00-
FRAC:             0.55-



Sunday, April 3, 2016

Convert csv file and send to ftp server

DATA: IT_FTP TYPE STANDARD TABLE OF ZFTPSERVERLIST ,
      WA_FTP 
TYPE ZFTPSERVERLIST,
      L_SLEN 
TYPE I,
      LV_USER
(30) TYPE C,
      LV_PWD
(30) TYPE C,
      C_KEY 
TYPE I VALUE 26101957.

DATA: TO_FILE TYPE CHAR300.
DATA: MI_HANDLE TYPE I.
DATA: itab1  TYPE truxs_t_text_data.

PERFORM CONVERT_TO_CSV.
PERFORM SERVER_CONN.
PERFORM R3_FTP USING TO_FILE.

FORM convert_to_csv .

  
CALL FUNCTION 'SAP_CONVERT_TO_TEX_FORMAT'
  
EXPORTING
    i_field_seperator    
= ','
    I_LINE_HEADER        
= 'X'
  
TABLES
    i_tab_sap_data       
= ITAB[] " final file
    
"FIELDNAMES           = T_DESC  " for header file
  
CHANGING
    i_tab_converted_data 
= itab1[]
  
EXCEPTIONS
    conversion_failed    
= 1
    
OTHERS               = 2.
  
IF sy-subrc <> 0.
    
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  
ENDIF.

ENDFORM.

FORM r3_ftp  USING    p_to_file.
  
""""" SEND FILE TO SERVER""""""
  
CALL FUNCTION 'FTP_R3_TO_SERVER'
  
EXPORTING
    HANDLE         
= MI_HANDLE
    FNAME          
= TO_FILE          "file path of destination system
    CHARACTER_MODE 
= 'X'
  
TABLES
    
TEXT           = itab1
  
EXCEPTIONS
    TCPIP_ERROR    
= 1
    COMMAND_ERROR  
= 2
    DATA_ERROR     
= 3
    
OTHERS         = 4.

  
IF sy-subrc <> 0.
    
WRITE : / 'FILE NOT SEND'.
  
ELSE.
    
WRITE : / 'FILE SEND'.
  
ENDIF.

ENDFORM.

FORM server_conn .

  
SELECT * FROM ZFTPSERVERLIST CLIENT SPECIFIED
  
INTO CORRESPONDING FIELDS OF TABLE IT_FTP
  
WHERE MANDT = SY-MANDT
  
AND   ZCOUNTRY = 'BD'.

  
CLEAR WA_FTP.
  
READ TABLE IT_FTP INTO WA_FTP INDEX 1.

*----CONNECTING TO FTP SERVER.
  
PERFORM FTP_CONNECT USING WA_FTP-ZUSER WA_FTP-ZPASSWD WA_FTP-ZHOST.

*----CHECKING SUCCESSFUL FTP CONNECTION
  
CHECK SY-SUBRC = 0.
  
CLEAR: TO_FILE.
  
" FILE NAME CREATIONN
  
CONCATENATE WA_FTP-zapp 'test' '_' DATE '.csv' INTO TO_FILE.

ENDFORM.


FORM ftp_connect  USING   LV_USER LV_PWD LV_HOST.

  L_SLEN 
= STRLEN( LV_PWD ).

  
CALL FUNCTION 'HTTP_SCRAMBLE'
  
EXPORTING
    SOURCE      
= LV_PWD
    SOURCELEN   
= L_SLEN
    
KEY         = C_KEY
  
IMPORTING
    DESTINATION 
= LV_PWD.

  
CALL FUNCTION 'FTP_CONNECT'
  
EXPORTING
    USER            
= LV_USER "Your SAP-UNIX FTP user name (case sensitive)
    PASSWORD        
= LV_PWD  "Your SAP-UNIX server host name (case sensitive)
    HOST            
= LV_HOST
    RFC_DESTINATION 
= 'SAPFTPA'
  
IMPORTING
    HANDLE          
= MI_HANDLE
  
EXCEPTIONS
    NOT_CONNECTED   
= 1
    
OTHERS          = 2.

ENDFORM.

How to read data from schedule job

DATA: JOBNAME TYPE TBTCO-JOBNAME.
DATA: JOBCOUNT TYPE TBTCO-JOBCOUNT.
DATA: LISTIDENT TYPE TBTCPV-LISTIDENT.
DATA: JOBNO TYPE TSP01-RQIDENT.
DATA: DATE TYPE C LENGTH 12.

DATA: flag TYPE I.

DATA: BEGIN OF i_outtab OCCURS 0,
  str
(255) TYPE C,
END OF i_outtab.


DATA: BEGIN OF BUFFER OCCURS 100000,
  
TEXT(6000) TYPE C,
END OF BUFFER.

DATA: startline TYPE I, endline TYPE I.


" GETTING DATE WISE SPOOL NUMBER

SELECT SINGLE JOBCOUNT FROM TBTCO INTO JOBCOUNT WHERE JOBNAME = 'job_name' AND SDLSTRTDT = SY-DATUM.

SELECT SINGLE LISTIDENT FROM TBTCPV INTO LISTIDENT WHERE JOBCOUNT = JOBCOUNT AND JOBNAME = 'job_name' .

CONCATENATE SY-DATUM+6(2) SY-DATUM+4(2) SY-DATUM+0(4) INTO DATE SEPARATED BY '_'.
JOBNO 
= LISTIDENT.

IF JOBNO NE '0'.
  
" FETCHING SPOOL DATA TO BUFFER
  startline 
= 1.

  
DO.
    
REFRESH BUFFER.
    endline 
= startline + 99999.
    
CALL FUNCTION 'RSPO_RETURN_SPOOLJOB'
    
EXPORTING
      rqident              
= JOBNO
      first_line           
= startline
      last_line            
= endline
      desired_type         
= 'RAW'
  
    
TABLES
      
BUFFER               = BUFFER
    
EXCEPTIONS
      no_such_job          
= 1
      job_contains_no_data 
= 2
      selection_empty      
= 3
      no_permission        
= 4
      can_not_access       
= 5
      read_error           
= 6
      type_no_match        
= 7
      
OTHERS               = 8.
    
IF sy-subrc <> 0.
      
EXIT.
    
ENDIF.

    
LOOP AT BUFFER.
      i_outtab
-str = BUFFER-TEXT.
      
APPEND i_outtab.
      
CLEAR i_outtab.
    
ENDLOOP.
    startline 
= endline + 1.
  
ENDDO.

EXIT, STOP, CONTINUE, RETURN and LEAVE PROGRAM

EXIT: The EXIT command will stop the LOOP passings, exit from the SUBROUTINES..etc.

The behavior of EXIT keyword is depends on where you use it.

  • If you use EXIT keyword inside IF .. ENDIF., it will comes out of the program.
  • If you use EXIT inside LOOP .. ENDLOOP., it will come out of loop.
  • If you use EXIT inside FORM .. ENDFORM., it will comes out of form (subroutine)

Leave Program : LEAVE PROGRAM always leaves the current program - there is never any processing after LEAVE PROGRAM 

STOP

STOP is a statement which is used to stop processing an event block, ex: If I have two events START-OF-SELECTION and END-OF-SELECTION in my program, If I use STOP keyword in START-OF-SELECTION, the keyword will exits start-of-selection and goes to END-OF-SELECTION.

CONTINUE

CONTINUE is a statement, which is used to skip execution of a record inside loop.. endloop, do..endo, while..endwhile etc. 

RETURN

RETURN is a statement which is used to stop processing of current block immediately.

Wednesday, August 19, 2015

A demo program to create subscreen in your ABAP Program

report  zsub_screen.

TABLES: USR02,       "Logon data
        SSCRFIELDS.  "FIELDS ON SELECTION SCREENS
*---------------------------------------------------------------
* SUBSCREEN 1
*---------------------------------------------------------------
SELECTION-SCREEN BEGIN OF SCREEN 100 AS SUBSCREEN.
SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-010.
   SELECT-OPTIONS: USERNAME FOR USR02-BNAME.
SELECTION-SCREEN END OF BLOCK B1.
SELECTION-SCREEN END OF SCREEN 100.

*---------------------------------------------------------------
* SUBSCREEN 2
*---------------------------------------------------------------
SELECTION-SCREEN BEGIN OF SCREEN 200 AS SUBSCREEN.
SELECTION-SCREEN BEGIN OF BLOCK B2 WITH FRAME TITLE TEXT-020.
   SELECT-OPTIONS: LASTLOGI FOR USR02-TRDAT.
SELECTION-SCREEN END OF BLOCK B2.
SELECTION-SCREEN END OF SCREEN 200.

*---------------------------------------------------------------
* SUBSCREEN 3
*---------------------------------------------------------------
SELECTION-SCREEN BEGIN OF SCREEN 300 AS SUBSCREEN.
SELECTION-SCREEN BEGIN OF BLOCK B3 WITH FRAME TITLE TEXT-030.
   SELECT-OPTIONS: CLASSTYP FOR USR02-CLASS.
SELECTION-SCREEN END OF BLOCK B3.
SELECTION-SCREEN END OF SCREEN 300.

* STANDARD SELECTION SCREEN FOR SCROLLING LEFT AND RIGHT
SELECTION-SCREEN: FUNCTION KEY 1,
                  FUNCTION KEY 2.

SELECTION-SCREEN: BEGIN OF TABBED BLOCK SUB FOR 15 LINES,
                  END OF BLOCK SUB.

START-OF-SELECTION.
    SELECT * FROM USR02 WHERE BNAME IN USERNAME
                          AND ERDAT IN LASTLOGI
                          AND CLASS IN CLASSTYP.
       WRITE: / 'User ', USR02-BNAME,
                'Last Login Date ', USR02-TRDAT,
                'Last Login Time ', USR02-LTIME,
                'CLASS ', USR02-CLASS.
    ENDSELECT.
END-OF-SELECTION.

INITIALIZATION.
* SCREEN ICON LEFT AND RIGHT
  SSCRFIELDS-FUNCTXT_01 = '@0D@'.
  SSCRFIELDS-FUNCTXT_02 = '@0E@'.
  SUB-PROG = SY-REPID.
  SUB-DYNNR = 100.

AT SELECTION-SCREEN.
  CASE SY-DYNNR.

    WHEN 100.
      IF SSCRFIELDS-UCOMM = 'FC01'.
        SUB-DYNNR = 300.
      ELSEIF SSCRFIELDS-UCOMM = 'FC02'.
        SUB-DYNNR = 200.
      ENDIF.

    WHEN 200.
      IF SSCRFIELDS-UCOMM = 'FC01'.
        SUB-DYNNR = 100.
      ELSEIF SSCRFIELDS-UCOMM = 'FC02'.
        SUB-DYNNR = 300.
      ENDIF.

    WHEN 300.
      IF SSCRFIELDS-UCOMM = 'FC01'.
        SUB-DYNNR = 200.
      ELSEIF SSCRFIELDS-UCOMM = 'FC02'.
        SUB-DYNNR = 100.
      ENDIF.
  ENDCASE.

Wednesday, June 3, 2015

AUTO invoice create using BAPI with the reference of sales order



 data : i_bapivbrk like bapivbrk occurs 0 with header line.
  data i_bapiret1 like bapiret1 occurs 0 with header line.
  data : i_bapivbrksuccess like bapivbrksuccess occurs 0 with header line.


  i_bapivbrk-doc_number = zsalesdocument.
  i_bapivbrk-ref_doc = zsalesdocument.
  i_bapivbrk-ref_doc_ca = 'C'.
  append i_bapivbrk.


   call function 'BAPI_BILLINGDOC_CREATEMULTIPLE'
    tables
    billingdatain         = i_bapivbrk
    return                = i_bapiret1
    success               = i_bapivbrksuccess.

   invoice_no = i_bapivbrksuccess-bill_doc.

    call function 'BAPI_TRANSACTION_COMMIT'
    exporting
      wait          = 'X'.

Purcahse order create using BAPI with the reference of purchase requisition



  "---DATA DECALRATION FOR AUTO PO CREATION
tables: bapimepoheader,
        bapimepoheaderx
.

data: it_bapimepoitem type bapimepoitem occurs 0 with header line,
      it_bapimepoitemx 
type bapimepoitemx occurs 0 with header line,
      it_poaccount 
type bapimepoaccount occurs 0 with header line,
      it_poaccountx 
type bapimepoaccountx occurs 0 with header line.
data: it_return2 like bapiret2 occurs 0 with header line.
data: flines like tline occurs 0 with header line,
      string
(132) type c, fobject like thead-tdobject, fname  like thead-tdname.

data: line_no type i.
data: vendor_no type lifnr.

data: ntgew type ekpo-ntgew.
data: brgew type ekpo-brgew.

data: str type string.
data: str1 type string.

data: v_plant(4) type c,
      v_whno
(3) type c,
      v_sorg
(4) type c,
      v_dplnt
(4) type c,
      v_ccode
(4) type c,
      v_curr
(3) type c,
      v_shippt
(4) type c,
      v_country
(2) type c.

data: temp_char(200) type c.

data: banfn type vbep-banfn. " for purchase requisition number.

  
select single banfn into banfn from vbep where vbeln = zsalesdocument. " fetching purchse requisition number

  
" for PO Header data fill

   bapimepoheader
-comp_code = t001-bukrs.
   bapimepoheader
-doc_type = 'ZCOM'.
   bapimepoheader
-item_intvl = '00010'.

   bapimepoheader
-vendor = VENDOR.
   bapimepoheader
-pmnttrms = '0001'.
   bapimepoheader
-purch_org = ‘ABC’.
   bapimepoheader
-pur_group = 'E15'.
   bapimepoheader
-currency = 'USD'.
   
"bapimepoheader-ref_1 = wa_asntab-asnnumber.
   bapimepoheader
-incoterms2 = 'inco2'.

   bapimepoheaderx
-comp_code = 'X'.
   bapimepoheaderx
-doc_type = 'X'.
   bapimepoheaderx
-item_intvl = 'X'.
   bapimepoheaderx
-vendor = 'X'.
   bapimepoheaderx
-pmnttrms = 'X'.
   bapimepoheaderx
-purch_org = 'X'.
   bapimepoheaderx
-pur_group = 'X'.
   bapimepoheaderx
-currency = 'X'.
   
"bapimepoheaderx-ref_1 = 'X'.
   bapimepoheaderx
-incoterms2 = 'X'.

  
" for PO Line item Data FIll

    line_no 
= line_no + 10.
    it_bapimepoitem
-po_item = '00010'.
    it_bapimepoitem
-material = material.
    it_bapimepoitem
-plant = PLANT.
    it_bapimepoitem
-quantity = '1'.
    it_bapimepoitem
-free_item = 'X'.
    it_bapimepoitem
-conf_ctrl = '0004'.
    it_bapimepoitem
-incoterms2 = 'inco2'.

    it_bapimepoitem
-gross_wt = brgew.  “ gross weight

    it_bapimepoitem
-preq_no = banfn. “ purchase requisition number

    it_bapimepoitem
-net_weight = ntgew. “ net weight

    
append it_bapimepoitem to it_bapimepoitem.
    
clear it_bapimepoitem.

    it_bapimepoitemx
-po_item = '00010'.
    it_bapimepoitemx
-material = 'X'.
    it_bapimepoitemx
-plant = 'X'.
    it_bapimepoitemx
-quantity = 'X'.
    it_bapimepoitemx
-free_item = 'X'.
    it_bapimepoitemx
-conf_ctrl = 'X'.
    it_bapimepoitemx
-incoterms2 = 'X'.
    it_bapimepoitemx
-gross_wt = 'X'.
    it_bapimepoitemx
-preq_no  = 'X'.
    it_bapimepoitemx
-net_weight = 'X'.
    
append it_bapimepoitemx to it_bapimepoitemx.
    
clear it_bapimepoitemx.
    
clear: temp_char.

    it_poaccount
-po_item = '00010'.
    it_poaccount
-serial_no = '01'.
    it_poaccount
-quantity = '1'.

    it_poaccount
-costcenter = cost_center.
    
append it_poaccount to it_poaccount.

    it_poaccountx
-po_item = '00010'.
    it_poaccountx
-serial_no = '01'.
    it_poaccountx
-quantity = 'X'.
    it_poaccountx
-costcenter = 'X'.
    
append it_poaccountx to it_poaccountx.


    
clear po_number.

    
call function 'BAPI_PO_CREATE1'
    
exporting
      poheader         
= bapimepoheader
      poheaderx        
= bapimepoheaderx
    
importing
      exppurchaseorder 
= po_number
    
tables
      
return           = it_return2
      poitem           
= it_bapimepoitem
      poitemx          
= it_bapimepoitemx
      poaccount        
= it_poaccount
      poaccountx        
= it_poaccountx.

   
call function 'BAPI_TRANSACTION_COMMIT'
    
exporting
      
wait   = 'X'
    
importing
      
return = return.