Monday, 22 September 2025

SAP ABAP CDS View - Batch Characteristic Value

SAP ABAP CDS View - Batch Characteristic Value

SAP ABAP CDS View: Batch Characteristic Value

In modern SAP development, Core Data Services (CDS View) play a vital role in building reusable and optimized data models. This article demonstrates how to create a CDS View to retrieve Batch Characteristic Values from SAP standard tables.

Objective

The purpose of this CDS View is to extract batch characteristic data from standard SAP tables such as INOB, AUSP, CABN, and related tables. The result can be leveraged for reporting, analytics, or Fiori applications.

CDS View Script


@AbapCatalog.sqlViewName: 'ZV_BATCHCHAR'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'CDS View Batch Characteristic Value'
define view ZCDS_BATCH_CHARACTERISTIC as select from inob as a 
inner join ausp as b on a.cuobj = b.objek 
left outer join cabnt as c on c.atinn = b.atinn
left outer join cabn  as f on f.atinn = b.atinn
left outer join cawnt as d on b.atinn = d.atinn and b.atzhl = right(d.atzhl, 3) {
    b.objek as auspObjek,
    a.objek as obj,
    a.obtab,
    b.klart,
    cast(substring(a.objek, 1, 40) as char40 ) as Material,
    cast(substring(a.objek, 41, 50) as char10 ) as Batch,
    b.atinn,
    f.atnam,
    c.atbez,
    b.atzhl,
    b.atwrt,
    d.atwtb,
    b.atflv,
    b.date_from,
    b.dec_value_from
} where a.obtab = 'MCH1' and a.klart = '023' and c.spras = 'E'
    

Sample Output

Below is an example of the result set when running this CDS View in SAP HANA:

Material Batch Characteristic Description Value (Technical) Value (Text) Valid From
MAT-1001 BATCH001 COLOR Batch Color RED Red 2025-01-01
MAT-1001 BATCH001 EXP_DATE Expiration Date 20251231 31-Dec-2025 2025-01-01
MAT-1002 BATCH005 PURITY Purity Level 99.8 99.8 % 2025-02-15

Consuming CDS View in ABAP

To consume the CDS View in an ABAP program, you can use a simple SELECT statement referencing the CDS View entity name:


REPORT ztest_batchchar.

TYPES: BEGIN OF ty_batchchar,
         material     TYPE char40,
         batch        TYPE char10,
         atnam        TYPE char30,
         atbez        TYPE char60,
         atwrt        TYPE char30,
         atwtb        TYPE char60,
         date_from    TYPE dats,
       END OF ty_batchchar.

DATA lt_batchchar TYPE TABLE OF ty_batchchar.
DATA ls_batchchar TYPE ty_batchchar.

SELECT material,
       batch,
       atnam,
       atbez,
       atwrt,
       atwtb,
       date_from
  FROM zcds_batch_characteristic
  INTO TABLE @lt_batchchar
  UP TO 20 ROWS.

LOOP AT lt_batchchar INTO ls_batchchar.
  WRITE: / ls_batchchar-material,
           ls_batchchar-batch,
           ls_batchchar-atnam,
           ls_batchchar-atwrt.
ENDLOOP.
    

Field Explanation

  • Material → extracted from the object key (first 40 characters).
  • Batch → extracted from the object key (characters 41–50).
  • atnam → technical name of the characteristic.
  • atbez → description of the characteristic.
  • atwrt / atwtb → technical and descriptive values of the characteristic.
  • date_from → start of validity period.

Key Benefits

- Simplifies batch characteristic reporting without complex queries.
- Enables seamless integration with SAP Fiori apps and HANA analytics.
- Provides a reusable data model for multiple business scenarios.

Conclusion

This CDS View provides a clean and efficient way to retrieve batch and characteristic information in SAP ABAP. With this approach, reporting and analytics become much easier and faster to implement.

Feel free to adapt this CDS View and ABAP snippet for your own SAP implementation!

SAP Issue Unapproved Requisition could be converted to Purchase Order

Purchase Requisitions that have not been released/approved can be continued to become Purchase Order. If they have not been released/approved, they cannot be made into Purchase Order. 



If you found this Issue in your SAP S4 System try to implement this SAP Note
https://me.sap.com/notes/3300350/E

Friday, 19 September 2025

Showing Field Invoice Receipt Date in SAP MIR7/MIRO Transaction

Invoice Receipt Date in SAP MIR7 Transaction



First step Maintain Table IDFIVATV_DEF using SM30 and Edit

Add new entries if your company code not maintained yet in this table.

Next step double click on company code and tick Receipt Date Active 



Last step is Save and Open MIR7 Transaction



