Showing posts with label EH&S. Show all posts
Showing posts with label EH&S. Show all posts

Monday, July 6, 2020

SAP EHS: How to select Dangerous Goods Data for Sales&Distribution Printouts

Depending on the setup of your ERP System  and the regional requirements, orders, deliveries and shipments in the Sales&Distribution module produce different kinds of printouts (delivery notes, shipment lists, bills of lading, etc. ).

Usually, those printouts contain Dangerous Goods Data, which is relevant for transport.
If the prinouts use the standard DG Data retrieval logic and your system has a standard DG Data maintenance & customizing, there is nothing to worry about.
The standard logic takes the material and the route from the SD document and determines the DG regulation for which the DG Data is retrieved.

These checks are specified in the customizing activity 'Assign DG Check Schema Determination Routines' per Sales Organisation.

There you can check which check schema is assigned.

You can check your printing settings in transaction NACE.



However, if the prinouts use customer specific data retrieval, z-logic, direct selects on the DGTMD and DGTM2 tables for every printout, then you might opt for a central, reusable solution to include in your retrieval code for all printouts.

The central function can use following logic available in standard function modules.

In this post I will focus on the deliveries:


  • Read the delivery item data using 'RV_DELIVERY_PRINT_VIEW'. Maybe this step is already there in the data retrieval logic.








  •  CALL FUNCTION 'DG56_GET_TRM_CNTRIES_DELV'
          EXPORTING
            e_vbdkl                 <s_vbdkl>
            i_nspras                iv_nspras
          TABLES
            e_tvbdpl                it_vbdpl
            e_rdgcountryreglang_tab lt_rdgcountryreglang
          EXCEPTIONS
            get_data_error          1
            OTHERS                  2.
        APPEND LINES OF lt_rdgcountryreglang TO lt_rdgcountryreglang_all.


      LOOP AT lt_rdgcountryreglang_all ASSIGNING <s_rdgcountryreglang>.
        CLEAR ls_rdgmdsel.
        ls_rdgmdsel-matnr  <s_rdgcountryreglang>-matnr.
        ls_rdgmdsel-lwdg   <s_rdgcountryreglang>-lwdg.
        ls_rdgmdsel-valdat sy-datum.
        APPEND ls_rdgmdsel TO lt_rdgmdsel.
      ENDLOOP.




  •       CALL FUNCTION 'HAZMAT_RECORD_READ_FROM_DB'
        EXPORTING
          i_flg_read_undeleted_only  abap_true
        TABLES
          i_rdgmdsel_tab             lt_rdgmdsel
          e_buftab                   lt_rdgma
        EXCEPTIONS
          no_records_found           1
          no_records_for_all_entries 2
          OTHERS                     3.



The same can be done for shipments using the standard module 'DG56_GET_TRM_CNTRIES_SHIP'
 or for orders.

The return structure RDGMA contains all print-relevant fields from the DGTMD and DGTM2 tables.

We can  easily and simply read the DG Data using standard functions according to the system customizing without writing any direct  select statements on DGTMD / DGTM2 tables.

This approach makes sure that you are selecting the correct DG Data for the SD Document.

For more information about DG Data, please check my
previos post.






Tuesday, May 26, 2020

SAP ABAP CDS Views: Data-to-Code vs Code-to-Data - an example with aggregation

I am going to discuss two approaches to a requirement that I have received often from customers.

EH&S: Business wants to know the specifications where there is an instance maintained for a certain property.

In EH&S it is possible to maintain more than one intance per property. Multiple property  instantes can be confusing for the business, so  customers wanted to have an evaluation if they have EH&S specifications with multiple instances (same sort) for certain properties.

Of course, it was possible to solve the requirement with the help of an ABAP Report looking something like that:

Back-End SAP, ABAP:

The logic is in my report. (Data-to-Code Approach)


REPORT zehs_r_inst_count.

TABLES: estvh, estva, estrh.

SELECT-OPTIONS: s_estcat FOR estvh-estcat DEFAULT 'SAP_EHS_1023_094'.
select-OPTIONS: s_subid for estrh-subid.

SELECT COUNT( DISTINCT estva~recn ) AS count, estva~ord,estva~recnroot, estva~recntvh, estvh~estcat, estrh~subid
INTO TABLE @DATA(lt_inst)
FROM estva INNER JOIN estvh ON estvh~recn = estva~recntvh JOIN estrh ON estva~recnroot = estrh~recnroot
WHERE estvh~estcat IN @s_estcat and estrh~subid in @s_subid AND estva~delflg EQ @space AND estvh~delflg EQ @space
and estrh~delflg eq @space
GROUP BY estva~recnroot, estva~recntvh, estva~ord, estvh~estcat, estrh~subid.

 perform show_alv.

Nonetheless, it is far more flexible and efficient to use CDS Views and not to hold so much DB logic in the application:


Eclipse, Open SQL:

The logic is contained in the view (Code-to-Data or code pushdown Approach)




For developers who are new to SAP CDS Views appear very easy, intuitive and sql-like.

I habe been usng the data-to-code a lot in the past, so that I notice a huge efficiency improvement when using CDS Views.

I can check my view in transaction se16n:


This new programming model supports clean code and reusability.
It is satisfying to clean the application from  sql statements and to push them to the data base. 

There are many options for using the newly created view:

 - If the evaluation should be pulled only once, this can be done using transaction se16n
-  If the evaluation should be done by a user, a report or transaction can be created
-  For views that will be checked often by many users, a FIORI App can be created by publishing the view as an oData Service

Sunday, April 26, 2020

SAP EHS: From Procedural Code to Clean Object Oriented ABAP Part 1

Programming in the SAP EH&S Module can often be callenging due to the concept of properties and characteristis, as well as the different usages (validity areas and ratings).

