Skip to content

Chapter 1 - Prompting

Learn prompt techniques tailored to SAP integration tasks: iterative refinement, constraint prompting, and building a reusable prompt cookbook.

  • Reading time: 90 minutes
  • Practical time: 45 minutes

Reading

Briefs

PriorityRead ThisWhy It Matters
1What is an LLMA short introduction into the world of Large Language Models.
2AI vs LLMThere is a big difference between these concepts.
3Why AI is ProbabilisticAI is different then the coding you are used to.
4Prompt AnatomyHow to communicate with AI?
5HallucinationAre you sure the answers are correct?
6Tokens, Context & SessionsBasic concepts you should be aware of.

The Same Task, Two Very Different Results

Two developers ask a coding agent to fix the same bug. Here's what happens:

Developer A:

"Fix the bug in the sales order validation."

The agent guesses which include or class to change. It updates validation logic in the wrong place, bypasses an existing BAdI check, and breaks posting for another scenario. Developer A spends an hour undoing the damage.

Developer B:

"In zcl_so_validation.clas.abap, method CHECK_MANDATORY_FIELDS, sales orders can be saved even when KUNNR is initial. Reuse helper method RAISE_FIELD_ERROR from zcl_so_message_helper.clas.abap. Return message class ZSO_MSG, number 001, and keep the existing error pattern used for missing VKORG."

The agent reads both files, understands the pattern, adds the validation, and gets it right on the first try.

Same tool. Same model. Wildly different outcomes. The difference is how the developer communicated. This chapter teaches you that skill.

What Is Prompt Engineering?

Prompt engineering is the practice of crafting instructions that reliably produce the output you want from an AI model.

For chatbots, this means writing a clear question and getting a clear answer. For coding agents, the stakes are higher. Your prompt doesn't just generate text — it triggers a chain of actions. The agent will read files, write code, run commands, and make decisions based on what you told it. A vague prompt doesn't just produce a bad answer — it produces bad actions.

This matters because agents and chatbots operate differently:

DimensionChatbot PromptingAgent Prompting
OutputText responseActions + code changes
ScopeSingle question/answerMulti-step task execution
ContextOnly what you paste inYour entire project (files, terminal, history)
Failure modeBad answer you can ignoreBad code changes you have to undo
CorrectionAsk againReset, re-prompt, or debug the mess
Tool useNoneFile I/O, shell commands, search, browser

When you prompt an agent, think of it less like asking a question and more like briefing a colleague. You wouldn't tell a new team member "fix the bug" and walk away. You'd point them to the right file, explain the expected behavior, and mention any constraints.

Learn More: Prompt engineering for GitHub Copilot Chat

Anatomy of a Good Prompt

Before diving into techniques, it helps to understand the building blocks of a well-structured prompt. Every effective prompt has up to four parts:

ComponentWhat It DoesExample
InstructionThe task or action you want performed"Refactor this function to use async/await"
ContextBackground information the model needs"This is a Node.js 20 project using Express and Knex"
Input dataThe specific data or code to work with"Here's the current callback-based function: ..."
Output indicatorThe format or shape of the desired result"Return only the refactored function, no explanation"

Not every prompt needs all four parts. A simple request might only need an instruction and some context. But when results are off, check which component is missing — that's usually where the problem is.

Here's how these four parts come together in practice:

text
[Context]       "We're working in an S/4HANA ABAP system with RAP and
                 CDS-based business objects."
[Instruction]   "Create a validation method for overdue invoices in the
                 behavior implementation class."
[Input data]    "Use `zbp_i_invoice.clas.abap` and follow the pattern from
                 method `validate_credit_limit`."
[Output]        "Return failed keys and reported messages using RAP
                 `%msg` structure with message class `ZINV_MSG`."

Practice: Rewrite 3 Bad Prompts

1st bad prompt:

text
"Make the form better."

Think about: Which form? What is broken? What must not change? What output format should the AI return?

