ERPClimb logoERPClimb

Code examples · Modern ABAP · Beginner

Write your own function module with an exception and call it (also from another system with RFC)

The problem

Several programs need the customer name for a customer number.

How we solve it

  1. 1Interfacein SE37 tabs
  2. 2BodySELECT, RAISE
  3. 3Call itlocal and RFC

The result

The caller gets the name, or a clear 'not found' message.

Overview drawn by ERPClimb from this example. The steps are the same as in the code below.

1. The requirement

Several programs need the customer name for a customer number. Put the logic in one reusable function module that fails in a clear way when the customer is unknown, and make it callable from other systems.

2. How we design it

A function module lives in a function group and is created in transaction SE37. Its interface (importing, exporting, exceptions) is maintained in tabs, not in the source. The body reads the name and raises an exception if nothing is found, so the caller decides what to do. Switching on the attribute Remote-Enabled Module turns the same module into an RFC function that other systems can call with CALL FUNCTION ... DESTINATION.

Why we decided it this way

  1. 1An exception instead of an empty resultAn empty name could also be a real blank name. The exception NOT_FOUND tells the caller clearly that the customer does not exist.
  2. 2Pass by valueRFC modules must pass parameters by value, and it also protects the caller's variable from side effects.
  3. 3Convert the number firstKNA1 stores customer numbers with leading zeros. The ALPHA conversion lets users type 1000 and still find 0000001000.

Before you start

  • A function group, for example ZCUST_UTIL, created in SE80 or SE37.
  • Customers in table KNA1 (look at them in SE16N).

3. The code, step by step

Step 1 of 3: Define the interface in SE37

The parameters and the exception are declared.

ABAP · SE37 · Z_GET_CUSTOMER_NAME (Import, Export, Exceptions tabs)
" What the interface tabs contain
FUNCTION z_get_customer_name.
*"----------------------------------------------------------------------
*"  IMPORTING
*"     VALUE(IV_KUNNR) TYPE  KUNNR
*"  EXPORTING
*"     VALUE(EV_NAME) TYPE  NAME1_GP
*"  EXCEPTIONS
*"      NOT_FOUND
*"----------------------------------------------------------------------
Part by part (plain text)
  1. What the interface tabs contain. In SE37 create Z_GET_CUSTOMER_NAME in your function group. Import: IV_KUNNR TYPE KUNNR, pass by value. Export: EV_NAME TYPE NAME1_GP, pass by value. Exceptions: NOT_FOUND. The comment block below is what SAP writes into the source for you; you do not type it.

Open in the portal

The SAP objects named in this code are linked in the code above and listed here. Each opens its page in the SAP Library.

SAP tables

Transaction codes

4. The final outcome

The caller gets the name, or a clear 'not found' message.

What you should see

Customer 1000: Example Customer Inc. Customer 99999999: not found

Try it and check the result

  • In SE37 press F8 on the module, enter customer 1000 and run: the name is returned. Enter 99999999: the exception NOT_FOUND is shown.
  • Run the report ZR_CALL_CUSTOMER_NAME (the local part) and compare it with SE16N.
  • For RFC, create a destination in SM59 that points to the same system first (type 3, with a user), and use its name.

Common mistakes

  • Leaving out EXCEPTIONS makes every raised exception a dump. Always list them, with OTHERS as the last entry.
  • RFC modules cannot have parameters passed by reference, and the interface should use flat or simple types. Check the attribute and the interface tab for errors on activation.
  • A function module is global: renaming it later means changing every caller. For new development, consider a method in a class; function modules stay necessary for RFC and for many SAP standard interfaces.

Keep learning

Checking status: Reviewed line by line against SAP documentation. Not executed on a live system. Reviewed 2026-11-26. Release: Any ABAP system with ADT (S/4HANA, ECC 7.40 or newer, or a trial system). Classic techniques, not ABAP Cloud.

Original ERPClimb teaching code with illustrative names. Not taken from a customer system. Syntax can differ by release; check the references below and activate in a sandbox first. ERPClimb is independent of SAP SE.