Accessing SAP with Python

Python is an Open Source, powerfull and easy to learn programming language. From simple applications to projects like
built modern web apps for SAP


http://zachis.it/blog/what-are-the-advantages-of-python/

This Example was done via the IPython interactive notebook, which provides a rich architecture for interactive computing.

How to quickly generate PDFs from SAP outputs

Every time we generate an Output, a Bill of Lading, Invoice output or Order Confirmation as examples, the System generates a Spool Request, which can be consulted in transaction SP01.

Sometimes it is necessary to generate a PDF version of a particular output. One way of doing this is to use standard program RSTXPDFT4. Select the Spool Request ID and execute the program.


But there is a much quicker way of generating a PDF from an output. Bellow is an example using an Outbound Delivery ( VL02n ) Bill of Lading output.


Go to you transaction, in this case VL02n, and issue an output with print preview.





Then write PDF! and a new window will open with the PDF file. You will be able to save it to you computer.




Note: This can also be done in the SMARTFORMS transaction to send a particular layout to PDF.


Add Generic Object Services - GOS to Sales Order

If the Generic Object Services - GOS button is missing from the sales Order Transaction. User Parameter SD_SWU_ACTIVE must be set to X. 

This user parameter is not set by default due to performance issues.

Parameter for activating Generic Object Services in the Sales ORder

ABAP Refactoring - Local Class to Global Class


In abap it is possible to create both local classes, defined within an ABAP program, or create them in the repository, via the ABAP Class Builder ( SE24 or SE80). The difference being that the global class can be used in several reports, and the local class can only be used within the ABAP program it was created in.

Then, if you are creating program specific logic, you can use the a local class. One example of local classes are the one used to Unit Test a program, or function module, or local class. 
Note that with global classes you should use the ABAP UNIT TEST framework, which is another reason that makes working with global classes so compelling.

But sometimes we realize that a class created a a local class would be useful on another context, thus we should change the class from local to global, so that it can be used in that new context. fortunately the ABAP Class builder provides a very useful re-factoring tool to convert a local class to a global one.

This is how to do it.

  • SE24->Object type->Import->Local Classes in program






  • Select the program and class to be converted to the ABAP repository, and press the import button.





  • The class is created in the ABAP repository, and now the programs should be changed to use this instead of the old local class. 


Note: This method does not delete the old local class, only creates a duplicate in the ABAP repository. It is a good practice to remove the local class from the program because it will be no longer used.

How to find a SAP Enhancement when we know the Exit function module

Here is an easy way to find the correct SAP enhacement where a particular exit function module is when you know the function module name.

The exemple below is for EXIT FM EXIT_SAPLVEDA_011 found in the IDOC inblund Sales Order creation function module 'IDOC_INPUT_ORDERS'.

How to simulate a User Input in the command field - User Comand

There are many situations where it is usefull to force a user command programatically. One of such cases is when we want to update the selection screen after a screen field has changed.

To do so one only has to call the following Function module: SAPGUI_SET_FUNCTIONCODE .


  CALL FUNCTION 'SAPGUI_SET_FUNCTIONCODE'
    EXPORTING
      functioncode           = '=ENT'
    EXCEPTIONS
      function_not_supported = 1
      OTHERS                 = 2.
In the exemple above, we are forcing an ENTER.



Here is a practical example on filling two display fields on the selection screen, after the user changes the plant.


When the User changes the Plant the List of Warehouses and Storage Locations Changes Automatically


Code:

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_werks .
  PERFORM get_plants USING p_werks.


AT SELECTION-SCREEN OUTPUT.
  PERFORM fill_screen_texts USING     p_werks
                            CHANGING  p_lgort
                                      p_lgnum.

  LOOP AT SCREEN.
    CASE screen-group1.
      WHEN 'DIS'.
        screen-input  = '0'.
    ENDCASE.
    MODIFY SCREEN.
  ENDLOOP.