Thursday, 18 September 2025

Add Custom Fields AR01 Transaction Code in SAP ABAP

Add Custom File to Tcode AR01 in SAP ABAP

Add Custom File to Tcode AR01 in SAP ABAP

A step-by-step guide to extend AR01 functionality with custom fields and logic in SAP ABAP.

Introduction

In this tutorial, we will explore how to add a custom file in tcode AR01 by enhancing the program RABEST_ALV01. The approach involves using the main structure FIAA_SALVTAB_RABEST and the include structure CI_REPRABEST.

Main Structure and Include

  • Main Structure: FIAA_SALVTAB_RABEST
  • Include Structure: CI_REPRABEST

Custom Code Implementation

Below is the ABAP code snippet inserted into the program RABEST_ALV01 to fetch additional information such as cost center, profit center, asset class description, and long text:


FIELD-SYMBOLS: <ANLN1> TYPE ANY.
FIELD-SYMBOLS: <ANLKL> TYPE ANY.
FIELD-SYMBOLS: <KOSTL> TYPE ANY.
FIELD-SYMBOLS: <PRCTR> TYPE ANY.
FIELD-SYMBOLS: <LTEXT> TYPE ANY.
FIELD-SYMBOLS: <TXK50> TYPE ANY.

LOOP AT <itab_data>[] ASSIGNING FIELD-SYMBOL(<fs_csfield>).

  ASSIGN COMPONENT 'KOSTL' OF STRUCTURE <fs_csfield> TO <KOSTL>.
  ASSIGN COMPONENT 'PRCTR' OF STRUCTURE <fs_csfield> TO <PRCTR>.
  ASSIGN COMPONENT 'ANLKL' OF STRUCTURE <fs_csfield> TO <ANLKL>.
  ASSIGN COMPONENT 'TXK50' OF STRUCTURE <fs_csfield> TO <TXK50>.
  ASSIGN COMPONENT 'LTEXT' OF STRUCTURE <fs_csfield> TO <LTEXT>.

  SELECT SINGLE PRCTR FROM CSKS
    INTO <PRCTR> WHERE KOSTL = <KOSTL>.

  SELECT SINGLE LTEXT FROM CEPCT
    INTO <LTEXT> WHERE PRCTR = <PRCTR> AND SPRAS = sy-langu.

  SELECT SINGLE TXK50 FROM ANKT
    INTO <TXK50> WHERE SPRAS = sy-langu AND ANLKL = <ANLKL>.

ENDLOOP.
      

Conclusion

By adding the above logic into RABEST_ALV01, you can extend the AR01 report to include additional information like cost center descriptions, profit center texts, and asset class details. This approach provides flexibility for financial reporting in SAP.

Wednesday, 17 September 2025

How to Create a REST API in SAP ABAP — Step-by-step Guide

How to Create a REST API in SAP ABAP — Step-by-step Guide

Summary: This tutorial shows how to build a REST API in SAP ABAP by creating a handler class, configuring SICF service, registering endpoints with cl_rest_router, implementing endpoint logic (example GET method), and testing the API. Based on an internal implementation reference.

Why expose REST APIs from SAP?

REST APIs allow SAP systems to integrate with web, mobile, and external services using standard HTTP and JSON payloads. Implementing REST endpoints in ABAP provides secure, reusable, and maintainable integration points for modern applications.

Prerequisites

  • Access to an SAP system with authorization to create classes (SE24) and SICF services (SICF).
  • Familiarity with ABAP object-oriented concepts and basic SAP transaction codes.
  • ABAP classes CL_REST_RESOURCE, CL_REST_ROUTER and utilities like /UI2/CL_JSON.

High-level overview (4 steps)

  1. Create an API handler class (e.g. ZCL_API_HANDLER).
  2. Register endpoint templates using cl_rest_router.
  3. Create and activate an SICF service (e.g. /sap/bc/zapi01).
  4. Implement endpoint logic in endpoint classes and test the API.

Step 1 — Create the API handler class

Create class ZCL_API_HANDLER in SE24, implement CL_REST_HTTP_HANDLER, and register endpoints.


DATA(lo_router) = NEW cl_rest_router( ).
lo_router->attach(
  EXPORTING
    iv_template      = '/ZFLIGHTLIST'
    iv_handler_class = 'ZCL_FLIGHT01' ).
ro_root_handler = lo_router.

Step 2 — Create and configure the SICF service

In SICF, create a new service node (e.g. ZAPI01) and attach your handler class ZCL_API_HANDLER. Activate the service.

Step 3 — Create endpoint class

Make class ZCL_FLIGHT01 inheriting from CL_REST_RESOURCE. Redefine GET/POST methods.


