SAP Note 3047487 BAdI Examples

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

CONFIDENTIAL: AUTHORIZED FOR SAP PILOT CUSTOMERS OF SAP NOTE 3047487

Attachment of SAP Note 3047487


Example of codes for Enhancement Spot HRPAYCL_LRE
TABLE OF CONTENTS
IMPORTANT REMARKS ............................................................................................................................. 3
1. ELECTRONIC REMUNERATION BOOK BADIS ..................................................................................... 3
1.1 Accessing BAdIs for the Electronic Remuneration Book .................................................................. 3
1.2 Creating a BAdI implementation ......................................................................................................... 3
1.3 Managing a BAdI implementation ....................................................................................................... 3
1.4 BAdI examples ..................................................................................................................................... 3
1.4.1 HRPAYCL_B_LRE_FILL_PRE_TOTAL ............................................................................................. 3
1.4.1.1 Creating a specific field using Master Data information ..................................................................... 4
1.4.1.2 Modifying an existing field using payroll information .......................................................................... 5
1.4.1.3 Creating a new field with a custom formatter..................................................................................... 5
1.4.2 HRPAYCL_B_LRE_FILL_FINAL ........................................................................................................ 7

2
This document has the purpose of centralizing useful information for BAdI development of Electronic
Remuneration Book (RPC_PAYCL_LRE_DRIVER) report.

To know more about general functions of BAdI development, see the following SAP documentation:

• What is a BAdI?

• Building your first BAdI

• How to implement a BAdI

The main goal is to provide real examples of BAdI implementations as to assist on customer development.

IMPORTANT REMARKS
• The following source code snippets are just examples and provide very basic examples on how to
address common scenarios within the available BAdIs. Only basic error-handling is defined in the
snippets, so the same must be adjusted by customers upon using, according to their business needs.
• The information provided below provides examples that are intended to be self-explanatory. Questions
regarding the use of these examples are deemed consultancy and should not be addressed through SAP
Support Portal.
• The use of the information below is entirely optional and SAP is not responsible for issues that may arise
from it. User discretion is advised.

1. ELECTRONIC REMUNERATION BOOK BADIS

1.1 Accessing BAdIs for the Electronic Remuneration Book


1. Go to transaction SE18
2. Type in HRPAYCL_LRE under the Enhancement Spot section
3. Hit Display
The enhancement has the two available BAdIs for Electronic Remuneration Book grouped together. In here,
you can create new implementations or manage the existing ones.

1.2 Creating a BAdI implementation


1. Expand the name of the BAdI. For this example, expand the BAdI HRPAYCL_B_LRE_FILL_FINAL.
2. Right-click the node Implementations and select Create BAdI Implementation.
3. Follow the wizard for new implementation creation.

1.3 Managing a BAdI implementation


By default, new BAdIs are not called until activated.

1.4 BAdI examples


There are two BAdIs available in the Electronic Remuneration Book report:
• HRPAYCL_B_LRE_FILL_PRE_TOTAL
• HRPAYCL_B_LRE_FILL_FINAL

1.4.1 HRPAYCL_B_LRE_FILL_PRE_TOTAL

The HRPAYCL_B_LRE_FILL_PRE_TOTAL BAdI is executed for each run of the report for an employee
before calculating the totals for all previous fields. This means that at the point that this BAdI is executed, all
the non-total fields should be filled with the employee information and can be modified or used in the BAdI.
1.4.1.1 Creating a specific field using Master Data information
The example below shows how to use the BAdI to create a new field for the employee.

METHOD if_hrpaycl_b_lre_pre_total~fill_pre_total.
DATA lo_new_field TYPE REF TO if_hrpaycl_lre_field. "This is the necessary
type to create a field
DATA lo_p0185 TYPE REF TO if_hrpadcl_infotype.
DATA ls_p0185 TYPE p0185.
DATA lv_begda TYPE begda.
DATA lv_endda TYPE endda.

* First, we can acquire the information that was selected at the report
screen utilizing the
* ms_config_date attribute from the io_run_configuration object.
lv_begda = io_run_configuration->ms_config-date_range-begda.
lv_endda = io_run_configuration->ms_config-date_range-endda.
* Next, we can use the io_master_date object, which contains the master data
information for
* the current employee to acquire the infotype 0185 in the selected time-
frame
* In this example, we will use the subtype 02 ( Passport) of the 0185
infotype to use as RUT.
TRY.
lo_p0185 = io_master_data->get_last( iv_infty = '0185'
iv_subty = '02'
iv_begda = lv_begda
iv_endda = lv_endda ).
lo_p0185->get_infotype( IMPORTING es_infty = ls_p0185 ).
CATCH cx_hrpadcl_infty_not_found.
RETURN.
ENDTRY.