*&---------------------------------------------------------------------*
*&      Form  GET_PLANTS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_P_WERKS  text
*----------------------------------------------------------------------*
FORM get_plants  USING    p_p_werks.

  TYPES: BEGIN OF ty_s_werks,
    werks TYPE werks_d,
    name1 TYPE name1,
    END OF ty_s_werks.

  DATA: lt_werks            TYPE TABLE OF ty_s_werks.


  SELECT DISTINCT werks name1 FROM t001w
    INTO TABLE lt_werks
    WHERE werks IN ( SELECT DISTINCT werks FROM zmdm_crwh ).


  CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
    EXPORTING
      retfield        = 'WERKS'
      dynpprog        = sy-repid
      dynpnr          = sy-dynnr
      dynprofield     = 'P_WERKS'
      value_org       = 'S'
    TABLES
      value_tab       = lt_werks
    EXCEPTIONS
      parameter_error = 1
      no_values_found = 2
      OTHERS          = 3.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

  " Simulate an user enter after he has
  " changed the plant, so that we can force a
  " AT SELECTION-SCREEN OUTPUT event
  " and thus populate fields
  " p_lgort and p_lgnum with the list of the
  " corresponding storage locations and
  " warehouses for the selected plant
  CALL FUNCTION 'SAPGUI_SET_FUNCTIONCODE'
    EXPORTING
      functioncode           = '=ENT'
    EXCEPTIONS
      function_not_supported = 1
      OTHERS                 = 2.


ENDFORM.                    " GET_PLANTS
*&---------------------------------------------------------------------*
*&      Form  FILL_SCREEN_TEXTS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_P_WERKS  text
*      <--P_P_LGORT  text
*      <--P_P_LGNUM  text
*----------------------------------------------------------------------*
FORM fill_screen_texts  USING     value(p_werks)  TYPE werks_d
                        CHANGING value(p_lgort)   TYPE string
                                 value(p_lgnum)   TYPE string.

  DATA: lt_zmdm_crstl TYPE TABLE OF zmdm_crstl,
        lt_zmdm_crwh  TYPE TABLE OF zmdm_crwh.
  FIELD-SYMBOLS:
         <fs_zmdm_crstl>  TYPE zmdm_crstl,
         <fs_zmdm_crwh>   TYPE zmdm_crwh.

  CLEAR: p_lgort, p_lgnum.

  CHECK p_werks IS NOT INITIAL.

  SELECT * FROM zmdm_crstl  INTO TABLE lt_zmdm_crstl WHERE werks = p_werks.
  SELECT * FROM zmdm_crwh   INTO TABLE lt_zmdm_crwh WHERE werks = p_werks.

  " Fill the list of Storage Locations for the selected plant ( P_WERKS )
  " to be displayed on the selection screen field ( P_LGORT )
  LOOP AT lt_zmdm_crstl ASSIGNING <fs_zmdm_crstl>.
    IF p_lgort IS INITIAL.
      p_lgort = <fs_zmdm_crstl>-lgort.
    ELSE.
      CONCATENATE p_lgort <fs_zmdm_crstl>-lgort INTO p_lgort SEPARATED BY space.
    ENDIF.
  ENDLOOP.

  " Fill the list of Warehouses for the selected plant ( P_WERKS )
  " to be displayed on the selection screen field ( P_LGNUM )
  LOOP AT lt_zmdm_crwh ASSIGNING <fs_zmdm_crwh>.
    IF p_lgnum IS INITIAL.
      p_lgnum = <fs_zmdm_crwh>-lgnum.
    ELSE.
      CONCATENATE p_lgnum <fs_zmdm_crwh>-lgnum   INTO p_lgnum SEPARATED BY space.
    ENDIF.
  ENDLOOP.

ENDFORM.                    " FILL_SCREEN_TEXTS



 

Grouping BRF Objects

You can group BRF objects that belong together in the business sense (such as events, contexts, expressions, actions, rule sets). In doing so, you can set up hierarchy structures, in other words create higher-level and lower-level groups.

Groups have no influence on the BRF process.

How to do it: 


  1. Call transaction BRF: /nBRF
  2. Go to: Utilities  Object Group