Suggested rewrite
text
Validate `SMTP_ADDR` using `CL_ABAP_REGEX` in method `CHECK_EMAIL_FORMAT` of class `zcl_bp_validation`. 
If invalid, add message class `ZBP_MSG` number `014` to the return table.
Do not change any other field checks.

Notice what changed: explicit context, clear task, concrete input, output format, and constraints.

2nd bad prompt:

text
"Refactor the user service."

Think about: Which service? How to refactor? What must not change? What output format should the AI return?

Suggested rewrite
text
Refactor `zcl_user_service.clas.abap` to remove duplicated `SELECT` logic by introducing a private helper method.
Keep all public method signatures unchanged so callers in reports and function modules continue to work. 
Do not modify DDIC tables or append structures.

Notice what changed: explicit context, clear task, concrete input, output format, and constraints.

3rd bad prompt:

text
"Write tests for the order service."

Think about: Which report? What is broken? What must not change? What output format should the AI return?

Suggested rewrite
text
Write ABAP Unit tests for method `zcl_order_service=>calculate_total`. 
Follow pattern from class `ltcl_pricing_test` — setup in `setup`, 
act in test method, assert with `cl_abap_unit_assert=>assert_equals`.

Example:

METHOD applies_discount_over_100.
       DATA(ls_order) = build_test_order( iv_amount = '120.00' ).
       DATA(lv_total) = zcl_order_service=>calculate_total( ls_order ).
       cl_abap_unit_assert=>assert_equals( act = lv_total
                                          exp = '108.00' ).
ENDMETHOD.

Notice what changed: explicit example, concrete input, output format, and constraints.

Iterative Refinement

Getting the perfect prompt on the first try is the exception, not the rule. Iterative refinement is the practice of treating your prompt as a first draft — you send it, evaluate the output, diagnose what went wrong, adjust, and try again.

The key skill is diagnosis — knowing why the output is off so you fix the right thing. Most problems trace back to one missing component:

SymptomLikely causeWhat to add or fix
Agent touched the wrong class or methodInstruction too vagueName the exact class and method
Agent used a pattern inconsistent with your codebaseMissing input dataPoint to an existing example to follow
Agent made valid but unwanted changesMissing constraintsExplicitly say what not to change
Output format doesn't match your conventionsMissing output indicatorDescribe the expected structure
Agent invented BAdI names or CDS viewsWrong assumptions from lack of contextFront-load SAP system context (package, release, RAP vs classic)

Refine the prompt, not the output

A common trap is to fix the agent's output manually and move on. This feels faster, but you lose the learning — the next prompt will produce the same problem. Instead, fix the prompt first, then re-run.

Instead of manually correcting the generated ABAP to remove the unwanted SELECT on VBAK.

Add to your prompt:

text
Do not add any new SELECT statements. 
Use only the data already available in the method's importing parameters.

Then re-run the prompt. You now have a better prompt you can reuse.

When to refine vs. when to start fresh

Refinement has limits. If output keeps drifting after two or three adjustments — especially if the agent starts referencing context from earlier in the conversation that's no longer relevant — start a new session with a clean, improved prompt.

Related strategy: The Plan → Review → Act brief covers how to structure larger tasks to reduce the need for refinement in the first place. PRA and iterative refinement complement each other — PRA minimises wrong starts; refinement recovers from them.

Prompt Cookbook

These are reusable prompt templates for the most common ABAP tasks. Replace the <placeholders> with your actual class, method, and package names. Each template is pre-loaded with the constraints and context that prevent the most common agent mistakes for that task.

Write an ABAP Unit test

text
Context: S/4HANA system, ABAP OO. Class <class>.clas.abap,
         method <method>. Existing test class: <ltcl_test_class>.

Write an ABAP Unit test for method <class>=><method>.
Follow the setup/act/assert pattern used in <ltcl_test_class>.
Use cl_abap_unit_assert=>assert_equals for assertions.
Create a helper method build_test_<entity> for fixture setup.

