Sample ABAP program for Updating notepad file data to Internal table and Sending it to Application server

*--Structure type
TYPES: BEGIN OF TY_STD,
RONO TYPE C LENGTH 4,
NAME1 TYPE C LENGTH 20,
GEN TYPE C LENGTH 6,
FEES TYPE C LENGTH 10.
END OF TY_STD.
*--Header line and internal table
DATA WKA TYPE TY_STD.
DATA INT_TAB TYPE TABLE OF TY_STD.
*--Application server files are known as "Datasets"
*--here "fname" is the Dataset
DATA FNAME TYPE C LENGTH 40 VALUE "StudentsInfo.txt".
*--Convert notepad file data into internal table
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
FILENAME = 'C:\Documents and Settings\Administrator\Desktop\students.txt'
HAS FIELD_SEPARATOR = 'X'
TABLES
DATA_TAB = INT_TAB.
*--Sending data to Application Server
OPEN DATASET FNAME FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
LOOP AT INT_TAB INTO WKA.
TRANSFER WKA TO FNAME.
ENDLOOP.
CLOSE DATASET FNAME.

Sample ABAP program to upload excel data to internal table and sending it to Application server

*--Attaching type group to the program
TYPE-POOLS TRUXS.
*--Structure Type
TYPES: BEGIN OF TY_STD,
RONO TYPE C LENGTH 4,
NAME1 TYPE C LENGTH 20,
GEN TYPE C LENGTH 6,
FEES TYPE C LENGTH 10,
END OF TY_STD.
*--Header line and internal table
DATA WA TYPE TY_STD.
DATA ITAB TYPE TABLE OF TY_STD.
*--Dummy Internal table for Function module purpose
DATA IT_RAW TYPE TRUXS_T_TEXT_DATA.
*--Application Server files are known as "Datasets"
*--here "fname" is the Dataset
DATA FNAME TYPE C LENGHT 40 VALUE 'Students.txt'.
*--Convert Excel flat file data into Internal table
CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
EXPORTING
I_TAB RAW DATA = IT_RAW
I_FILENAME = 'C:\Documents and Settings\Administrator\Desktop\students.xls'
TABLES
I_TAB_CONVERTED_DATA = ITAB.
*--Sending data to Application Server
OPEN DATASET FNAME FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
LOOP AT ITAB INTO WA.
TRANSFER WA TO FNAME.
ENDLOOP.
CLOSE DATASET FNAME.

Sample ABAP program to upload CSV data into Internal table and sending it to Application server

REPORT.
*--Attaching type group to program
TYPE-POOLS KCDE.
*--Structure type
TYPES: BEGIN OF TY-STD,
RONO TYPE C LENGTH 4,
NAME1 TYPE C LENGTH 20,
GEN TYPE C LENGTH 6,
FEES TYPE C LENGTH 10,
END OF TY_STD.
*--Header line and Internal table
DATA WKA TYPE TY_STD.
DATA ITABLE TYPE TABLE OF TY_STD.
*--Header line and internal table for CSV data
DATA IS_CSV TYPE LINE OF KCDE_INTERN.
DATA IT_CSV TYPE KCDE_INTERN.
*--Application server files are known as "Datasets"
*--here "fname" is the Dataset
DATA FNAME TYPE C LENGTH 40 VALUE 'StudentsInfo.txt'.
*--Convert CSV flat file data into internal table
CALL FUNCTION 'KCD_CSV_FILE_TO_INTERN_CONVERT'
EXPORTING
I_FILENAME = 'C:\Documents and Settings\Administrator\Desktop\Students.csv'
I_SEPARATOR = ','
TABLES
E_INTERN = IT_CSV.
LOOP AT IT_CSV INTO IS_CSV.
CASE IS_CSV-COL.
WHEN 1.
WA-RONO = IS_CSV-VALUE.
WHEN 2.
WA-NAME1 = IS_CSV-VALUE.
WHEN 3.
WA-GEN = IS_CSV-VALUE.
WHEN 4.
WA- FEES = IS_CSV-VALUE.
ENDCASE.
AT END OF ROW.
APPEND WKA TO ITABLE
CLEAR WKA.
ENDAT.
ENDLOOP.
*--Sending data to application server
OPEN DATASET FNAME FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
LOOP AT ITABLE INTO WKA.
TRANSFER WKA TO FNAME.
ENDLOOP.
CLOSE DATASET FNAME.
.

 

Sample ABAP program to create random data in a notepad file

Below is the program in ABAP to prepare a question paper with 20 questions on Arithmetic operations on two numbers.

The program will generate numbers and arithmetic operations randomly.

The program will save these random questions in a .txt file on the local system.

REPORT.
*--Structure type
TYPES: BEGIN OF TYP_NUM,
N1 TYPE I,
OP TYPE C,
N2 TYPE I,
CH TYPE C,
END OF TYP_NUM.
DATA OPT TYPE I.
DATA WA TYPE TYP_NUM.
DATA ITAB TYPE TABLE OF TYP_NUM.
DO 20 TIMES.
CALL FUNCTION 'RANDOM_I4'
EXPORTING
RND_MIN = 10
RND_MAX = 90
IMPORTING
RND_VALUE = WA-N1.
CALL FUNCTION 'RANDOM_I4'
EXPORTING
RND_MIN = 10
RND_MAX = 90
IMPORTING
RND_VALUE = WA-N2.
CALL FUNCTION 'RANDOM_I4'
EXPORTING
RND_MIN = 1
RND_MAX = 4
IMPORTING
RND_VALUE = OPT.
CASE OPT.
WHEN 1.
WA-OP = '+'.
WHEN 2.
WA-OP = '-'.
WHEN 3.
WA-OP = 'x'.
WHEN 4.
WA-OP = '/'.
ENDCASE.
WA-CH = '='.
APPEND WA TO ITAB.
CLEAR WA.
ENDDO.
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
FILENAME = 'c:/ques_paper.txt'
TABLES
DATA_TAB = ITAB.

 

