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”

How to look up a custom table in the transformation

This post describes the procedure to look up a custom table in bw transformation. The business scenario can be calculation of certain key figures based on the entries in the table.

This table can be custom table i.e. created by the user or any standard table too.

The first step for doing a look up is to store all the table entries or selected table entries in a global internal table. Continue reading “How to look up a custom table in the transformation”

Operations on Internal Tables

Processing an Internal table

To perform an operation on each and every record of an internal table

LOOP AT <itab>.          Each record will be transferred to Implicit
Header Line
:
:
ENDLOOP.
LOOP AT <itab> INTO <wa>.  Each record will be transferred to an Explicit
Header Line
:
:
ENDLOOP.
LOOP AT <itab> INTO <wa> FROM n1 TO n2.          to process a range of records
:
ENDLOOP.
Inserting Lines into Tables

Adding records either at the end or at the specified position. If index is not specified records will be added at the end of the table.

To add a line to an internal table

INSERT <line> INTO TABLE <itab> INDEX n.

INTO addition is not required if inserting is done through implicit header line.

To add several lines to an internal table.

INSERT LINES OF <itab1> [FROM <n1>] [TO <n2>] INTO TABLE <itab2>.
Appending Lines to Tables

Adding records at the end of the table,

To add a line to an internal table.

APPEND <line> INTO TABLE <itab>.

To add several lines to an internal table.

APPEND LINES OF <itab1> [FROM <n1>] [TO <n2>] INTO TABLE <itab2>.
Changing Lines

To change a single line of any internal table, use the MODIFY statement.

To change a single line

MODIFY TABLE <itab> FROM <wa> [TRANSPORTING <f1> <f2> ...].

Changing Several Lines

MODIFY <itab> FROM <wa> TRANSPORTING <f1> <f2>....WHERE <cond>.
Deleting Lines

To delete a single line of any internal table, use the DELETE statement.

DELETE TABLE <itab> FROM <wa>.

or

DELETE TABLE <itab> WITH TABLE KEY <k1> = <f1> ... <kn> = <fn>.

To delete more than one line using a condition-

DELETE <itab> WHERE <cond>.
Reading Lines of Tables

To read a single line of any table

READ TABLE <itab> INTO <wa> WITH KEY <k1> = <f1> ...<kn> = <fn>.

Or

READ TABLE <itab> INTO <wa> INDEX n.
Clearing Tables

To make internal tables empty

CLEAR <itab>.   Clears only implicit header line.
CLEAR <itab>[].   Clears only body records.
REFRESH <itab>.    Clears only body records.
FREE <itab>.     Clears only body records and frees the memory.
Counting number of records

To know the number of records contained in an internal table

DESCRIBE TABLE <itab> LINES n.

‘n’ is a variable that holds the count.

Sorting the records

To arrange the records of an internal table in an order

SORT <itab> BY Ascending/Descending <f1> Ascending/Descending <f2>..

Default order is Ascending.

Other Operations
  • An internal table can be assigned to another internal table.
  • Two internal tables can be compared with relational operators.

Both tables must be type compatible.

Function Modules in SAP

Some Important Function Modules

RANDOM_I4

FITRV_CALCULATOR

HR_99S_MONTHS_BETWEEN_DATES

MONTHS_BETWEEN_TWO_DATES

RH_GET_DATE_DAYNAME

DATE_GET_WEEK

SPELL_AMOUNT

F4_DATE

F4_CLOCK

F4_FILENAME

Function Modules
  • SE37 is the transaction code for creating function modules.
  • A function module name has a practical minimum length of three characters and a maximum length of 30 characters
  • Customer function modules must begin with Y_ or Z_
  • The name of each function module is unique within the entire BW system
  • Function modules have a special screen used for defining parameters. Parameters are not defined via ABAP/4 statements.
  • Leaving a function module is accompanied via the RAISE statement instead of check, exit or stop.
  • Every function module should be assigned to a Function group.
  • Function Group is to group all logically related Function Modules.
