Wednesday, 13 August 2014

The magic of write statement with edit mask SAP ABAP tutorial

In this SAP ABAP tutorial I will show you how powerful write statement actually is.Please refer to the following programs.

Program 1:     

Real time requirement – The client wants to display the date field in YYMMDD format. Mainly used in smart forms where different company wants to display the smart forms in different date format.

DATA LV_OUTPUT(50TYPE C.

WRITE SY-DATUM.

ULINE.
WRITE SY-DATUM TO LV_OUTPUT YYMMDD.
WRITE LV_OUTPUT.

Output : 140813


Program 2:

Real time requirement – The client wants to display the output without leading zeros. Mainly used in smart forms where the client wants a character value to be displayed as ‘123’ instead of ‘0000000123’ or vice versa.

DATAnum(10)            TYPE VALUE '123',

      formatted_text(10TYPE c .


WRITE num TO formatted_text.
WRITE formatted_text.
WRITE num TO formatted_text NO-ZERO.
WRITE formatted_text.

Program 3:

Real time requirement – The client wants to display the output without leading zeros. Mainly used in smart forms where the client wants a character value to be displayed as ‘123’ instead of ‘0000000123’ or vice versa.

DATAlv_char(10)     TYPE c VALUE '0000000123',

      lv_output_c(10TYPE c,

      lv_output_n(10TYPE n.

WRITE 'Original Value : 'lv_char.

WRITE lv_char TO lv_output_c NO-ZERO.
WRITE lv_char TO lv_output_n NO-ZERO.
WRITE lv_char TO lv_char     NO-ZERO.

WRITE  'Value assigned to type c : '40 lv_output_c.
WRITE  'Value assigned to type n : ',40 lv_output_n.
WRITE  'Value assigned to the same variable : ',40 lv_char.

Program 4:

Real time requirement – The client wants to display the output without any sign neither + nor -. Mainly used in smart forms where the client wants  a character value to be displayed as the number itself without any sign.

DATAlv_var1   TYPE i,

      lv_output TYPE c LENGTH 10.


DO 10 TIMES.
  lv_var1 sy-index 5.
  IF lv_var1 >= 0.
    WRITE lv_var1 TO lv_output.
    WRITE / lv_output.

  ELSE.
    WRITE lv_var1 TO lv_output NO-SIGN.
    WRITE / lv_output.

  ENDIF.
ENDDO.


Program: 5.

Real time requirement – The client wants to display the output without any sign neither + nor -. Mainly used in smart forms where the client wants  a character value to be displayed as the number itself without any sign.

DATAlv_var1   TYPE i VALUE -10,

      lv_output TYPE c LENGTH 10.

WRITE / lv_var1.
WRITE lv_var1 NO-SIGN.


Thanks a lot again friends for reading this tutorial. If you find this is useful and helpful to you, please consider it sharing to your friends using the social buttons below. 




Monday, 28 April 2014

How to find the number of days between two date in SAP ABAP using Function Module

Following are the codes to find the number of days between two date in SAP ABAP using Function Module
*&---------------------------------------------------------------------*
*& Report  ZTESTR_5
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  ztestr_5.

DATA lv_days TYPE p.

PARAMETERS p_date1 TYPE sy-datum,
             p_date2 TYPE sy-datum.

CALL FUNCTION '/SDF/CMO_DATETIME_DIFFERENCE'
 EXPORTING
   DATE1                  p_date1
*   TIME1                  =
   DATE2                  p_date2
*   TIME2                  =
 IMPORTING
   DATEDIFF               lv_days
*   TIMEDIFF               =
*   EARLIEST               =
 EXCEPTIONS
   INVALID_DATETIME       1
   OTHERS                 2
          .
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.


WRITE / lv_days.

Similarly , you can also find the time difference by populating the time1 and time2 parameter.
Thanku , please do share and comment.

Wednesday, 26 March 2014

How to convert floating value ( 4.4000000000000000E+01 ) to char value ( 44 )

In our development we often came across many situation where we need to convert floating value to char value for example to display the value at the output of ALV .


REPORT  ztestr_2.

DATA lv_value_from TYPE atflv VALUE '4.4000000000000000E+01',
       lv_output     TYPE cha_class_view-sollwert.

CLEAR lv_output.

CALL FUNCTION 'QSS0_FLTP_TO_CHAR_CONVERSION'
  EXPORTING
    i_number_of_digits   2
    i_fltp_value         lv_value_from
    i_screen_fieldlength 16
  IMPORTING
    e_char_field         lv_output.

WRITE /  lv_output.

output :  44.00

NB : The exporting parameter ' i_number_of_digits ' determines the number of digits after decimal which are visible.
if you write i_number_of_digits = 1 ,
output will be 44.0

Sunday, 17 March 2013

Timestamp in yyyymmddhhmmss format in ABAP

REPORT  ZTESTR_TS.

DATA :  lv            TYPE string.

WRITE : / sy-datum , sy-timlo , sy-uzeit.

CONCATENATE sy-datum+0(4) sy-datum+4(2) sy-datum+6(2) sy-timlo+0(2) sy-timlo+2(2) sy-timlo+4(2)  INTO lv.

WRITE : / lv.

Saturday, 16 March 2013

Validating Select-options in SAP ABAP

Generally , while validating select-options we write the code as below.

*It is only partially correct , refer the codes below
select matnr from mara into lv_mara
                             where matnr in so_matnr.
if sy-subrc is not initial.
   "error message
endif.

but above code , is wrong . Now suppose the user enters two material number , 0000000002 and
0000000045 where the 2nd one does not exist in the system .But sy-subrc will become 0 , and the error message will not be executed .
So , we have to consider the following codes that will help !!!

REPORT  ztestr_14.

TABLES : mara.

SELECT-OPTIONS : so_matnr FOR mara-matnr.

TYPES : BEGIN OF ty_matnr,
          matnr TYPE matnr,
        END OF   ty_matnr.

DATA : li_matnr TYPE STANDARD TABLE OF ty_matnr.
FIELD-SYMBOLS : <fls_matnr> TYPE ty_matnr.

IF so_matnr IS NOT INITIAL.

  SELECT matnr FROM mara
               INTO TABLE li_matnr
               WHERE matnr IN so_matnr.

  IF sy-subrc IS INITIAL.

    SORT li_matnr ASCENDING BY matnr.

    LOOP AT so_matnr.

      READ TABLE li_matnr ASSIGNING <fls_matnr>
                          WITH KEY  matnr = so_matnr-low
                          BINARY SEARCH.

      IF sy-subrc IS NOT INITIAL.

        PERFORM error_handling USING text-e01 so_matnr-low.

      ENDIF.

    ENDLOOP.

    IF so_matnr-high IS NOT INITIAL.

      READ TABLE li_matnr ASSIGNING <fls_matnr>
                          WITH KEY  matnr = so_matnr-high
                          BINARY SEARCH.

      IF sy-subrc IS NOT INITIAL.

        PERFORM error_handling USING text-e01 so_matnr-high.

      ENDIF.

    ENDIF.

  ELSE.

     PERFORM error_handling USING text-e01 so_matnr-low.

  ENDIF.

ENDIF.

form ERROR_HANDLING  using    p_text_e01 TYPE string
                              value type any.

       DATA : lv_message TYPE string.
       CONSTANTS : lc_plach1 TYPE char2 VALUE '&1'.
        CLEAR : lv_message.
        lv_message = p_text_e01." where text-e01 = Material number &1 does not exist
        REPLACE lc_plach1 WITH value
        INTO lv_message.
        MESSAGE e000(zh) WITH lv_message.

endform.                    " ERROR_HANDLING

Sunday, 3 March 2013

Validating Local/Presentation path exist or not in SAP ABAP

Following is the program that checks whether the local path entered is valid or not.

TYPE-POOLS : abap.

Paramaters : p_zloc type rlgrap-filename.

    DATA : lv_dir TYPE string,
           lv_bol TYPE abap_bool,
           lv_string TYPE rlgrap-filename.
    DATA lv_w_string TYPE string.
    DATA : lv_i TYPE i,
                  lv_j TYPE i.

    lv_string   = p_zloc.
    lv_w_string = lv_string.

    DO.
      SEARCH lv_w_string FOR '\'.
      IF sy-subrc EQ 0.
        lv_i = sy-fdpos + 1.
        lv_j = lv_i + lv_j.
        lv_w_string = lv_w_string+lv_i.
      ELSE.
        EXIT.
      ENDIF.
    ENDDO.

    lv_dir = lv_string+0(lv_j).

    CALL METHOD cl_gui_frontend_services=>directory_exist
      EXPORTING
        directory            = lv_dir
      RECEIVING
        result               = lv_bol
      EXCEPTIONS
        cntl_error           = 1
        error_no_gui         = 2
        wrong_parameter      = 3
        not_supported_by_gui = 4
        OTHERS               = 5.
    IF  lv_bol NE 'X'." if lv_bol = 'X' the path is a vlid path
      MESSAGE e000(zh) WITH text-008.
    ENDIF.

How to separate path name and file name in abap

parameters : p_locpth type RGGRAP-FILENAME. " Parameter where user input the full path

    DATA : lv_w_string TYPE string,
                  lv_string TYPE rlgrap-filename.

    DATA : lv_i TYPE i,
                  lv_j TYPE i.

    lv_string   = p_locpth.
    lv_w_string = lv_string.

    DO.
      SEARCH lv_w_string FOR '\'.
      IF sy-subrc EQ 0.
        lv_i = sy-fdpos + 1.  " sy-fdpos gives the location of the character you are searching and it *starts with 0
        lv_j = lv_i + lv_j.
        lv_w_string = lv_w_string+lv_i.
      ELSE.
        EXIT.
      ENDIF.
    ENDDO.

    lv_dir = lv_string+0(lv_j).

write : / lv_dir.

Wednesday, 26 September 2012

SAP for a layman...

Whenever you hear the word SAP from a layman , in almost all the cases they pronounce it wrong as "SAP" (like the sap of the tree) , it happens with me also when i first speak this word  . Actually the correct way is "ess-ay-pea" pronounced as S (separate) , A (separate) and P (separate) not in continuous 'SAP'.

SAP  stands for Systems Applications and Products in Data Processing is a software that integrates all the business functional areas of  a company in a synchronous way .
The business fnctional areas or functional modules or application areas represents various modules on the basis of the business areas such as
1.SD - Sales and Distribution.
2.PP - Production Planning.
3.WM - Warehouse Management
4.MM - Material Management.
5.FI - Financial Accounting.
6.PM - Plant Maintenance etc

Let me explain the above definition in detail , Tata Motors producing variety of cars having headquaters in different different locations of India say for example Hyderabad , Delhi , Mumbai and Bangalore . Now at the time of festivals suddenly the selling of cars increases , so the sales department ,SD(Sales and Distribution) has to inform the PP(Production Planning) to give more cars to the Warehouse Management , the Production Planning in turn send report to MM(Material Management), which in turn send report to different different vendors to get the different parts of cars such as tyres , steels , glass etc and finally report goes to FI (Financial Accounting) and the final report of the entire process goes to the CEO . The entire process have to be done at the same time to be more productive . Now suppose the SD module is maintained differently from other modules , so the revenue generated from sales and more requirement of product has to be updated manually into other modules , which is not at all synchronous and more prone to errors.
Thus to solve this problem we use SAP because it has very high level of integration between various functional modules which ensures proper synchronization and consistency with zero error . SAP integrates all its functional modules because of its centralized database for all the different modules of business.
Now upto this point you are clear about SAP , now know some of the facts about SAP
SAP came from the German word "Systeme, Anwendungen, und Produkte in der Datenverarbeitung".
It is developed by five IBM employees Hans-Werner Hector , Hasso Plattner , Dietmar Hopp , Klaus Tschira and Claus Wellenreuther in 1972 .

Saturday, 25 August 2012

Object Oriented Programming

Object Oriented Programming as the name suggest is a programming technique which revolves around objects . In simple terms it is the process by which the real world problems are converted into software solutions . Let us take an example , tata safari is the real world object of class car , this object has two characteristics i.e it has a state(attribute or data)  and behavior (methods or functions).The state includes engaged gear , current speed etc and the behavior includes accelerations , changing gear , applying brakes etc . The software solution of the above will be attributes (data or state) and behavior (methods or functions) .
Attributes includes :   data : engaged_gear  type c,    (In ABAP)
                                             current_speed  type n.
                                   or
                                   int current_speed; (In Java)
Behaviors includes : method apply_brakes()
                                    {
                                        current_speed = current_speed - 2.
                                     }
                                    method accelerations() etc.
Following are the features of Object Oriented Programming

1.Class -  Class is a pattern or style based on which all the objects in that class is created . Class is a collection of attributes and behaviors of similar types of objects.
But a class does not have its physical existence it is the objects present in a class that have physical existence like the folders in our computer does not have physical existence rather it is the files that present inside the folder has physical existence i.e occupies some memory .
  • Attributes : Attributes contains the data that can be stored in the objects of a class.
  • Behaviors : Behaviors refers to the functionality of the object.
2.Object -  Object is the runtime instance of a class which has physical existence and having its own attributes and behaviors . We can create any number of objects based on a single class and the process of object creation is known as instantiation.

Example - Human is a class because we can not touch human i.e it does not have physical existence but we can touch the hands , nose , hair which are the objects of human class .
Car is a class because we can not touch car but we can touch indica car , safari car etc which are objects of class car.

3.Abstraction - Abstraction is a feature through which the essential parts of the object is represented hiding the background details.
  • Data is abstracted by high level language constructs such as numbers ,letters , high level data concepts etc.
  • Functionality is abstracted by presenting interfaces that hide complex logics such as methods or functions.
Example - When we are driving car we simply apply the brakes without knowing what is happening in the background that stop the car i.e unnecessary details are abstracted.

4.Encapsulation - Encapsulation is the packaging of attributes(data member) and behaviors(member functions) into one reusable module where user can only call certain functions or methods  but cannot access the data directly.This feature enables the class to hide the implementation details of the class.
It helps in data data hiding which leads to data security(making attributes and behaviors private) .

Example - Think of the capsules that doctor recomend you , which is nothing but the packaging of different benefitial drugs , leaves etc into one reusable module.

5.Polymorphism -  The term is derived from greek words poly means many and morphos means forms . So polymorphism means method having same name have different forms i.e objects from different classes react differently to the same method calls.

Example - Our behaviour is different when we are with our parents , different when we are with our friends , teachers etc . Similarly same method behaves differently based on which class object call it.

6.Inheritance -  Inheritance is the feature through which the attributes and behaviors of one object can be reused in other which leads to reusability and reusability leads to less amount of coding.

7.Exception Handling  -  Exception handling is a feature through which user can handle the abnormal behavior (number divided by 0 is an example of abnormal behaviour) of the programme . Whenever an exception , control navigates to the exception block handles the exception and exit out of the programme normally.

If you like this tutorial , please like and comment...