Some useful BAPIs

BAPI stands for Business Application Programming Interface. These are remote enabled function modules (RFCs).

BAPIs are used to Integrate SAP systems with Non-SAP systems

These FM names start with BAPI_

 

*========================================================
*BAPI_COMPANYCODE_GETDETAIL
*This function module gives address details of a company code
*========================================================
DATA ADDRS1 TYPE BAPI0002_3.
CALL FUNCTION 'BAPI_COMPANYCODE_GETDETAIL'
EXPORTING
COMPANYCODEID  = '1000'
IMPORTING
COMPANYCODE_ADDRESS = ADDRS1.
WRITE/:
ADDRS1-NAME,
ADDRS1-STREET,
ADDRS1-CITY,
ADDRS1-COUNTRY.
*================================================================
*BAPI_SALESORDER_GETLIST
*This function module gives list of Sales orders in the form of an internal table
*================================================================
DATA ORD TYPE BAPIORDERS.
DATA INT_TAB TYPE TABLE OF BAPIORDERS.
PARAMETERS PAR_CUST TYPE C LENGTH 10.
CALL FUNCTION 'NUMERIC_CHECK'
EXPORTING
STRING_IN = PAR_CUST
IMPORTING
STRING_OUT = PAR_CUST.
CALL FUNCTION 'BAPI_SALESORDER_GETLIST'
EXPORTING
CUSTOMER_NUMBER = PAR_CUST
SALES_ORGANIZATION = '1000'
TABLES
SALES_ORDERS = INT_TAB.
LOOP AT INT_TAB INTO ORD.
WRITE:/ ORD-SD_DOC,
ORD-DOC_DATE,
ORD-MATERIAL.
ENDLOOP.
*===========================================================
BAPI_CUST_SAREAS_MATERIALS_GET
*This function module gives list of Materials available to a customer in his sales area
*============================================================
DATA MATL TYPE BAPIMAKT.
DATA INT_TAB TYPE TABLE OF BAPIMAKT.
CALL FUNCTION 'BAPI_CUST_SAREAS_MATERIALS_GET'
EXPORTING
CUSTOMERNO = '0000002311'
SPECIFIC_SALESORGANISATION = '1000'
SPECIFIC_DISTR_CHANNEL = '01'
TABLES
MATERIALS = INT_TAB.
LOOP AT INT_TAB INTO MATL.
WRITE: / MATL-MATERIAL,
MATL-MATL_DESC.
ENDLOOP.

 

Some useful standard Function modules

*=============================================================
*SPELL_AMOUNT
*This function module converts amount into English words
*=============================================================
DATA WORDS1 TYPE SPELL.
CALL FUNCTION 'SPELL_AMOUNT'
EXPORTING
AMOUNT = 679340232
IMPORTING
IN_WORDS = WORDS1.  * WORDS1 is a structure with some components
WRITE:/ WORDS1-WORD.

Continue reading “Some useful standard Function modules”

Some Useful Function Modules involving Dates

REPORT.
*===============================================
*NUMBER_OF_DAYS_PER_MONTH_GET
*This function module gives number of days in a given month of given year
*===============================================
DATA DAY1 TYPE T009B-BUTAG.
CALL FUNCTION 'NUMBER_OF_DAYS_PER_MONTH_GET'
EXPORTING
PAR_MONTH = '02'
PAR_YEAR = '2012'
IMPORTING
PAR_DAYS = DAY1.   "Datatypes of parameters must match each other
WRITE DAY1.

Continue reading “Some Useful Function Modules involving Dates”

OPEN SQL examples in ABAP – Part 3

*===================================================
*Filtering the fields
*displaying 2 fields data from SCARR table (Airlines)
*with the help of two individual variables
*by declaring them with built-in-data types
*====================================================
DATA CIDN(3) TYPE C.
DATA CNAME(20) TYPE C.
SELECT CARRID
CARRNAME
INTO (CID, CNAME)
FROM SCARR.
WRITE:/ CID, CNAME.
ENDSELECT.
*******************************************************
*displaying 2 fields data from SCARR table (Airlines)
*with the help of two individual variables
*by declaring them with DB Table field References
DATA CIDN TYPE SCARR-CARRID.
DATA CNAME TYPE SCARR-CARRNAME.
SELECT CARRID
CARRNAME
INTO (CIDN, CNAME)
FROM SCARR.
WRITE:/ CIDN, CNAME.
ENDSELECT.

Continue reading “OPEN SQL examples in ABAP – Part 3”

OPEN SQL EXAMPLES IN ABAP – PART 2

*******************************************
**displaying Raw Materials and Finished products
**only from MARA table.
DATA WA TYPE MARA.
*
**--WHERE clause is for filtering the records
SELECT * FROM MARA INTO WA
WHERE MTART = 'ROH' OR MTART = 'FERT'.
WRITE: / WA-MATNR,
WA-MBRSH,
WA-MTART,
WA-MEINS.
ENDSELECT.
*
**************************************************

Continue reading “OPEN SQL EXAMPLES IN ABAP – PART 2”