Do not modify the class under test.
Do not add test doubles or frameworks unless they already exist
in the project.

Refactor a method safely

text
Context: Class <class>.clas.abap, method <method>.
         Package <package>. This method is called by <callers>.

Refactor <method> to <goal — e.g. remove duplicate SELECT logic>.
Keep all public method signatures unchanged.
Do not modify DDIC tables, append structures, or CDS views.
Do not change behavior observable by callers — only internal
implementation.

After refactoring, list every change made and explain why.

Add an authorization check

text
Context: Class <class>.clas.abap, method <method>.
         Authorization object: <auth_object>, field <field>,
         activity <activity — e.g. 03 for display>.

Add an AUTHORITY-CHECK for object <auth_object> before the main
logic executes. If the check fails, raise exception
<exception_class> with message class <msg_class>, number <num>.

Follow the pattern already used in method <reference_method>.
Do not change any logic after the authority check.

Create a RAP validation method

text
Context: S/4HANA RAP, behavior implementation class
         <zbp_class>.clas.abap. Business object entity <entity>.
         Message class <msg_class>.

Add a validation method <validate_method_name> that checks
<business rule — e.g. NETWR must be greater than zero>.
Return failed keys via the failed-<entity> structure and
messages via reported-<entity> using %msg.

Follow the pattern from existing validation method
<reference_validation_method>.
Do not add new importing parameters or change the method
signature.

Diagnose a slow OData call

text
Context: ABAP OData service, DPC extension class
         <zcl_dpc_ext>.clas.abap, method <method>.
         Current response time: ~<Xs>.

Before making any changes, read <method> and explain what's
causing the slowdown. List your top 3 hypotheses ranked by
likelihood. For each, state what evidence in the code supports
it.

Do not modify any code yet. Only analyse and explain.

Explain unfamiliar ABAP code

text
Read method <class>=><method> in <class>.clas.abap.

Explain what this method does in plain language:
1. What is its purpose?
2. What are the inputs and outputs?
3. What are the key steps in the logic?
4. Are there any non-obvious patterns, workarounds, or risks
   I should be aware of before modifying it?

Do not suggest improvements or changes. Only explain.

Key Takeaways

PrincipleWhat It Means in Practice
Know when to stopIf the agent invents file paths, references unknown APIs, or generates confident-sounding nonsense — stop. Start a new conversation with a clearer prompt. Pushing through a hallucinating agent wastes more time than starting fresh.
Be in controlAlways understand the code an agent generates. If you can't explain what a function does, don't ship it. You are still the engineer responsible for the output — read the diff, trace the logic, run the tests.
ExperimentThere's no single correct prompt for any task. Try different phrasings and levels of detail. Notice what produces accurate results and what leads the agent astray. Over time you'll develop an intuition for your tools and codebase.

Practical

Prerequisites

Before starting the practical part, make sure you have:

  • Environment ready. If needed, complete Chapter 00 - Setup and Requirements first (Eclipse, ADT, GitHub Copilot, and Copilot subscription setup)
  • Eclipse with your ABAP project open
  • the sample ABAP report below available to copy into chat and into Eclipse
  • Copilot Chat open in Ask mode

Use this intentionally inefficient ABAP example in Exercises 2 and 3. Replace ** with your initials:

abap
REPORT z**_material_overview_report.

TABLES: mara, makt.

TYPES: BEGIN OF ty_result,
                             matnr     TYPE mara-matnr,
                             maktx     TYPE makt-maktx,
                             doc_count TYPE i,
                      END OF ty_result.

DATA: gt_result TYPE STANDARD TABLE OF ty_result,
                     gs_result TYPE ty_result,
                     gt_mara   TYPE STANDARD TABLE OF mara,
                     gs_mara   TYPE mara,
                     gv_count  TYPE i.

SELECT-OPTIONS: so_matnr FOR mara-matnr.

START-OF-SELECTION.
       PERFORM get_material_overview.

