ABAP New Syntax Lecture 8: String Templates, Formatting Options and Functions

ABAP NEW SYNTAX COURSE
LECTURE 8
STRING TEMPLATES
EMBED · FORMAT · TRANSFORM · READ

ABAP string templates create readable text without long chains of CONCATENATE statements. A template is enclosed by pipe characters and can mix literal text with embedded expressions inside braces.

Learning outcome: Build readable messages, format dates and numbers, apply ALPHA conversion, call safe expressions inside templates, and avoid locale and performance mistakes.

1. Your first string template

CODE SCREEN 1 — BASIC TEMPLATE
DATA(lv_message) = |Hello { sy-uname }!|.

DATA(lv_order_text) =
  |Order { lv_vbeln } belongs to customer { lv_kunnr }.|.

Everything outside braces is literal text. Each expression inside { ... } is evaluated and converted to character form at runtime.

2. Classic concatenation versus modern syntax

"Classic
CONCATENATE 'Order' lv_vbeln 'is ready'
  INTO lv_text SEPARATED BY space.

"Modern
DATA(lv_text_new) = |Order { lv_vbeln } is ready|.

The template makes spaces, punctuation and field positions visible in one place. This reduces formatting defects during maintenance.

3. Dates, times and numbers

CODE SCREEN 2 — FORMATTING OPTIONS
DATA(lv_summary) =
  |Date: { sy-datum DATE = USER } |
  && |Time: { sy-uzeit TIME = USER } |
  && |Amount: { lv_amount NUMBER = USER DECIMALS = 2 }|.

DATE = USER, TIME = USER and NUMBER = USER follow the current user formatting environment. Use an explicit technical format when a machine-readable interface requires a stable representation.

4. ALPHA conversion in a template

DATA(lv_display) = |Sales order { lv_vbeln ALPHA = OUT }|.
DATA(lv_internal) = |{ lv_input ALPHA = IN }|.

ALPHA = OUT removes leading zeros for display, while ALPHA = IN adds them when the value and target context support the conversion. Validate domain lengths before relying on the result.

5. Width, alignment, padding and case

DATA(lv_row) =
  |{ lv_material WIDTH = 18 ALIGN = LEFT PAD = '.' } |
  && |{ lv_status CASE = UPPER WIDTH = 10 ALIGN = RIGHT }|.

These options are useful for logs and plain-text exports. For UI layouts, let the UI technology handle visual alignment instead of producing space-padded text.

6. Built-in functions inside templates

CODE SCREEN 3 — EMBEDDED EXPRESSION
DATA(lv_name) = '  sap developer  '.

DATA(lv_result) =
  |Welcome { to_upper( val = condense( val = lv_name ) ) }|.

Embedded expressions keep one-time transformations close to the output. If an expression becomes long, compute it before the template so the message remains easy to scan.

7. Multi-line business message

DATA(lv_mail_body) =
  |Dear { lv_customer_name },| && cl_abap_char_utilities=>newline &&
  |order { lv_vbeln ALPHA = OUT } is ready.| &&
  cl_abap_char_utilities=>newline &&
  |Total: { lv_amount NUMBER = USER DECIMALS = 2 } { lv_currency }.|.

Use an explicit newline value when building mail or log content. For HTML email, construct valid escaped HTML instead of mixing plain-text newlines with markup.

8. Common mistakes

  • Do not assume USER formatting is stable across users or interfaces.
  • Escape or validate external values before embedding them in HTML, JSON or SQL.
  • Avoid expensive method calls repeatedly inside templates used in large loops.
  • Keep templates short; pre-calculate complex business rules.
  • Check which formatting options exist in your ABAP release and language version.
Release note: Available formatting options can differ across ABAP releases. Use F1 help or the ABAP Keyword Documentation connected to your target system.

9. Practical exercise

  1. Create an order confirmation containing order number, customer, date, amount and currency.
  2. Display the order without leading zeros.
  3. Format the amount with two decimal places.
  4. Create both USER-formatted and stable technical variants, then compare them.
  5. Move one complex embedded calculation into a helper variable.

10. Interview quick checks

What delimiters define an ABAP string template?

Pipe characters define the template, while braces enclose embedded expressions.

Why can NUMBER = USER be unsuitable for an API payload?

Its result depends on user formatting settings; machine interfaces normally need an explicitly defined stable format.

When should a calculation be moved outside the template?

When it is expensive, repeated or makes the output expression difficult to read and test.

Official SAP references

Educational examples. Validate formatting behavior, escaping requirements and syntax availability on your ABAP release.

Post a Comment

0 Comments