* Now we must first instantiate a field using the


cl_hrpaycl_lre_field_assembler class.
* The make method receives the value of the field and its code, in this case
we are using the
* RUT code, but you can use create a field using any code.
* As for the iv_value parameter, it can receive any type of data and it will
automatically identify it
* and create the field accordingly
lo_new_field = cl_hrpaycl_lre_field_assembler=>make(
iv_code = '1101'
iv_value = ls_p0185-icnum ).
* And finally, for this field to be added in the list of fields for the
employee, we must use the
* io_field_collection object and add the new field to it.
io_field_collection->add( lo_new_field ).
* With this done, the field created in this BAdI is effectivly created for
to the employee
ENDMETHOD.

Table 1: Example code for method hrpaycl_b_lre_fill_pre_total


1.4.1.2 Modifying an existing field using payroll information
The example below shows a scenario where the “Pago indemnización a todo evento” (2331) field is modified
by using information taken from the employee’s RT.

METHOD if_hrpaycl_b_lre_pre_total~fill_pre_total.
DATA lo_new_field TYPE REF TO if_hrpaycl_lre_field.
DATA lv_endda TYPE endda.
DATA lv_amount TYPE betrg.
DATA lr_data_reference TYPE REF TO data. "Necessary type in order to
access a table
DATA lt_rt TYPE hrpay99_rt.
DATA ls_wage_type TYPE pc207.
* We start by cetting the end date information from the ms_config attribute
of the run configuration of the report
lv_endda = io_run_configuration->ms_config-date_range-endda.
* Now we will use the io_payroll_result object, which you can use to acess
all the payroll information from the employee
* In this example we are acquiring the reference to a table using the
get_table method so we can access the employee's RT
lr_data_reference = io_payroll_result->get_table( 'RT' ).
* As we have fetched the reference to the RT, we must copy it to an
internal table in order to use it
lt_rt = lr_data_reference->*.
* Next we will loop at the RT and we will seek for the /103 wage-type
LOOP AT lt_rt INTO ls_wage_type
WHERE lgart = '/103'.
lv_amount = ls_wage_type-betrg * '0.15'.
ENDLOOP.
* After that, we must create the new field in order to insert it
lo_new_field = cl_hrpaycl_lre_field_assembler=>make(
iv_code = '2331'
iv_value = lv_amount ).
* But as we are modifying an existant field, we first must delete the old
entry, using the remove method of the io_field_collection object.
io_field_collection->remove( '2331' ).
* After that, we can sucessfuly add the field created in this BAdI in the
field_collection.
io_field_collection->add( lo_new_field ).
* With this done, the field created in this BAdI is effectivly created for
to the employee
ENDMETHOD.

Table 2: Example code for method hrpaycl_b_lre_fill_pre_total

The end result with this modification should be:

Figure 1: Example of end result for method hrpaycl_b_lre_fill_pre_total

1.4.1.3 Creating a new field with a custom formatter


The example below shows a scenario where the “Tasa indemnización a todo evento” (1132) field is created.
This field is required to have the decimals values. By standard, if you create a field passing an amount, it will
be rounded by the default formatter. In this case, you must create a customer formatter by creating an
implementation of if_hrpaycl_lre_field_formatter interface. For this, you must follow the below steps:
1. Go to transaction SE24.
2. Type in the name of your BAdI implementation.
3. Select in Local Definitions/Implementations
4. Here you can create your local classes
Below is an example of a custom formatter that can be used to have decimals in the end result:

CLASS lcl_no_rounding_formatter DEFINITION FINAL.


PUBLIC SECTION.
INTERFACES if_hrpaycl_lre_field_formatter.
ENDCLASS.

CLASS lcl_no_rounding_formatter IMPLEMENTATION.


METHOD if_hrpaycl_lre_field_formatter~format.

DATA lv_value TYPE string.


DATA lv_offset TYPE i.

MOVE iv_value TO lv_value.

IF lv_value < '0.00'.