The BAPIs  BAPI_BUS1077_* from the function group 1077 are still very widely used in procedural code.

Nonetheless, it is the  Object Oriented concept that supports Clean Code. 

Therefore, in order to achieve clean ABAP code for EH&S Developments we can use the class CL_EHCSM_SPC_ACCESS instead of the 1077 BAPIs.

Simple Example:

Here I am going to demonstrate a rework of a procedural function module into a clean code method.

Reading specification properties using the function module BAPI_BUS1077_GETDETAIL:

Procedural code:

SELECT matnr recnroot FROM estmj INTO CORRESPONDING FIELDS OF TABLE lt_estmj WHERE matnr iv_matnr AND delflg abap_false.

  lv_lines lineslt_estmj ).

  CASE lv_lines.
    WHEN 0.
      ev_return '1'
      RETURN.
    WHEN 1.
      READ TABLE lt_estmj INTO ls_estmj INDEX 1.
    WHEN OTHERS.
      ev_return '2'
      RETURN.
  ENDCASE.



  CLEARls_subheader.

  ls_subheader-record_no ls_estmj-recnroot.
  APPEND ls_subheader TO lt_subheader.

  ls_propheader-subchacat 'SAP_EHS_XXX_YYY'.
  APPEND ls_propheader TO lt_propheader.

  ls_propheader-subchacat 'SAP_EHS_XXX_YYY'.
  APPEND ls_propheader TO lt_propheader.

  ls_propheader-subchacat 'SAP_EHS_XXX_YYY'.
  APPEND ls_propheader TO lt_propheader.

  ls_propheader-subchacat 'SAP_EHS_XXX_YYY'.
  APPEND ls_propheader TO lt_propheader.

  ls_propheader-subchacat 'SAP_EHS_XXX_YYY'.
  APPEND ls_propheader TO lt_propheader.

  CALL FUNCTION 'BAPI_BUS1077_GETDETAIL'
    EXPORTING
      scenario         '01'
      key_date         sy-datum
      flg_header       'X'
      flg_matjoin      'X'
      flg_properties   'X'
      flg_prop_data    'X'
      flg_prop_details 'X'
    TABLES
      return           lt_return
      sub_header       lt_subheader
      matjoin          lt_matjoin
      prop_header      lt_propheader
      prop_val         lt_propval
      prop_data        lt_propdata
      prop_usage       lt_propusage.

In the procedural code we find select statements, which should be avoided.
It is very common in the EH&S Development to be trying to read the specification to a certain material.

Recommendations:

  • Avoid direct Selects


First try to find an existing function in your framework and only if nothing is available, write a select statement.

  • A BAPI is better than a direct select.  
  • The method of a class is better than a BAPI. 
In this example I recommend to capsule the often needed function get_recn_by_matnr in a method. In my method I still had to use the BAPI BAPI_BUS1077_GETLIST  but it is reusable and in case of change, I will change it once.

zcl_ehs_hazard=>get_recn_by_matnr. 



  • Use INSERT VALUE instead of APPEND
Appended lines need a defined type. The INSERT VALUE is more flexible and can be used even if some structure type gets changed.

  • DRY (DO NOT REPEAT YOURSEF)
In the above code we see that many properties were hard-coded and appended to the properties  header table. In similar cases it is worth considering to move such logic to the configuration (you can use a z-table instead of the hard code). The advantage is that when business asks for an additional property to be evaluated, you can simple do this as a customizing activity, not affecting your code.
You can see in the clean code that the logic is capsuled in the method 
zcl_ehs_hazard=>get_XYZ_vat.

  • Use ABAP OO instead of procedural code
In this example ( BAPI_BUS1077_GETDETAIL vs. CL_EHCSM_SPC_ACCESS) we can easily see the advantages of using the dedicated method retrieve_val_char_data of the class 
 CL_EHCSM_SPC_ACCESS. 

The method  retrieve_val_char_data  provides the option to filter the characteristics by usage. This means that if I want to evaluate a certain usage, for e.g. Validity Area Korea, I can filter before I get the data and don't have to worry afterwards. 

  • Prefer inline declaration always when it is possible, when not declare the variables close to their usage

With the OO Style we can use inline declarations for the import parameters (clean code)


  • Avoid hard code or the so called magic words
You can see the hard coded scenario '01' in the procedural code. Usually, you can find the constants that you need in the interface. In the clean code, for example, we use if_ehcsm_spc_const_c=>gc_lockmode-no_lock.

Not everyone knows what '01' or 'N' is but everyone will understand 'no lock'.

  • Call your variable and methods in a way that they can be pronounced
This makes your code more readable and the hand-over process more efficient.


Clean ABAP Code:


    METHOD XYZ.
    DATA(lo_accesscl_ehcsm_spc_access=>get_instance).
    DATA lt_key TYPE ehcsmt_spc_key.
    DATA lt_usage_filter TYPE ehcsmt_spc_usage_filter.
    INSERT VALUE #recn zcl_ehs_hazard=>get_recn_by_matnriv_matnr INTO TABLE lt_key .
    INSERT VALUE #rvlid iv_country vaclid zcl_ehs_hazard=>c_vaclid INTO TABLE lt_usage_filter.
    lo_access->retrieve_val_char_data(
      EXPORTING
        it_spc_header_key    lt_key
        it_vat               zcl_ehs_hazard=>get_XYZ_vat
        it_spc_usg_filter    lt_usage_filter
        iv_lock_mode         if_ehcsm_spc_const_c=>gc_lockmode-no_lock
       IMPORTING
         et_spc_val_char_data DATA(lt_spc_char_data)
         et_message           DATA(lt_message)
         ev_severity          DATA(lv_severity)
    ).





The most important is to use your framework and what it offers because it is tested!
Good luck!