# BAPI_ALM_ORDER_MAINTAIN Example: Create a SAP PM Order in ABAP

> Source: https://netray.co/tools/sap-pm-order-create-bapi-abap-skeleton
> Publisher: Netray (netray.co), AI and integration engineering for ERP and manufacturing systems.
> Section: Free Tools

## Short answer

Fill in order type, plant and equipment and get a working-shape ABAP skeleton that creates a maintenance order through BAPI_ALM_ORDER_MAINTAIN.

## Category

sap

## Erp

SAP ECC / S/4HANA

## Audience

- ABAP developer
- SAP PM consultant
- Integration developer

## Tool

**Kind:** template

**Output:** code

**Language:** abap

**Fields:**

#### Order type

**Key:** order_type

**Type:** text

**Default:** PM01

#### Planning plant

**Key:** plant

**Type:** text

**Default:** 1000

#### Planner group

**Key:** planner_group

**Type:** text

**Default:** 001

#### Equipment number

**Key:** equipment

**Type:** text

**Default:** 10000123

#### Order short text

**Key:** short_text

**Type:** text

**Default:** Replace drive belt on conveyor 4

#### Main work center

**Key:** work_center

**Type:** text

**Default:** MECH01

#### Operation control key

**Key:** control_key

**Type:** text

**Default:** PM01

**Blocks:**

#### Create order, operation, save and commit

**Body:** DATA: lt_methods  TYPE STANDARD TABLE OF bapi_alm_order_method,
      ls_method   TYPE bapi_alm_order_method,
      lt_header   TYPE STANDARD TABLE OF bapi_alm_order_headers_i,
      ls_header   TYPE bapi_alm_order_headers_i,
      lt_header_up TYPE STANDARD TABLE OF bapi_alm_order_headers_up,
      ls_header_up TYPE bapi_alm_order_headers_up,
      lt_operation TYPE STANDARD TABLE OF bapi_alm_order_operation,
      ls_operation TYPE bapi_alm_order_operation,
      lt_operation_up TYPE STANDARD TABLE OF bapi_alm_order_operation_up,
      ls_operation_up TYPE bapi_alm_order_operation_up,
      lt_numbers  TYPE STANDARD TABLE OF bapi_alm_numbers,
      lt_return   TYPE STANDARD TABLE OF bapiret2.

* 1. Header with a temporary order number
ls_header-orderid    = '%00000000001'.
ls_header-order_type = '{{order_type}}'.
ls_header-planplant  = '{{plant}}'.
ls_header-plangroup  = '{{planner_group}}'.
ls_header-equipment  = '{{equipment}}'.
ls_header-short_text = '{{short_text}}'.
APPEND ls_header TO lt_header.

ls_header_up-order_type = abap_true.
ls_header_up-planplant  = abap_true.
ls_header_up-plangroup  = abap_true.
ls_header_up-equipment  = abap_true.
ls_header_up-short_text = abap_true.
APPEND ls_header_up TO lt_header_up.

* 2. One operation
ls_operation-activity    = '0010'.
ls_operation-control_key = '{{control_key}}'.
ls_operation-work_cntr   = '{{work_center}}'.
ls_operation-plant       = '{{plant}}'.
ls_operation-description = '{{short_text}}'.
APPEND ls_operation TO lt_operation.

ls_operation_up-activity    = abap_true.
ls_operation_up-control_key = abap_true.
ls_operation_up-work_cntr   = abap_true.
ls_operation_up-plant       = abap_true.
ls_operation_up-description = abap_true.
APPEND ls_operation_up TO lt_operation_up.

* 3. Methods: create header, create operation, save
ls_method-refnumber  = '000001'.
ls_method-objecttype = 'HEADER'.
ls_method-method     = 'CREATE'.
ls_method-objectkey  = '%00000000001'.
APPEND ls_method TO lt_methods.

CLEAR ls_method.
ls_method-refnumber  = '000001'.
ls_method-objecttype = 'OPERATION'.
ls_method-method     = 'CREATE'.
ls_method-objectkey  = '%000000000010010'.
APPEND ls_method TO lt_methods.

CLEAR ls_method.
ls_method-refnumber  = '000001'.
ls_method-method     = 'SAVE'.
ls_method-objectkey  = '%00000000001'.
APPEND ls_method TO lt_methods.