rv_result = '0.00'.
ENDIF.
* By default, a number will have a . as the standard delimiter. We will be
replacing it by a comma
REPLACE '.' WITH ',' INTO lv_value.

rv_result = lv_value.
ENDMETHOD.
ENDCLASS.
Table 3: Example code for if_hrpaycl_b_lre_pre_total implementation

The example below shows how to use your custom formatter in the BAdI implementation:

METHOD if_hrpaycl_b_lre_pre_total~fill_pre_total.
DATA lo_new_field TYPE REF TO if_hrpaycl_lre_field.
DATA lv_endda TYPE endda.
DATA lv_amount TYPE betrg.
DATA lr_data_reference TYPE REF TO data.
DATA lt_rt TYPE hrpay99_rt.
DATA ls_wage_type TYPE pc207.
DATA lo_formatter TYPE if_hrpaycl_lre_field_formatter. "Necessary
type to create a formatter

* In this example we are once again using an information from the employee's
RT, but in this case we
* want to calculate a field that must contain the decimals value.
lv_endda = io_run_configuration->ms_config-date_range-endda.
lr_data_reference = io_payroll_result->get_table( 'RT' ).
lt_rt = lr_data_reference->*.
LOOP AT lt_rt INTO ls_wage_type
WHERE lgart = '/103'.
* We are using an arbitrary value only to generate a value with decmilas
lv_amount = ls_wage_type-betrg * '0.0738'.
ENDLOOP.
* Now we must instantiate the formatter using the custom formatter class we
created
CREATE OBJECT lo_formatter TYPE lcl_no_rounding_formatter.
* After that, we must use the make_with_custom_format method from the
assembler so we can use the custom
* formatter.
lo_new_field = cl_hrpaycl_lre_field_assembler=>make_with_custom_format(
iv_code = '1132'
iv_value = lv_amount
io_formatter = lo_formatter ).
io_field_collection->add( lo_new_field ).
* With this done, the field created will have the custom field formatter.
ENDMETHOD.
Table 4: Example code for if_hrpaycl_b_lre_pre_total implementation

The expected result for the “Tasa indemnización a todo evento” is as below:

Figure 2: Example of end result for method hrpaycl_b_lre_fill_pre_total

1.4.2 HRPAYCL_B_LRE_FILL_FINAL
The HRPAYCL_B_LRE_FILL_FINAL BAdI is executed for each run of the report for an employee after
calculating the totals for all fields. This means that at the point that this BAdI is executed, you have access to
all generated fields and all of them can be modified or used in the BAdI. Its functionality is the same as the
HRPAYCL_B_LRE_FILL_PRE_TOTAL BAdI, and the examples provided above will also work in this BAdI.
www.sap.com/contactsap

© 2021 SAP SE or an SAP affiliate company. All rights reserved.


No part of this publication may be reproduced or transmitted in any form or for any purpose without the express permission of SAP SE or an SAP affiliate company.

The information contained herein may be changed without prior notice. Some software products marketed by SAP SE and its distributors contain proprietary software components of other software vendors.
National product specifications may vary.

These materials are provided by SAP SE or an SAP affiliate company for informational purposes only, without representation or warranty of any kind, and SAP or its affiliated companies shall not be liable
for errors or omissions with respect to the materials. The only warranties for SAP or SAP affiliate company products and services are those that are set forth in the express warranty statements
accompanying such products and services, if any. Nothing herein should be construed as constituting an additional warranty.

In particular, SAP SE or its affiliated companies have no obligation to pursue any course of business outlined in this document or any related presentation, or to develop or release any functionality
mentioned therein. This document, or any related presentation, and SAP SE’s or its affiliated companies’ strategy and possible future developments, products, and/or plat form directions and functionality are
all subject to change and may be changed by SAP SE or its affiliated companies at any time for any reason without notice. The information in this document is not a commitment, promise, or legal obligation
to deliver any material, code, or functionality. All forward-looking statements are subject to various risks and uncertainties that could cause actual results to differ materially from expectations. Readers are
cautioned not to place undue reliance on these forward-looking statements, and they should not be relied upon in making purchasing decisions.

SAP and other SAP products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of SAP SE (or an SAP affiliate company) in Germany and other
countries. All other product and service names mentioned are the trademarks of their respective companies. See www.sap.com/trademark for additional trademark information and notices.

You might also like