Showing posts with label ABAP. Show all posts
Showing posts with label ABAP. Show all posts

Tuesday, 27 October 2015

Offset Concept in SAP ABAP

Most of the time in ABAP, we may have to read the part of data of a field (say X) and populate to other field (Say Y). In this case we have to use the offset concept to read the value.

Example:

SY-DATUM is the standard SAP field, which holds the current system date. The format of the date in SY-DATUM field will be YYYYMMDD.

Where,
  • YYYY => Year
  • MM => Month
  • DD => Day

In most of the cases we may have to read the value of month form the SY-SATUM and pass to some variable. Similarly we may have to read the value of year & day.


Below is a simple SAP ABAP program which gives clear picture on how to use offset concept.

======================

REPORT  ZVTEST_PGM_1.

DATAl_day(2)   TYPE c" Day
      l_month(2TYPE c" Month
      l_year(4)  TYPE c" Year

*--Print the value of Current System Date (YYYYMMDD)
WRITE:'Current System Date : ' sy-datum.

*--Use offset concept & populate the Year, Month & Day value to Local valriables

*--Year: Starting form Zeroth Position next 4 characters
l_year sy-datum+0(4).

*--Month: Starting form fourth Position next 2 characters
l_month sy-datum+4(2).

*--Day: Starting form sixth Position next 2 characters
l_day sy-datum+6(2).

*--Print value of Year, Month & date which is stored in local variables
WRITE:'Year  : 'l_year.
WRITE:'Month : 'l_month.
WRITE:'Day   : 'l_day.

======================

Hope post explained the OFFSET concept which we generally use in SAP ABAP.

Will share more useful articles soon.

Thanks & Regards,
Venugopal M N
SAP BI-ABAP Consultant 

Monday, 12 October 2015

ABAP Program to Convert Value Stored In a Variable to UPPER/LOWER Case

In general, most of the time in SAP, we may need to convert the value of the String or Character field (say name, city, etc..) to UPPER case of to LOWER case.

We can achieve the functionality using the simple ABAP syntax provided by SAP.

Syntax:
Assuming the P_STR is a variable which is of type STRING or CHAR (C).

To Convert to UPPER case:
To convert value stored in the P_STR to UPPER case, we can use the below syntax.
TRANSLATE p_str TO UPPER CASE.

To Convert to LOWER case:
To convert value stored in the P_STR to LOWER case, we can use the below syntax.
TRANSLATE p_str TO UPPER CASE.

Program:
REPORT  ZVTEST_PGM_1.

*--Parameter for User Input
PARAMETERSp_str TYPE string.

*--Convert to Upper Case
TRANSLATE p_str TO UPPER CASE.
*--Print Value in Output
WRITE:'Upper Case Value : 'p_str.

*--Convert to Lower Case
TRANSLATE p_str TO LOWER CASE.
*--Print Value in Output
WRITE:'Lower Case Value : 'p_str.

================================
Save & Activate.

Enter the input string ("VENUgopal NanjaPPA") & execute (F8).


Below is the converted output.

Thanks & Regards,
Venugopal M Nanjappa

Monday, 9 December 2013

How to Display Traffic Lights in Report Output : SAP ABAP

Sometime there will a requirements to display the Traffic Light Signal icons in report output.

In this post, we will see how to display the Traffic Lights Signal icons in report output.

Setp-1: Create a report program form SE38 transaction and paste the below code.

REPORT ZVENU_TRAFFIC_SIGNALS.

*--Declaration of variables
DATAL_GREEN(4)  TYPE VALUE '@08@',
      L_YELLOW(4TYPE VALUE '@09@',
      L_RED(4)    TYPE VALUE '@0A@'.

*--Printing the GREEN Traffic light
WRITE/ L_GREEN 'Green Signal'.

*--Printing the GREEN Traffic light
WRITE/ L_YELLOW 'Yellow Signal'.

*--Printing the GREEN Traffic light
WRITE/ L_RED 'Red Signal'.

Step-2: Save and Activate.

Step-3: Execute the report. Now you will be able to see the traffic lights in output, which is as soon below.


Important Notes:
  • Every ICONs in SAP has its own two digit code associated with it. 
  • To display the ICON in Report/ALV output we need to pass the the icon code between to @@ symbols. i.e @XX@.
Regards,
Venugopal M N

Wednesday, 20 November 2013

Step by Step Procedure to add Button in Application Toolbar in ALV Output: SAP ABAP

Problem Statement: 

In some situation, there will be a requirement to add custom button in application tool bar of ALV output. In this tutorial we will see the step by step procedure to add new button to application tool bar.


Step-1: Create the report ZAPP_BUTTTON_RPT from SE38 transaction and copy paste the below code.

*&---------------------------------------------------------------------*
*& Report  ZAPP_BUTTTON_RPT
*&---------------------------------------------------------------------*

REPORT zapp_buttton_rpt.

TYPE-POOLSslis.  " SLIS contains all the ALV data types

*&---------------------------------------------------------------------*
*& Data Declaration
*&---------------------------------------------------------------------*
DATAt_sbook      TYPE TABLE OF sbook.
DATAt_fieldcat   TYPE slis_t_fieldcat_alv,
      x_fieldcat  
TYPE slis_fieldcat_alv.

*&---------------------------------------------------------------------*
*& START-OF-SELECTION
*&---------------------------------------------------------------------*
START-OF-SELECTION.

*--Fetch the required data from databaase table
  
SELECT FROM sbook INTO TABLE t_sbook.

*--Build field catalog
  x_fieldcat
-fieldname  'CARRID'.    " Fieldname in the data table
  x_fieldcat
-seltext_m  'Airline Name'." Column description
  
APPEND x_fieldcat TO t_fieldcat.
  
CLEAR x_fieldcat.

  x_fieldcat
-fieldname  'CONNID'.
  x_fieldcat
-seltext_m  'Con. No.'.
  
APPEND x_fieldcat TO t_fieldcat.
  
CLEAR x_fieldcat.

*--Call FM to disaplay data in ALV GRID format
  
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    
EXPORTING
*     i_callback_program       = 
*     i_callback_pf_status_set = ''
*     i_callback_user_command  = ''
      it_fieldcat              
t_fieldcat
    
TABLES
      t_outtab                 
t_sbook
    
EXCEPTIONS
      program_error            
1
      
OTHERS                   2.
  
IF sy-subrc NE 0.
    
MESSAGE 'Error in displaying output.' TYPE 'E'.
  
ENDIF.

Step-2: Save, Activate and Execute the above program. Output of the program looks like as follows.



Step-3: Now go to SE90 transaction.
Expand the Objects from the left 'Repository Information System' section as follows.
i.e. Repository Information System >> Program Library >> Program Subobjects >> GUI Status
Double click on GUI Status, and enter Program Name as 'SAPLKKBL' and Extcute.

The screen looks as follows.


Upon executing, we will see the list of available GUI Status

Step-3: 
Select the CHECKBOX corresponds to STANDARD.
From Menu, go to GUI Status >> Copy (Click On COPY)


Step-4: Popup window appears, fill the details as follows and click on 'COPY'.


Step-5: Below window will appears, click on 'COPY'. 


Step-6: Now the GUI status is copied to your program. This can be seen as below.


Step-7: Double click on the 'ZSTANDARD' GUI Status. It will take you to below screen.


Step-8: Enter the function code as '&BUT' as shown above and double click on the same to proceed further. 

Upon double clicking following screen appears, select 'Static Text' Radio Button and press OK.

Step-9: Below screen appears, enter Function text, Icon Name and Information Text as show below and press OK.

As you can see in the above figure, we have selected "Folder Icon". You select the icon of your interest by F4 help on Icon name field.

Step-9: Assign Function to function key. In our example we have selected "Shift-F1", then press OK.

Step-10: Below screen appears, now validate the data entered and press OK.


Step-11: Upon pressing OK it will take you to below screen.


Save the changes to GUI status and Activate.

Step-12: Now go back to your program. Copy and Paste the below code. SAVE and ACTIVATE the program.

*&---------------------------------------------------------------------*
*& Report  ZAPP_BUTTTON_RPT
*&---------------------------------------------------------------------*

REPORT zapp_buttton_rpt.

TYPE-POOLSslis.  " SLIS contains all the ALV data types

*&---------------------------------------------------------------------*
*& Data Declaration
*&---------------------------------------------------------------------*
DATAt_sbook      TYPE TABLE OF sbook.
DATAt_fieldcat   TYPE slis_t_fieldcat_alv,
      x_fieldcat  
TYPE slis_fieldcat_alv.

*&---------------------------------------------------------------------*
*& START-OF-SELECTION
*&---------------------------------------------------------------------*
START-OF-SELECTION.

*--Fetch the required data from databaase table
  
SELECT FROM sbook INTO TABLE t_sbook.

*--Build field catalog
  x_fieldcat
-fieldname  'CARRID'.    " Fieldname in the data table
  x_fieldcat
-seltext_m  'Airline Name'." Column description
  
APPEND x_fieldcat TO t_fieldcat.
  
CLEAR x_fieldcat.

  x_fieldcat
-fieldname  'CONNID'.
  x_fieldcat
-seltext_m  'Con. No.'.
  
APPEND x_fieldcat TO t_fieldcat.
  
CLEAR x_fieldcat.

  
DATAl_repid TYPE sy-repid.
  l_repid 
sy-repid.

*--Call FM to disaplay data in ALV GRID format
  
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    
EXPORTING
      i_callback_program       
l_repid
      i_callback_pf_status_set 
'SUB_PF_STATUS'
      i_callback_user_command  
'USER_COMMAND'
      it_fieldcat              
t_fieldcat
    
TABLES
      t_outtab                 
t_sbook
    
EXCEPTIONS
      program_error            
1
      
OTHERS                   2.
  
IF sy-subrc NE 0.
    
MESSAGE 'Error in displaying output.' TYPE 'E'.
  
ENDIF.

*&---------------------------------------------------------------------*
*&      Form  sub_pf_status
*&---------------------------------------------------------------------*
*  Sub-Routine to Set the PF status
*----------------------------------------------------------------------*
FORM sub_pf_status USING rt_extab TYPE slis_t_extab..
  
SET PF-STATUS 'ZSTANDARD'.
ENDFORM.                    "sub_pf_status

*&---------------------------------------------------------------------*
*&      Form  user_command
*&---------------------------------------------------------------------*
*   Sub-Routine to handle the click on the ALV aoutput
*----------------------------------------------------------------------*
FORM user_command USING r_ ucomm    LIKE sy-ucomm
                       rs_selfield 
TYPE slis_selfield.

  
IF r_ucomm EQ '&BUT'.
    
MESSAGE 'Custom button is clicked.' TYPE 'S'.
  
ENDIF.

ENDFORM.  "User_command

Step-13: Execute(F8) the program. Now we can see the newly added BUTTON in application toolbar.


Click on the button to see the success message written in the code.

Conclusion:
Thus we have learnt the procedure to add the Button in Application Toolbar in ALV Output.


Regards,
Venugopal M N

Sunday, 13 October 2013

SAP ABAP - Best Coding practices

Following are the some of the best practices which ABAP developer should take care to achieve optimal performance.

1.       Avoid writing select query on a same table more than once
Since the retrieval of the data from the database take most of the processing time, it is better to fetch the required data from the table and store it in a internal table for further processing

2.       Refreshing the internal tables. Clearing variables & structures
In the custom program, before using the declared internal tables, work area& variables initialize them to default values. Use CLEAR statement for variables &work area. Use REFRESH statement for internal tables

3.       Clearwork area at the end of every LOOP pass
In order to avoid the problems such as carryover of the values of some field of work area corresponding to the last loop pass to the next loop pass, we need to clear the work area at the end of every LOOP pass.

4.       Avoid using the MOVE-CORRESPONDING
Compiler has to search & map the corresponding fields in case of MOVE-CORRESPONDING statement, hence it impact the performance. So, it is better to go for individual field assignment in case of copying the work area content to other inside the LOOP.

5.       Release the memory occupied by internal tables &work area
At the end of the program uses FREE statement to release the memory occupied by the internal tables, work area& internal tables.

6.       Avoid hard coding
Declare the CONSTANTS & use them in the program instead of hard coding the values.

7.       Check SY-SUBRC value after SELECT , READ statements
Putting the appropriate set of code based on the value of the sy-subrc after SELECT or READ statement enhances the performance & helps in avoiding execution of the unwanted code

8.       Check for internal table (it_tab) IS NOT INITIAL before using FOR ALL ENTRIES
Before using FOR ALL ENTRIES in the select statements, it is best practice to check does the table in the in the FOR ALL ENTRIES is having some values, else it result in fetching 0 records.

9.       Remove the unused constants, variables, work area& internal tables from the program
Unused constants, variables, work area & internal tables are the dead codes in the program. So, by removing the dead codes we can improve the performance of the code & make the code more readable.

10.       Avoid LOOP on the same internal table more than once
Try to include the required logic inside the same LOOP in order to enhance the performance.

11.       Avoid declaring the internal tables with header line
Declare separate work area to process the records of the internal table. By doing this one can avoid the chances of not clearing the header line in case of internal table with header line.

12.       SORT the internal table before using BINARY SEARCH in the READ statement.
READ statement fails to read the entry from the table if you are using BINARY SEARCH on unsorted internal table. So, SORT the internal table by the key fields which you are using in the READ statement.

13.       Place the most probable condition in the IF statement
This enhances the performance of the code when processing large amount of data.



Wednesday, 19 June 2013

How to download the source code to local directory in SAP?

SAP has provided a standard report ‘REPTRAN’ using which we can downloads the source code into local directory.

Steps:
  • Execute the program REPTRAN in SE38.
  • In selection screen, give the program name and the directory path to which you wanted to download and execute.

Regards,
Venugopal M N

Monday, 17 June 2013

How to get the components of structure i.e. list of fields of a structure in SAP-ABAP?

Sometime it might be required to get the components of the structure i.e. list of fields present in structure.


Use function module GET_COMPONENT_LIST to get the components of the structure.

Example:

REPORT ZVENU_TEST4.

DATA: t_components TYPE STANDARD TABLE OF RSTRUCINFO,
      x_components 
TYPE RSTRUCINFO.

TYPESBEGIN OF ty_tab,
        kunnr 
type kna1-kunnr,
        name1 
type kna1-name1,
       
END OF ty_tab.

DATA: x_kna1 TYPE ty_tab.

*--CAll FM to get hte components of structure
CALL FUNCTION 'GET_COMPONENT_LIST'
  
EXPORTING
    
PROGRAM          = sy-repid
    FIELDNAME        = 
'X_KNA1'
  
TABLES
    COMPONENTS       = t_components.

LOOP AT t_components INTO x_components.
  
WRITE:/  sy-tabix ,' - Field Name : ', x_components-COMPNAME.
ENDLOOP. Output:

Output:


Similarly, using this functional module we can get the components of structure which is present in another program. In such a case pass the program name to PROGRAM field and structure name to FIELDNAME field.

NOTE: This FM give the components of internal table with header line, whereas using this FM we cannot get the component of structure without header line.

Regards,
Venugopal M N