METHOD if_rest_resource~get.
  " Parse JSON request
  DATA(lo_entity) = mo_request->get_entity( ).
  DATA(lv_data)   = lo_entity->get_string_data( ).
  DATA: ls_params TYPE ty_flight.

  /ui2/cl_json=>deserialize( json = lv_data changing data = ls_params ).

  " Read SFLIGHT
  IF ls_params IS NOT INITIAL.
    SELECT * FROM sflight INTO TABLE @DATA(lt_flight)
      WHERE carrid = @ls_params-carrid
        AND connid = @ls_params-connid.
  ELSE.
    SELECT * FROM sflight INTO TABLE @lt_flight.
  ENDIF.

  " Build JSON response
  IF lt_flight IS NOT INITIAL.
    /ui2/cl_json=>serialize( data = lt_flight RECEIVING r_json = lv_result ).
    lv_result = '{"Status":"OK","Message":"SUCCESS","Data":' && lv_result && '}'.
  ELSE.
    lv_result = '{"Status":"ERROR","Message":"DATA NOT FOUND"}'.
  ENDIF.

  lo_response->set_string_data( iv_data = lv_result ).
  lo_response->set_header_field(
    EXPORTING iv_name = 'CONTENT-TYPE'
              iv_value = 'APPLICATION/JSON' ).
ENDMETHOD.

Step 4 — Test the API

Test with Postman or cURL. Example:


curl -u username:password -X GET "https://your-sap-host/sap/bc/zapi01/ZFLIGHTLIST" \
-H "Content-Type: application/json" \
-d '{"carrid":"AA","connid":"0017"}'

Security & Best Practices

  • Always use HTTPS and proper authentication.
  • Validate and sanitize inputs.
  • Return correct HTTP status codes.
  • Use pagination for large datasets.
  • Log requests and errors responsibly.

FAQ

Where do I register endpoints?

Inside cl_rest_router in the handler class.

Which transaction creates the web service?

SICF is used to create and activate the service node.

Which ABAP class handles JSON?

/UI2/CL_JSON is commonly used for JSON serialization and deserialization.

Tuesday, 16 September 2025

Custom Agent Determination in SAP Flexible Workflow for Supplier Invoice Approval

Custom Agent Determination in SAP Flexible Workflow for Supplier Invoice Approval

Custom Agent Determination in SAP Flexible Workflow for Supplier Invoice Approval

In SAP MM, supplier invoice approval is often handled using the Flexible Workflow. However, standard configuration may not fully cover complex approval requirements. In such cases, Custom Agent Determination can be implemented using BAdI MRM_WORKFLOW_AGENTS.

This blog explains how to implement a custom approval workflow for supplier invoices (MIR7), including creating custom tables, CDS views, Fiori custom logic, and defining workflow conditions.

1. Create Custom Tables

Two custom tables are required:

a. Table for Mapping User Approval

Table: ZMMT007 (Supplier Invoice Approval Agent Assignment)

  • FUNCTION – Function key
  • FUNDESC – Function description
  • APP_LVL – Approval level
  • BUS_USR – Business user

b. Table for Mapping Condition Approval

Table: ZMMT008 (Invoice Release Strategy)

  • ZREL_GROUP – Release group
  • ZREL_STRAT – Release strategy
  • ZFUNCTION – Function
  • ZSIGN – Comparison sign
  • ZCURR – Currency
  • ZAMOUNT – Amount threshold

2. Create Custom CDS Views

CDS views are required for retrieving approval mapping data. Example definitions:

@AbapCatalog.sqlViewName: 'ZIWFINVOICE'
define view ZI_SUPP_INV_WF as select from zmmt007 {
  key function as Function,
  fundesc as FuncDesc,
  app_lvl as AppLvl,
  bus_usr as BusUser
}
      
@AbapCatalog.sqlViewName: 'ZIWFINVOICEREL'
define view ZI_SUPP_INV_REL_MAP as select from zmmt008 {
  key zrel_group,
  key zrel_strat,
  zfunction,
  zsign,
  zcurr,
  zamount
}
      

Make sure to release CDS Views for usage in custom logic by setting the API State to Use System-Internally (Contract C1).

3. Implement Custom Logic in Fiori

Navigate to Custom Logic app in Fiori and create a new implementation:

  • Business Context: Procurement: Flexible Workflow
  • BAdI: Determination of Workflow Agents for Supplier Invoice
  • Implementation ID: BADI MRM_WORKFLOW_AGENTS

Sample ABAP snippet for custom agent selection:

