bcquality/custom/knowledge/patterns/cmfrt-maxstrlen-copystr.md
BeytullahCengiz88 afb1fa2883 Add CMFRT naming conventions, object ID ranges, and patterns documentation
- Introduced guidelines for using the "CMFRT" prefix in object names, fields, and procedures to avoid naming collisions and ensure clarity.
- Established rules for object ID ranges to prevent conflicts with other extensions and maintain historical integrity.
- Documented best practices and anti-patterns for various coding patterns, including case statements, interface injection, label usage, and validation methods.
- Implemented a standards review skill to evaluate AL source changes against CMFRT company standards, ensuring compliance with naming, permissions, and architectural patterns.
2026-07-14 14:09:04 +02:00

1.3 KiB

bc-version domain keywords technologies countries application-area
all
patterns
maxstrlen
copystr
truncation
magic-number
field-length
overflow
string
al
w1
all

CMFRT use MaxStrLen in CopyStr, never a literal length

Description

When CMFRT code truncates a text value to fit a field, the length argument of CopyStr must be MaxStrLen(TargetRecord.TargetField), never a numeric literal. The field is the single source of truth for its own length; a hardcoded number silently diverges the moment the field is widened or the code is copied to a field of a different size, producing either needless truncation or a runtime overflow error.

Best Practice

Write Rec."CMFRT AQ User ID" := CopyStr(UserId(), 1, MaxStrLen(Rec."CMFRT AQ User ID"));. The expression stays correct through any future field-length change and documents intent: truncate to whatever fits the target.

See sample: cmfrt-maxstrlen-copystr.good.al.

Anti Pattern

CopyStr(UserId(), 1, 50) — the literal encodes the field length at the time of writing. Widening the field leaves data truncated at the stale length; narrowing it turns the assignment into a runtime "length exceeds" error that only fires on long values in production.

See sample: cmfrt-maxstrlen-copystr.bad.al.