Phani Puttabakula - Mar 12, 2026
Part 4: Writing ABAP Code with ABAPer – Draft Structure
Part of ABAPer Series
Summary
ABAPer doesn’t just analyze code — it writes it. This article demonstrates AI-assisted code generation for ABAP programs, classes, database tables, CDS views, and unit tests. You’ll learn prompt engineering patterns that produce production-quality ABAP on the first try.
The Problem
Boilerplate ABAP code is tedious. Creating a new class with methods, exception handling, and proper patterns takes time. Writing CDS views requires memorizing annotation syntax. Unit tests are often skipped because they’re time-consuming to set up. AI code generation solves these problems — but only if the prompts are structured correctly.
Why This Matters
ABAPer can create and activate ABAP objects directly in your SAP system through MCP tools (create-object, create-and-activate, update-object, activate-object). This isn’t copy-paste from ChatGPT — the AI creates the object in SAP, writes the source code, and can activate it in one step.
Step-by-Step Tutorial
Section 1: Creating an ABAP Program
PROMPT:
Create an ABAP report program Z_CUSTOMER_AGING in package ZTEST.
It should:
- Accept company code and customer number range on selection screen
- Read open items from BSID
- Calculate aging buckets: 0-30, 31-60, 61-90, 90+ days
- Display results in ALV using CL_SALV_TABLE
- Use modern ABAP syntax (inline declarations, string templates)
- Include proper error handling and authority checksEXPECTED OUTPUT: [Full ABAP program source with:
- REPORT statement
- TYPE declarations for output structure
- SELECTION-SCREEN with PARAMETERS and SELECT-OPTIONS
- AUTHORITY-CHECK
- SELECT with explicit field list
- Aging calculation logic
- ALV display with CL_SALV_TABLE
- Exception handling with TRY/CATCH]
What happens behind the scenes:
- AI generates the source code
create-objectMCP tool creates the program in SAP- Optionally,
activate-objectactivates it
[Screenshot placeholder: AI panel showing program creation with MCP tool execution log]
Section 2: Creating a Class
PROMPT:
Create an ABAP class ZCL_MATERIAL_SERVICE in package ZTEST.
Public methods:
- GET_MATERIAL importing IV_MATNR type MATNR returning RS_MARA type MARA
raises ZCX_NOT_FOUND
- SEARCH_MATERIALS importing IV_PATTERN type STRING returning RT_MATERIALS type MARA_TAB
- CREATE_MATERIAL importing IS_MARA type MARA returning RV_MATNR type MATNR
raises ZCX_MATERIAL_ERROR
Use constructor injection for database access (testability).
Follow clean ABAP principles.EXPECTED OUTPUT: [Full class definition and implementation with:
- CLASS … DEFINITION PUBLIC CREATE PUBLIC
- PUBLIC SECTION with method signatures
- PRIVATE SECTION with attributes
- METHOD implementations
- Exception handling
- Constructor with dependency injection pattern]
Section 3: Creating a Database Table
PROMPT:
Create a transparent table ZCUSTOMER_SCORE in package ZTEST.
Fields:
- MANDT (key, client)
- CUSTOMER_ID (key, CHAR 10)
- SCORE_DATE (key, DATS)
- CREDIT_SCORE (DEC 5,2)
- RISK_CATEGORY (CHAR 1: A=low, B=medium, C=high)
- LAST_UPDATED (TIMESTAMP)
- UPDATED_BY (SYUNAME)
Include delivery class A, no table maintenance dialog.EXPECTED OUTPUT: [ABAP Dictionary table definition source]
Section 4: Creating CDS Views
PROMPT:
Create a CDS view Z_I_SalesOrderItem that:
- Reads from VBAP (sales order items) and VBAK (sales order header)
- Joins on VBELN
- Exposes: order number, item, material, quantity, net value, currency, order date, sold-to party
- Add appropriate @AbapCatalog and @ObjectModel annotations
- Include association to I_MaterialEXPECTED OUTPUT: [CDS view source with:
- @AbapCatalog.sqlViewName annotation
- @AbapCatalog.compiler.compareFilter: true
- @AccessControl.authorizationCheck: #CHECK
- @ObjectModel annotations
- SELECT with JOIN
- Association definition
- Proper field aliases]
Section 5: Writing Unit Tests
PROMPT:
Generate ABAP unit tests for class ZCL_MATERIAL_SERVICE.
Cover:
- GET_MATERIAL with valid material → returns correct data
- GET_MATERIAL with invalid material → raises ZCX_NOT_FOUND
- SEARCH_MATERIALS with pattern → returns matching entries
- SEARCH_MATERIALS with no match → returns empty table
- CREATE_MATERIAL with valid data → returns material number
- CREATE_MATERIAL with duplicate → raises ZCX_MATERIAL_ERROR
Use test doubles for database access.
Follow the Given-When-Then pattern.EXPECTED OUTPUT: [Test class source with:
- CLASS ltcl_material_service DEFINITION FOR TESTING RISK LEVEL HARMLESS DURATION SHORT
- SETUP method with test double injection
- Individual test methods following Given-When-Then
- cl_abap_testdouble or manual mock usage
- Assertions with cl_abap_unit_assert]
Section 6: Prompt Engineering for ABAP
Pattern 1: Specify the Object Type and Name
Create an ABAP [program|class|interface|table|CDS view] named [NAME] in package [PACKAGE].Starting with the object type tells the AI which MCP tool to use and what source format to generate.
Pattern 2: Define the Contract First
Public methods:
- METHOD_NAME importing [params] returning [result] raising [exceptions]Defining the method signatures before asking for implementation produces more consistent results.
Pattern 3: Specify Standards
Use modern ABAP syntax. Follow clean ABAP guidelines.
No obsolete statements. Use inline declarations.Without this, the AI may generate older-style ABAP.
Pattern 4: Include Non-Functional Requirements
The program processes 1M records daily. Optimize for performance.
Use parallel processing where appropriate.Context about volume and performance requirements changes the generated code significantly.
Pattern 5: Reference Existing Objects
Follow the same pattern as ZCL_EXISTING_SERVICE.
Use the same error handling approach as Z_REFERENCE_PROGRAM.The AI can fetch reference objects from SAP and use them as templates.
Section 7: Validation Strategies
After AI generates code, always validate:
- Syntax Check — Use the Problems panel or ask: “Run syntax check on this code”
- Activation — Activate the object to catch dependency errors
- Unit Tests — Run tests if generated: click the Test quick action
- Code Review — Run a review on the generated code: click Review
- Manual Inspection — Read through the generated code. AI may misunderstand requirements.
PROMPT for self-review:
I asked you to create Z_CUSTOMER_AGING.
Review the generated code against my original requirements.
List any missing requirements or deviations.Best Practices
Generate in steps, not all at once. Create the class definition first, review it, then ask for the implementation. This gives you control at each stage.
Always specify the package. ABAPer needs to know where to create the object in the SAP repository.
Provide example data. For reports and data processing programs, describe sample input/output to guide the AI.
Ask for tests alongside code. Generate the test class immediately after the production class — the AI has full context.
Use create-and-activate for speed. When you’re confident in the prompt, the AI can create and activate in one operation, saving round trips.
Don’t generate and forget. Always review AI-generated code. It’s a starting point, not a finished product.
Troubleshooting
Object creation fails with “authorization error”
- Your SAP user needs developer authorization and a developer key
- Check that you have write access to the target package
Generated code doesn’t activate
- Run syntax check first to identify errors
- Common issues: missing type definitions, referenced objects don’t exist in your system
- Ask the AI: “The activation failed with error [paste error]. Fix the code.”
Generated code uses obsolete syntax
- Add to your prompt: “Use modern ABAP syntax only. No obsolete statements.”
- Be specific: “Use inline declarations, string templates, and NEW instead of CREATE OBJECT”
CDS view annotations are wrong
- CDS annotation syntax varies by SAP release. Specify your system version: “Target SAP S/4HANA 2023”
- If activation fails, paste the error back to the AI for correction
Unit tests fail immediately
- Ensure test doubles are set up correctly
- Check that the class under test supports constructor injection
- If tests reference non-existent objects, ask the AI to fix the dependencies
Conclusion
[Summarize: creating programs, classes, tables, CDS views, and unit tests with AI. Emphasize the create → activate → test workflow. Note that AI generation is a starting point — always validate.]
Coming Next in the Advanced Series
Future articles will cover:
- Automated Refactoring — Converting legacy code to modern ABAP patterns
- SAP S/4HANA Migration Assistance — Batch remediation of incompatible code
- Multi-Step AI Workflows — Chaining prompts for end-to-end development
- GitHub-Based Development — Using Git workflows with ABAP
- Enterprise Code Governance — Team-wide quality standards with AI
- AI-Driven Test Generation — Comprehensive test coverage strategies
Ready to transform your SAP experience?
Join thousands of developers using BlueFunda AI tools.