CALL FUNCTION 'BAPI_ALM_ORDER_MAINTAIN'
  TABLES
    it_methods      = lt_methods
    it_header       = lt_header
    it_header_up    = lt_header_up
    it_operation    = lt_operation
    it_operation_up = lt_operation_up
    return          = lt_return
    et_numbers      = lt_numbers.

IF line_exists( lt_return[ type = 'E' ] ) OR line_exists( lt_return[ type = 'A' ] ).
  CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
  LOOP AT lt_return INTO DATA(ls_ret) WHERE type = 'E' OR type = 'A'.
    WRITE: / ls_ret-message.
  ENDLOOP.
ELSE.
  CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
    EXPORTING
      wait = abap_true.
  READ TABLE lt_numbers INTO DATA(ls_num) INDEX 1.
  WRITE: / 'Order created:', ls_num-aufnr_new.
ENDIF.

**Notes:** This is a skeleton showing the usual call pattern. Verify the structure field names (BAPI_ALM_ORDER_HEADERS_I, BAPI_ALM_ORDER_OPERATION and the _UP flag structures) in SE11 for your release, and confirm the format of the objectkey for operations. Order type, work center and control key must exist in your customizing. Always test in a sandbox and never run it with a COMMIT in a test client you do not own.

## Content

**Intro:** Integrations, mobile apps and migration programs often need to create PM orders without a person in IW31. BAPI_ALM_ORDER_MAINTAIN is the standard function module for that, and this generator gives you the call pattern.

**Sections:**

#### How to use the skeleton

**Body:** Fill in the order type, planning plant, planner group, equipment, short text, work center and control key from your own customizing, then copy the code into a test report. The pattern has five parts: fill the header and its update flags, fill the operation and its flags, build the method table, call the BAPI, then check the return table and commit. Run it first in a development client with a known equipment number.

#### How the BAPI call works

**Body:** BAPI_ALM_ORDER_MAINTAIN is driven by a method table. Each row names an object type such as HEADER or OPERATION, a method such as CREATE, and an object key. A new order uses a temporary key beginning with a percent sign, and SAVE at the end tells SAP to persist the data. The matching _UP tables carry X flags for the fields you filled, because the BAPI only changes fields that are flagged. After the call, ET_NUMBERS maps the temporary number to the real order number.

**Bullets:**

- Temporary order number starts with %
- Every changed field needs its flag in the _UP table
- COMMIT only if RETURN holds no E or A messages

#### Common mistakes

**Body:** Developers often forget the flag structures, so the BAPI silently ignores their fields. Another frequent error is calling BAPI_TRANSACTION_COMMIT even after an error message, leaving a half-built order. Check also that the user running the program has authorization for the order type and plant, and that mandatory fields from the order type customizing such as the main work center are filled. For mass creation, call the BAPI in batches and log each return table.

**Cta:** If you need an agent or integration to raise orders safely, we build it with human approval before any write-back to SAP.

**Faq:**

#### How do I create a PM order from ABAP?

**Answer:** Use BAPI_ALM_ORDER_MAINTAIN with a method table that has HEADER CREATE, optional OPERATION CREATE rows and a final SAVE, plus header and update-flag tables. Check the RETURN table for errors, then call BAPI_TRANSACTION_COMMIT.

#### Why was my order not saved?

**Answer:** Usually the SAVE method is missing from the method table, or a COMMIT was not called, or an error message in RETURN was ignored. Also check that the update flags are set for each field. Read the return messages line by line.

#### How do I get the new order number?

**Answer:** After a successful call, the ET_NUMBERS table returns the new order number in the field AUFNR_NEW for the temporary number you passed. Read it before or immediately after the commit and store it in your integration log.

#### Is there an alternative for notifications?

**Answer:** Yes. BAPI_ALMNOTIF_CREATE creates PM notifications and BAPI_ALMNOTIF_SAVE persists them. For order changes after creation, use the same order BAPI with CHANGE methods. For modern landscapes, also look at OData services released for maintenance orders.

## Search terms

bapi_alm_order_maintain example, create pm order bapi, bapi alm order maintain abap code, sap pm order create program, bapi_alm_order_headers_i, sap maintenance order bapi, bapi transaction commit order, sap pm integration order creation

---
Talk to an engineer: https://netray.co/contact?intent=erp_ai | Machine catalog: https://netray.co/agent/catalog.json
