Wednesday, November 6, 2013

Logoff SAP session without warning

As a measure of control or simpy to release resources, we can force, with abap code, the shutdown of all instances of the current logon session. Here is a small program to show how to do that.

Monday, November 4, 2013

ABAP communication with COMM Ports ( Serial Ports )

One need that sometimes arise, is to allow the system to communicate with scales, printers or other devices. As far as I know, there isn't a simple and straigth forward way to do that.
In this post I'll present a class to make this possible. This class uses OLE to allow the desktop PC to communicate using a serial port. In a future post, I'll show how to create a webservice, running in a given PC, that can be accessed by a SAP Server in order to allow the communication in background and/or allow any pc to read a remote serial port.
For now, this only works in the running PC and there is the need to create and register the MSCOMM32.OCX (I'll show how to create a script to do this for 32 and 64 windows)

1 - Download MSCOMM32.OCX ( for instance, you can download it from here : Link)

2 - Create a text file named comm.reg with the following content:


3 - Create a batch file comm.bat with the following content:


4 - Place all three files in the same location and run, with administrator privileges, the comm.bat file).

5 - Now, the ABAP Class. Create the following class:















6 - Create these class attributes:






7 - Create these methods:










8 - Give these parameters to the created methods:









9 - Insert the appropriate code bellow in each of the corresponding methods:


10 - Enjoy :)

ABAP File Operations : Desktop and Server

Sometimes, we need to get a file from the presentation server or the application server. Usually, we need to fork the code in order to be able to use one or the other. Here, I'll show a simple class to make this task a lot easier. All the usual file/directory operations, are called the same way. The only distinction is a flag to indicate that the file should be considered in the desktop or in the server.
The class only has static methods, to use it, you just need to call any of the public methods.
This is the beggining, in the future I'll try to allow FTP, network shares, among others and consider to create a instanciable class to deal with connection, autentication, etc.

1 - Create the following Class

2 - Create the following Methods:



3 - Use the following parameters for each method:
























4 - Insert the appropriate code in each method:


Friday, November 1, 2013

ABAP Week Operations

Some SAP objects must deal with week periods. Here I'll show a small class to deal with some of the needs that arise when making calculations with week periods. This class makes simplier to use only one object to provide the needed calculations with weeks. As usual, there is allways room for improvement.

1 - Create the Class:
















2 - Create the Methods:







3 - Define Methods Parameters












4 - Insert the code in the appropriate methods:

method ADD_NUMBER.

datal_first_day type dats.


  do im_numb times.
    l_first_day get_first_dayim_week ch_week ).
    l_first_day l_first_day + 7.
    ch_week     get_from_dayim_day l_first_day ).
  enddo.

endmethod.

method DIFFERENCE.

  if im_week2(4im_week1(4).
    re_no_weeks im_week1+4(2im_week2+4(2).
  elseif im_week1(4> im_week2(4).
    re_no_weeks im_week1(4im_week2(452 + im_week1+4(2)

                                                         im_week2+4(2).
  else.
    clearre_no_weeks.
  endif.

endmethod.

method GET_FIRST_DAY.

  call function 'WEEK_GET_FIRST_DAY'
    exporting
      week         im_week
    importing
      date         re_day
    exceptions
      week_invalid 1
      others       2.

endmethod.

method GET_FROM_DAY.

  call function 'DATE_GET_WEEK'
    exporting
      date         im_day
    importing
      week         re_week
    exceptions
      date_invalid 1
      others       2.

endmethod.

method GET_LAST_DAY.

  re_day get_first_dayim_week im_week 6.

endmethod.

method SUBTRACT_NUMBER.

datal_first_day type dats.


  do im_numb times.
    l_first_day get_first_dayim_week ch_week ).
    l_first_day l_first_day 7.
    ch_week     get_from_dayim_day l_first_day ).
  enddo.

endmethod.

Thursday, October 31, 2013

Convert ALV to Excel and send it by email

One of the most requested abilities for an report is the possibility to run it as a job and send the output to a given user. However, the spool output of most ALV reports is unusable as is. To solve this problem, I'll show a simple class that takes an internal table, a fieldcatalog and a user email to create a xml excel file from the field catalog with the contents of the internal table and forward it to the email supplied.
I should warn that the excel output isn't something fancy. It's just a simple table. The class can be greatly enhanced.

1 - Create a class:



2 - Create the class atributes:





3 - Create the class methods:

4 - Create the method parameters:










5 - Insert the appropriate code in each method: