SAPFree generatorPDF, Excel, Word exportSAP ECC / S/4HANA

SAP PM Open Maintenance Orders ABAP SELECT Generator

A list of open maintenance orders is one of the most requested custom PM reports. This generator writes the ABAP SELECT that joins AUFK and AFIH and excludes technically completed and closed orders using JEST.

Fill in

Generated code
Download:

Open maintenance orders

REPORT ZPM_OPEN_ORDERS.

TYPES: BEGIN OF ty_order,
         aufnr TYPE aufk-aufnr,
         auart TYPE aufk-auart,
         ktext TYPE aufk-ktext,
         erdat TYPE aufk-erdat,
         equnr TYPE afih-equnr,
         tplnr TYPE afih-tplnr,
         ingrp TYPE afih-ingrp,
       END OF ty_order.

DATA lt_orders TYPE STANDARD TABLE OF ty_order WITH EMPTY KEY.
DATA lv_werks  TYPE aufk-werks VALUE '1000'.
DATA lv_auart  TYPE aufk-auart VALUE 'PM01'.
DATA lv_ingrp  TYPE afih-ingrp VALUE '001'.

SELECT k~aufnr, k~auart, k~ktext, k~erdat, h~equnr, h~tplnr, h~ingrp
  FROM aufk AS k
  INNER JOIN afih AS h ON h~aufnr = k~aufnr
  WHERE k~werks = @lv_werks
    AND k~auart = @lv_auart
    AND h~ingrp = @lv_ingrp
    AND NOT EXISTS ( SELECT objnr FROM jest
                       WHERE objnr = k~objnr
                         AND stat IN ( 'I0045', 'I0046' )
                         AND inact = @abap_false )
  ORDER BY k~aufnr
  INTO TABLE @lt_orders
  UP TO 500 ROWS.

IF sy-subrc <> 0.
  WRITE / 'No open orders found'.
ENDIF.

LOOP AT lt_orders INTO DATA(ls_order).
  WRITE: / ls_order-aufnr, ls_order-auart, ls_order-equnr, ls_order-tplnr, ls_order-ktext.
ENDLOOP.

Variant: count of orders per status

SELECT j~stat, COUNT( * ) AS cnt
  FROM aufk AS k
  INNER JOIN jest AS j ON j~objnr = k~objnr
  WHERE k~werks = @lv_werks
    AND k~auart = @lv_auart
    AND j~inact = @abap_false
  GROUP BY j~stat
  ORDER BY cnt DESCENDING
  INTO TABLE @DATA(lt_status_counts).

Uses new Open SQL syntax (ABAP 7.40 SP05 and later) with escaped host variables. On older releases, replace the commas and @ signs with the classic syntax. I0045 is TECO and I0046 is CLSD in standard systems; confirm in TJ02T. Test in a sandbox first and add authority checks (for example on object I_AUART) before releasing to users.

Open order report review

Optional: tell us which order report you maintain and we will look at how to simplify it.

Optional. The tool and every export work without this. No spam.

How to use the generator

Enter your plant, order type and planner group, choose a row limit, and copy the code into a report program in SE38 or into an ABAP class. The result lists order number, type, text, creation date, equipment, functional location and planner group. The second block gives a quick count of orders per active status, which is useful to spot a large pile of TECO orders that were never closed.

How the query works

AUFK holds the order master, including plant and the status object number OBJNR. AFIH adds the PM specific fields. The NOT EXISTS subquery on JEST removes any order that has an active TECO or CLSD status, which is more reliable than checking a single date field. INACT must be blank so that reversed statuses are not counted. Because JEST can have many rows per object, a NOT EXISTS predicate avoids duplicate order rows that a plain join would create.

  • AUFK-OBJNR links to JEST-OBJNR
  • I0045 is TECO and I0046 is CLSD
  • INACT blank means the status is currently active

Common mistakes

The most common error is selecting from JEST with a join and filtering by status not equal to TECO, which still returns orders because they have other active statuses such as REL. Another is forgetting the client when using native SQL. Also avoid SELECT * on AUFK in loops: add only the fields you need, index on plant and order type, and keep a row limit during testing. On HANA, consider a CDS view if the logic is reused.

Frequently Asked Questions

How do I select only orders that are not completed?

Exclude orders that have an active TECO or CLSD status in JEST using a NOT EXISTS subquery on the order object number OBJNR. Make sure INACT is blank, otherwise a reversed status still counts. This code does that for you.

Will this run on S/4HANA?

Yes, AUFK, AFIH and JEST are still available in on-premise S/4HANA and the Open SQL syntax is valid. For high volume reporting consider CDS views or the standard Fiori apps, and always check authorization.

Can I add dates or notifications to the query?

Yes. Join AFKO on AUFNR for scheduled start and finish dates, and join QMEL on AFIH-QMNUM = QMEL-QMNUM for the linked notification. Add each field to both the SELECT list and the result type so the structure still matches.

Why is my result empty?

Check that the plant and order type match exactly, including leading zeros in the planner group. Also confirm that orders are not all TECO already. Run the status count variant to see which active statuses your orders really have.

If you would rather ask for open orders in plain language and get the query shown back, we can connect that to your SAP system with read-only access.