FORM get_material_overview.
       SELECT *
              FROM mara
              INTO TABLE gt_mara
              WHERE matnr IN so_matnr.

       LOOP AT gt_mara INTO gs_mara.
              CLEAR gs_result.
              gs_result-matnr = gs_mara-matnr.

              SELECT SINGLE maktx
                     INTO gs_result-maktx
                     FROM makt
                     WHERE matnr = gs_mara-matnr
                            AND spras = sy-langu.

              SELECT COUNT( * )
                     INTO gv_count
                     FROM mseg
                     WHERE matnr = gs_mara-matnr.

              gs_result-doc_count = gv_count.
              APPEND gs_result TO gt_result.
       ENDLOOP.
ENDFORM.

Exercise 1 - Prompt Upgrade Sprint

Goal: Use Ask mode to improve your own prompts.

  1. Open Copilot Chat via Copilot -> Open Chat.
  2. In chat, select Ask mode.
  3. Choose a model (for example, Claude Sonnet).
  4. Paste this prompt:
text
I want to create a simple ABAP class that reads material data from MARA
by material number and returns the description.

Help me rewrite this into a high-quality coding prompt.
First, list the missing information you need from me.
Then provide:
1. an improved prompt
2. a version with stricter constraints
3. a short checklist I can reuse for future prompts.
  1. Review the response and fill in the missing information.
  2. Ask again with the completed context and compare output quality.

Tip: Think of a small task you actually need to do. Write a one-sentence prompt first, then ask: How can I improve this prompt to get better results?

Deliverable:

  • Final improved prompt with context filled in
  • Missing-information checklist
  • 2 bullets on what changed between first and second attempt

Exercise 2 - Explain and Fix the Performance Problem

Goal: Use Ask mode to understand why this report logic is slow and generate a safe performance fix.

  1. Open Copilot Chat via Copilot -> Open Chat.
  2. In chat, select Ask mode.
  3. Choose a model (for example, Claude Sonnet).
  4. Use Object Search to find and open the report, then add it as context in Copilot (Add Context -> current file/selection or drag the open report tab into context).
  5. Write your own prompt asking the AI to:
    • explain what the report does
    • identify the main performance bottlenecks
    • propose a safe fix that keeps behavior unchanged
    • generate the improved report code
  6. Send your prompt.
  7. Review the response and check whether it explains both the functional flow and the proposed performance fix.
  8. Copy the generated optimized report into Eclipse and test it there.
  9. If the response is too vague or the fix changes behavior, refine your prompt and ask again.

Deliverable:

  • Your final prompt
  • 3 to 5 bottlenecks identified
  • One-paragraph plain-language explanation
  • Optimized report code ready to paste into Eclipse

Exercise 3 - Refactor to Classes and ABAP 7.5

Goal: Use Ask mode to modernize the same report into class-based ABAP 7.5 code.

  1. Stay in Ask mode and reuse the same ABAP report from Exercise 2.
  2. Write your own prompt asking the AI to:
    • refactor the report into local classes
    • use ABAP 7.5 syntax
    • preserve selection-screen behavior and output semantics
    • explain the class and method structure
    • generate the refactored code
  3. Send your prompt.
  4. Review the response and check whether the constraints were respected.
  5. Copy the generated class-based version into Eclipse and test it there.
  6. If the code is too broad or does not clearly use class-based ABAP 7.5 patterns, refine your prompt with tighter constraints and ask again.

Deliverable:

  • Your final prompt
  • Ranked refactor plan
  • Proposed class and method structure
  • Refactored ABAP 7.5 code ready to paste into Eclipse
  • 2 bullets: what changed from your first request to final request

For this level, stay in Ask mode inside the chat. The AI may still generate code, but you should transfer it manually into Eclipse rather than applying edits from the chat tool.

What's Next

Continue with Chapter 2 - MCP Server to connect your agent directly to a live SAP system and query real data without manual copy-paste.

Copyright © MSG Systems Romania AI Labs