Function Groups

As stated before, a function group is a program that contains function modules. With each R/3 system, SAP supplies more than 5000 pre-existing function groups. In total, they contain more than 30,000 function modules. If the functionality you require is not already covered by these SAP supplied function modules, you can also create your own function groups and function modules.

Menu path to create a function group in the initial Screen of SE37 transaction is

Goto -> Function Groups -> Create Group

Syntax for the CALL FUNCTION Statement

The following is the syntax for the call function statement.

Call Function ‘Sample’

[exporting p1 = v1 ….]

[importing p2=v2 …]

[changing p3=v3 …]

[tables p4=it …]

[exceptions x1=n [others = n] ] .

where:

  • ‘Sample’ is the function module name
  • p1 through p4 are parameter names defined in the function module interface
  • v1 through v3 are variable or field string names defined within the calling program
  • ‘IT’ is an internal table defined within the calling program
  • n is any integer literal; n cannot be a variable
  • x1 is an exception name raised within the function module
  • All additions are optional
  • Call Function is a single statement. Do not place periods or commas after parameters or exception names
  • The function module name must be coded in upper case. If it is coded in lowercase, the function will not be found and a short dump will result.
  • It is recommended to use Pattern Button to call a Function Module
Function Module Interface

To pass parameters to a function module, you must define a function module interface. The function module interface is the description of the parameters that are passed to and received from the function module.

A function module interface includes

  • Import parameters
  • Export parameters
  • Changing parameters
  • Table parameters and
  • Exceptions
Import Parameters

Import   parameters are variables or field strings that contain values passed into the function module from the calling program.

Export Parameters

Export Parameters are variables or field strings that contain values returned from the function module.

Changing Parameters

Changing parameters are variables or field strings that contain values that are passed into the function module,  changed by the code within the function module, and then returned.

Table Parameters

Table parameters are internal tables that are passed to the function module, changed within it, and returned. The internal tables must be defined in the calling program.

Exceptions

An exception is a name for an error that occurs within a function module.

Example

Program which is calling a user defined function module that displays text

REPORT Sample1.
*--calling the function module.
CALL FUNCTION 'ZDISPLAY'
EXPORTING
text        = 'GOD is great'
row         = 12
column  =40
text_color = 3.
*--calling the function module.
CALL FUNCTION 'ZDISPLAY'
EXPORTING
text    ='Rose is red'
row    =5
column  = 10
text_color  =6.

*——————————————————–

Function module preparation in SE37 transaction.

*--Import Parameters
TEXT TYPE C
ROW TYPE I
COLUMN TYPE I
TEXT_COLOR TYPE I
*--Source code
SKIP TO LINE row.
POSITION column.
WRITE text
COLOR = text_color.

String Commands in ABAP

Below are some of the common and handy commands for manipulating strings in ABAP.

CONCATENATE

To join multiple strings together into one string.

DATA u(10) TYPE C VALUE 'Rust'.
DATA v(10) TYPE C VALUE 'Iron'.
DATA w(10) TYPE C VALUE 'Brown'.
DATA str(30) TYPE  C.
CONCATENATE u v w INTO str.
CONCATENATE u v w INTO str SEPARATED BY '***'.
SPLIT

To separate a given string into multiple sub strings

DATA str(30) TYPE C VALUE 'Rust Iron Brown'.
DATA: u(10) TYPE C,
              v(10) TYPE C,
               w(10) TYPE C.
SPLIT str AT ',' INTO u v w.
WRITE: / u,
       / v,
       / w.
TRANSLATE

To convert a given string from one form to another form

TRANSLATE str TO LOWER CASE.
TRANSLATE str TO UPPER CASE.
TRANSLATE str USING 'AXBY'.

in the above example, all occurrences of A in str are replaced by X and B in str are replaced by Y.

SEARCH

to search for substring in a main string.

SEARCH str for 'AD'.  // it is not case sensitive.

In the above example if substring ‘AD’ is existed in main string str then SY-SUBRC will become 0 otherwise

