ABAP in BW Data load Processes

Learn step by step approach of writing start routine, field routine and end routine in BW transformations.

Learn about ABAP routine filters in DTP and info packages and use of OLAP variables as Filters.

Access the ebook on Amazon at amazingly low prices.

 

How to write ABAP program to trigger PC based on file in application server

In this post, I will detail on how to write an ABAP program which checks for a specific file in the application server and triggers a process chain. If no file is found, it just displays a message and does not do anything.

Once the program is ready, you need to schedule a job with this program which runs in the background every 30 minutes for example to check the file and trigger the PC.

Continue reading “How to write ABAP program to trigger PC based on file in application server”

How to write ABAP program to Merge multiple files in one file in Application server

In some scenarios, it may happen that business has loaded multiple files in a period for sales for example. All these files should be merged into one consolidation file and loaded once to target instead of loading each file separately.

Also the consolidation file will have a standard name which the infopackage can recognize and pick up rather than changing the infopackage to load multiple files with different names.

Continue reading “How to write ABAP program to Merge multiple files in one file in Application server”

How to write a program to move files in application server to an archive folder

Many times after loading the file from the application server to BW targets, we need to move it in a separate folder such as archive folder to avoid  the same file being repeatedly processed. Also the archive folder will serve as a storage for history of all the files that were already loaded.This helps in referencing them at a later point in time if required.

Continue reading “How to write a program to move files in application server to an archive folder”

How to write infopackage program to pick up a certain file from the application server

We can write a program in the infopackage in BW to pick up a certain file based on its name from the application server and load it. This is required if there are multiple files in the same folder and only a certain file needs to be loaded out of those.

Suppose we have a scenario like this: Business uploads the files for multiple sales division in a folder in the application server –  /usr/SAP/DEV/Reports/target_sales.csv.

Continue reading “How to write infopackage program to pick up a certain file from the application server”

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.