DATA: lt_wf TYPE TABLE OF ZI_SUPP_INV_WF,
      lt_wf_rel TYPE TABLE OF zi_supp_inv_rel_map,
      ls_wf_relx TYPE zi_supp_inv_rel_map.

SELECT * FROM I_SupplierInvoiceAPI01
  WHERE SupplierInvoice = @supplierinvoice
    AND FiscalYear = @FiscalYear
  INTO TABLE @DATA(lt_SupplierInv).

IF ls_invoice-companycode = 'JSB'
   AND ls_invoice-ACCOUNTINGDOCUMENTTYPE = 'RE'
   AND ls_invoice-INVOICEGROSSAMOUNT >= 1.

  SELECT * FROM zi_supp_inv_rel_map
    WHERE zrel_group = 'WPO-GEN-WF'
      AND zrel_strat = 'ALL'
    INTO TABLE @lt_wf_rel.

ENDIF.
      

In the Filter tab, set the filter: workflowscenario = WS00800303.

4. Define Flexible Workflow Conditions

In the Fiori app Manage Workflows for Supplier Invoices, configure the workflows according to business rules. For example:

a. Invoice With PO

  • Company code = JSB
  • Invoice is based on PO
  • Steps: Level-1 and Level-2 Agent Determination via BAdI

b. Invoice Without PO < 10 Million

  • Company code = JSB
  • Amount < 10,000,000 IDR
  • Steps: Level-1, Level-2, Level-3 Agent Determination

c. Invoice Without PO ≥ 10 Million

  • Company code = JSB
  • Amount ≥ 10,000,000 IDR
  • Steps: Level-1 to Level-4 Agent Determination

Conclusion

By combining custom tables, CDS views, and Fiori custom logic, businesses can implement robust and flexible approval workflows for supplier invoices in SAP. This approach ensures compliance, supports complex approval hierarchies, and automates the agent determination process.

👉 If you’re also interested in other SAP MM workflow enhancements, check out our related article on SAP Invoice Splitting.

How to Set Default Values in SAP Business Partner (BP)

How to Set Default Values in SAP Business Partner (BP)

How to Set Default Values in SAP Business Partner (BP)

In SAP Business Partner (BP) transactions, setting default values can help streamline data entry and ensure consistency across the system. For example, you may want to predefine a default language or tolerance group at the company level. SAP provides a standard way to handle this through the BUSDEFAULT structure and enhancement options.

This guide explains how to set default BP values using customer exits and the function module BUP_BUPA_FIELDVALUES_SET.

Understanding the BUSDEFAULT Structure

SAP provides a structure called BUSDEFAULT, which contains fields that can be configured with default values. You can explore it using transaction SE11.

Some of the key fields available in BUSDEFAULT include:

  • LANGU – Business partner language
  • COUNTRY – Country/Region key
  • POST_CODE1 – City postal code
  • MC_CITY1 – City name
  • TELNR – Telephone number
  • BPKIND – Business Partner Type
  • BU_SORT1 / BU_SORT2 – Search Terms

For a detailed reference, SAP Note 2992030 provides more insights into this feature.

Implementation Steps

Follow these steps to configure default values for SAP BP:

  1. Create a custom function module in the customer namespace.
  2. Within the function module, call BUP_BUPA_FIELDVALUES_SET to populate default values.
  3. Save and activate the function module.
  4. Run transaction BUS7.
  5. Open event ISDAT/ISSTA.
  6. Create a new entry to register your function module for application BUP.
  7. Save and ensure the Call indicator is set to X.

Sample ABAP Code


DATA i_busdefault TYPE busdefault.

CLEAR i_busdefault.
i_busdefault-langu = sy-langu.

CALL FUNCTION 'BUP_BUPA_FIELDVALUES_SET'
  EXPORTING
    i_busdefault = i_busdefault.
      

This example sets the default language (LANGU) based on the system language (sy-langu).

Registering the Function Module

In transaction BUS7, add a new entry to link the function module with the event:

  • Event: ISDAT (Read Data)
  • Function Module: Your custom module (e.g., Z_BUP_BUPA_DEFAULTS)
  • Call: Set to X

This ensures the default values are applied automatically whenever BP data is processed.

Conclusion

By leveraging the BUSDEFAULT structure and BUP_BUPA_FIELDVALUES_SET function module, you can automate the process of setting default values in SAP Business Partner (BP). This not only saves time but also improves data consistency and reduces manual errors.

For complex requirements, you can extend this approach by customizing additional fields within the BUSDEFAULT structure and linking them to events in BUS7.

SAP ABAP - Upload Count PID dengan BAPI

Upload Count PID dengan BAPI di ABAP — Contoh Program YMMC100 Upload Count PID dengan BAPI di ABAP — Progr...