SY-SUBRC will become 4.

Moreover

SY-FDPOS maintains the offset value of the substring to search for substring in a main string.

FIND

To search for substring in a main string.

FIND 'AD' IN str.

It is case sensitive.

SY-FDPOS doesn’t work.

OFFSET CONCEPT IN STRINGS
  • This concept is used to extract or modify substrings of a given main string
  • OFFSET indicates position of a character in a string
  • OFFSET of 1st character is 0.
  • OFFSET of 2nd character is 1 and so on.
DATA W(20) TYPE C VALUE 'INFINITY'.
WRITE W+2(3). //here the output is 'FIN'.

this is the example of extracting substring from the main string here 2 indicates OFFSET of the substring and 3 indicates number of characters.

remember here ‘+’ is not an addition operator. Observe no spaces in both the sides.

W+3(2) = '99'.
WRITE W.

Here the output is INF99ITY

This is an example for setting substring of main string.

Courtesy: ABAP  technical documentation

 

Declarative Statements in ABAP

These statements define data types or declare data objects which are used by the other statements in a program or routine.

Ex:

TYPES, DATA, TABLES, CONSTANTS, FIELD-SYMBOLS, PARAMETERS, SELECT-OPTIONS

Constants  This keyword is for declaring constant identifiers.

CONSTANTS PI TYPE P DECIMALS 3 VALUE ‘3.147’.

Constants cannot be modified in the further program.

Field-Symbols These are similar to pointers in C language. Used for referring to any type of data object in a program.

FIELD-SYMBOLS: <FS1>, <FS2>.

DATA:  A TYPE I, str(20) TYPE C.

ASSIGN  A TO <FS1>.

WRITE <FS1>.    //Value of A will be displayed

ASSIGN STR TO <FS1>.

WRITE <FS1>.   //Value of STR will be displayed.

Types This keyword is used to define new user defined data types.

TYPES FLOATING TYPE P DECIMALS 6.

DATA A TYPE FLOATING.

DATA B TYPE FLOATING.

Tables This keyword is used to create a structure in RAM corresponding to a database table.

TABLES: mara, vbrk.

Type-Pools This keyword is used to attach a Data Dictionary (DDIC) type group to the current program. Type Group is a central repository object that maintains definitions of user defined data types.

TYPE-POOLS SLIS.              //Used in ALV Reporting

TYPE-POOLS SSCRFIELDS.     //Used for selection screen.

 

Courtesy: ABAP Technical Documentation

System Fields in ABAP

All the below system fields belong to the data dictionary (DDIC) Structure – SYST.

SY-INDEX for loop counting
SY-SUBRC condition of previous statement
SY-FDPOS position of substring in main string (works with SEARCH)
SY-ABCDE a string of 26 characters
SY-ULINE for horizontal lines of any length
SY-VLINE for only one vertical character
SY-DATUM for application server’s date
SY-UZEIT for application server’s current time
SY-DATLO for Presentation server’s date
SY-TIMLO for Presentation server’s current time
SY-DBSYS backend database name
SY-HOST name of application server
SY-SYSID name of the R/3 system
SY-OPSYS name of application server’s operating system
SY-MANDT Logon client number
SY-UNAME Logon User name
SY-LANGU Logon language
SY-MSGID message class name
SY-MSGNO message number
SY-MSGTY message type
SY-MSGV1 message variable 1
SY-MSGV2 message variable 2
SY-MSGV3 message variable 3
SY-MSGV4 message variable 4
SY-LSIND secondary list index
SY-PAGNO current page number
SY-LINSZ current width of a line
SY-LINCT current number of lines per page
SY-TABIX index of internal table’s record
SY-DBCNT no. of records processed by database statement
SY-DYNNR current screen number
SY-REPID current program name
SY-TITLE title of the program
SY-PFKEY name of the current GUI status
SY-UCOMM funtion code of the current pushbutton or menu option

Courtesy: ABAP Technical Documentation