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!



Friday, April 24, 2020

SAP: Error Analysis in Output Processing, example Sales&Distribution (VL02N)

Having worked for customers in the logstics branche, I often had to analyze the output processing in the Sales&Distribution Module

Here I would like to share some error examples and the approach to them.


  • Wrong Data in the Output
The output processing is executed successfully but the data is not correct:
In order to debug cases with wrong output data, it is helpful to check transaction NACE.
There we go to the output type V2 (Shipping) and navigate to our output type.
We can check the processing routines:


By placing a break-point in the Form Routine, we can easily debug the data retrieval.

  • Critical errors 
Thee output processing is not executed, a red error message is there in the log:
In this case the partner agreements are not maintained correctly. A customizing activuty needs to be done in transaction WE20. Then we can reexecute the output.



Some error messages in the output processing are nonthreatening but they appear and can be confusing for the end users.

  • Minor errors
For example, in deliveries (transaction VL02N) ,after processing the output routines, the paper is printed correctly or the IDoc is sent but when you go to the log you find errors like missing translations or missing phrases / phrase sets.

Let's look at one example where we get the error  message DG523:


The message is almost self-explanatory and it is clear there is master EH&S Data missing.
After maintaining the missing phrase set assignment, we reprocess and the log is green.
In case of missing master data, do not forget to maintain in the production system.






SAP EHS: Getting started with Mixed Loading Checks





Many SAP Customers execute manual mixed loading checks, which is time consuming and error prone.

To automate this process, they should provide customizing for mixed loading rules, which is regulation specific and can be switched on and off per sales organization. If the mixed loading rules are violated, a delivery split should take place.

In the SAP standard system the danger label is used as the segregation key.

There is a Sales Organization specific Mixed Loading Rule Activation.
The mixed loading checks in SD are executed by the Standard Function Module DG63_CHK_MLOAD which should be customizied as a dangerous goods check method together with the function module DG63_CHK_INIT_HEADER.
For the mixed loading checks a dangerous goods check schema (matrix) can be defined.

It contains the mixed loading checks and can be assigned to a particular sales organisation and delivery type.

After maintaining the Customizing for Segregation Rules, one example can be created at Sales Order Level.

The delivery cannot be saved because the mixed loading rules are violated.
The next step is to have a delivery split in this case.






Thursday, April 16, 2020

SAP EHS: Dangerous Goods Data for Sales and Distribution Documents

In my experience I have seen many SAP Customers struggling with the output of Dangerous Goods (DG) Data on Sales and Distribution(SD) Documents, such as printouts or IDocs.

The Standard SAP Solution for this topic is quite simple and straightforward. The Problem is that it has the prerequisite of the EH&S Master Data Data being available on the system.

Many customers, who recently introduced the EH&S module, have already implemented custom solutions based on custom data (the z-table solution).

When trying to show DG Data on SD Documents, you have to make sure following conditions are met on the system:

Master Data:

  • Material Master Data (TCode MM03), Dangerous Goods Indicator  MARA-PROFL should be set

  • Dangerous Goods Mater (TCode DGP3):, There should be a valid DG Record for the document date of the SD Document, the validity area and the mode of transport dtermined by the route in the delivery.
e.g. The delivery is sent from Germany to Slovenia by road - the system finds the regulation ADR
(This Customzing can be found in Transaction 




If there is no record in the DGP3 Transaction, it can be filled through transaction DGE5 in a system with EH&S master data available. Otherwise it can be maintained manually in transaction DGP1.
  • SD Delivery Document: The route should be maintained in the delivery document
Customizing Activities:

  • Activate Dangerous Goods Checks
  • Specify Dangerous Goods Check Methods
  • Specify Usage Profiles for Dangerous Goods Check Methods
  • Assign Dangerous Goods Check Schemas
Once you have a Dangerous Goods schema assigned to your sales organization, you can execute the dangerous goods in Sales&Distribution, for example in TCode VL02N.



You can either execute the DG Checks on the SD Document or execute the output procedure for the relevant paper or IDoc.

There are customers who have their special master data maintenance. For example, some customers don't maintain a DG indicator (MARA-PROFL) on material master lever, the field for this is simply empty. 

They should be advised to update the master data and make sure that newly created data contains it as well.




Other customers have to provide the DG Data to third party interface, such as transport companies or systems who take care of the SD orders.

A classic problem is the route not being maintained on the SD Document. This results in the DG Data not being retrieved, since each and every standard module from the already mentioned DG Check methods 'gives up' if there is no route.

Some companies will have their route determination customized and automatically filled. Others will be maintaining it manually. 

The SAP Standard doesn't throw a message if the route is not maintained, it just leaves the DG Data empty. 

This functionality can fortunately be enhanced. For example, a custom function can be created to show an error message or write an error in the application log if critical data for the DG determination is not available.

Following enhancements can be implemented:

  • Develop Enhancement for Material Exchange Multi-Component Explosion
  • Develop Enh. for Determ. Country/MTC Combination in Shipping/Sales
  • Develop Enh. for Determ. Country/MTC Combination in Shipment Document
  • Develop Enhancement for Country/Mode-of-Transport Category Combination
  • Develop Enhancement for Editing DG Data in Shipping Document
  • Develop Enhancement for Editing DG Data in Sales Document
You can check my post:
SAP: Implement a User Exit (Customer Exit) but How?
 to see how to implement a user exit.








Monday, April 13, 2020

SAP: Implement a User Exit (Customer Exit) but How?



This is a step by step tutorial that might be useful for a beginners.

Let's suppose you find a suitable customizing activity and it sounds like 'Develop Enhancement for...'
Then you click on the node and what you find is the name of an enhancement, for e.g. DG700004.

  • Create an enhancement project

Then you go to transaction CMOD (Enhancements) and create a project in your customer name space
(Z*).



  • Create the enhancement assignment

You open the project and go to enhancement assigments. There you can use the previously found enhancement spot DG700004.




  • Place your implementation in the enhancement component

In the enhancement components you will find the function exit. It is a function module. EXIT_SAPLDG70_004 in our example.



In the function module there is an include ZX....
You can double click it and after a passing a name space warning, you can type your code.

  • Activate the enhancement project

Don't forget to activate your enhancement project.
Place a break-poin and check what happens.

A break-point in the Function MODX_FUNCTION_ACTIVE_CHECK
might help you get to the pont where the enhancement is called, in our case it will be function '004'.