Tuesday 27 August 2024

Automating Username Generation for Employee BP Records mandatory in SAP EHS

In the context of SAP EHS (Environment, Health, and Safety), ensuring that all employees have a unique Business Partner (BP) record is crucial for effective management and compliance. Each employee must have a username to be assigned as part of their BP data, which is essential for proper system integration and user access.

This guide demonstrates how to create an ABAP program that automatically generates usernames for employees. The usernames are derived from the first letter of the employee’s first name and their last name, followed by a numeric suffix to ensure uniqueness if similar usernames already exist. This is particularly useful in the SAP EHS module, where each employee's BP record requires a unique identifier.

1. Access the ABAP Editor (SE38)


◉ Transaction Code: SE38

◉ Open the SAP GUI and enter the transaction code SE38 in the command field.

Automating Username Generation for Employee BP Records mandatory in SAP EHS

2. Create a New Program


◉ In the ABAP Editor: Initial Screen, enter a name for your program (e.g., ZUPDATE_SYNAME) in the Program field.
◉ Click the Create button.

Automating Username Generation for Employee BP Records mandatory in SAP EHS

3. Define Program Attributes


◉ Title: Enter a meaningful title for your report (e.g., "Update SYNAME for Active Employees").
◉ Type: Choose Executable Program since this is a standalone report that will be executed directly.
◉ Status: Typically, this would be set to Test Program during development and Production Program when finalized.
◉ Application: Select the appropriate application area, such as HR if the program is related to human resources.

Automating Username Generation for Employee BP Records mandatory in SAP EHS

4. Write the ABAP Code


◉ In the ABAP Editor, you can now write or paste the code provided for your report.

Automating Username Generation for Employee BP Records mandatory in SAP EHS

The ABAP Code : 

*&---------------------------------------------------------------------*
*& Report ZUPDATE_SYNAME
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ZUPDATE_SYNAME.

DATA: lt_employees TYPE TABLE OF pa0002,
      ls_employee  TYPE pa0002.

DATA: lt_pa0001 TYPE TABLE OF pa0001,
      ls_pa0001 TYPE pa0001.

DATA: lt_info_0105 TYPE TABLE OF pa0105,
      ls_info_0105 TYPE pa0105.

DATA: lv_counter TYPE string,
      lv_suffix  TYPE string,
      lv_base_uname TYPE string,
      lv_final_uname TYPE string,
      existing_uname TYPE pa0105-uname.

* Check the current date
WRITE: / 'Current date:', sy-datum.

* Select PERNR from PA0001 where ENDDA is greater than or equal to today
SELECT * FROM pa0001 INTO TABLE lt_pa0001
  WHERE endda >= sy-datum.

* Check how many records were selected
DATA(lv_count) = lines( lt_pa0001 ).
WRITE: / 'Number of records found:', lv_count.

* Now, use the results from lt_pa0001 to select from PA0002
IF lt_pa0001 IS NOT INITIAL.
  SELECT * FROM pa0002 INTO TABLE lt_employees
    FOR ALL ENTRIES IN lt_pa0001
    WHERE pernr = lt_pa0001-pernr.
ENDIF.

* Check if LT_EMPLOYEES has records
IF sy-subrc = 0 AND lt_employees IS NOT INITIAL.
  LOOP AT lt_employees INTO ls_employee.

    CLEAR: lt_info_0105, lv_counter, lv_base_uname, lv_final_uname.

    SELECT * FROM pa0105 INTO TABLE lt_info_0105
      WHERE pernr = ls_employee-pernr
      AND endda >= sy-datum.

    IF lt_info_0105 IS INITIAL.

      lv_base_uname = ls_employee-vorna(1) && ls_employee-nachn.

      lv_counter = 1.

      WHILE lv_counter < 1000.

        CONCATENATE lv_base_uname lv_counter INTO lv_final_uname.

        SELECT SINGLE uname FROM pa0105
          INTO @existing_uname
          WHERE uname = _final_uname.

        IF sy-subrc <> 0.
          EXIT.
        ENDIF.

        lv_counter = lv_counter + 1.

      ENDWHILE.

      CLEAR ls_info_0105.
      ls_info_0105-pernr = ls_employee-pernr.
      ls_info_0105-endda = '99991231'. " Set to the max date
      ls_info_0105-begda = sy-datum.
      ls_info_0105-subty = '0001'.
      ls_info_0105-aedtm = sy-datum.
      ls_info_0105-uname = SY-uname.
      ls_info_0105-usrty = '0001'.
      ls_info_0105-usrid = lv_final_uname.

      INSERT pa0105 FROM ls_info_0105.

      WRITE: / 'Created new UNAME for Employee ID:', ls_employee-pernr,
             'UNAME:', ls_info_0105-uname.

    ELSE.

      LOOP AT lt_info_0105 INTO ls_info_0105.

        IF ls_info_0105-uname IS INITIAL.

          lv_base_uname = ls_employee-vorna(1) && ls_employee-nachn.

          lv_counter = 1.

          WHILE lv_counter < 1000.

            CONCATENATE lv_base_uname lv_counter INTO lv_final_uname.

            SELECT SINGLE uname FROM pa0105
              INTO @existing_uname
              WHERE uname = _final_uname.

            IF sy-subrc <> 0.
              EXIT.
            ENDIF.

            lv_counter = lv_counter + 1.

          ENDWHILE.

          ls_info_0105-uname = lv_final_uname.
          MODIFY pa0105 FROM ls_info_0105.

          WRITE: / 'Updated UNAME for Employee ID:', ls_employee-pernr,
                 'New UNAME:', ls_info_0105-uname.

        ENDIF.

      ENDLOOP.

    ENDIF.

  ENDLOOP.

ELSE.
  WRITE: / 'No employee records found for the given PERNRs.'.
ENDIF.

* Display message or log completion
WRITE: / 'UNAME update completed.'.
 

5. Save the Program


◉ Click Save (Ctrl+S) to save your program.
◉ You will be prompted to choose a package. If this is a test or temporary program, you can save it in the $TMP package, which is for local objects that don't need to be transported. Otherwise, choose an appropriate package.
◉ Transport Request: If you’re saving it in a package that requires transport, you’ll need to assign it to an existing transport request or create a new one.

6. Check and Activate the Program


◉ Check: Click the Check button (Ctrl+F2) to ensure there are no syntax errors in your program.
◉ Activate: Once the program is error-free, click the Activate button (Ctrl+F3). Activation makes the program executable in the system.

7. Execute the Program


◉ After activation, you can execute the program directly from SE38 by clicking the Execute button (F8).
◉ You may also run it by entering the program name in the command field from the SAP Easy Access screen.

Automating Username Generation for Employee BP Records mandatory in SAP EHS

Automating Username Generation for Employee BP Records mandatory in SAP EHS

No comments:

Post a Comment