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 BW Customer exit to enhance BEx variables

Here I will site an example of how to enhance a BEx variable using BW customer exit.

More details and insightful examples can be accessed in my ebook BEx variable enhancement by Customer Exit.

Display only two years of data in the BEx query

Scenario: Displaying only two years data in the BEx query based on the system date. The BEx query is based on a cube having 5 years of data.

Method:

Create a variable for calday or any time characteristic present in the cube.

I am taking 0calday as an example. SAP provides standard variables also to serve the same purpose. Here I will create a customer exit variable to illustrate the method.

Let the variable name is VAR2

This variable should be Interval value and mandatory.

There is no user input required when the query is accessed. Hence the BEx variable should not be “Ready for Input”.

Based on the system date, an Interval of two years will be calculated. For example, if system date is 24th Feb, 2017, the BEx query should display 2016 and 2017 data.

Drag and drop this variable in the filter section of the query.

Save the query.

Now go to the CMOD transaction and select your project. The project is generally provided by the client and should not be created by the developer. Click Display.

Click on the “components” button and then double click on the Function exit EXIT_SAPLRRS0_001

Double click on Include file name to go into the coding area.

Write the following code: Continue reading “How to write BW Customer exit to enhance BEx variables”

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”

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”