UNPKG

bc-code-intelligence-mcp

Version:

BC Code Intelligence MCP Server - Complete Specialist Bundle with AI-driven expert consultation, seamless handoffs, and context-preserving workflows

97 lines (64 loc) 6 kB
--- title: "FieldError vs TestField Decision Guide" domain: "maya-mentor" difficulty: "intermediate" bc_versions: "14+" tags: ["fielderror", "testfield", "validation", "error-handling", "method-comparison", "decision-making", "field-validation"] prerequisites: ["fielderror-syntax-usage", "testfield-basic-syntax"] related_topics: ["validation-patterns", "error-handling-strategies", "field-validation"] --- # FieldError vs TestField Decision Guide ## Overview FieldError and TestField serve different purposes in AL validation scenarios, though both handle field validation errors. Understanding when to use each method is crucial for creating effective validation logic that provides appropriate user feedback and maintains proper execution flow. The fundamental difference lies in their validation approach: TestField performs validation checks and conditionally throws errors, while FieldError assumes validation has already failed and immediately displays an error message. **Decision Principle**: Use TestField for conditional validation based on field content; use FieldError for unconditional error reporting when business rules are violated. ## Core Behavioral Differences ### TestField Validation Logic TestField evaluates field conditions before throwing errors. It checks if a field is empty (or doesn't match an expected value) and only throws an error when the validation condition fails. TestField combines validation logic with error reporting in a single method call. ### FieldError Error Reporting FieldError does not perform validation checks. It assumes that validation has already been performed elsewhere and simply displays an error message with field context. FieldError is purely an error reporting mechanism. ### Execution Flow Impact TestField allows code to continue when validation passes, making it suitable for prerequisite checking at the beginning of procedures. FieldError always terminates execution, making it appropriate for definitive validation failure scenarios. ## Usage Context Guidelines ### TestField Scenarios Use TestField when you need to verify that required fields contain appropriate values before proceeding with business logic. TestField works well for mandatory field checking, prerequisite validation, and simple conditional requirements. TestField is ideal for validating field presence or specific expected values where the validation condition can be evaluated in a single expression. ### FieldError Scenarios Use FieldError when complex business rule validation has already determined that a field contains invalid data. FieldError works well in OnValidate triggers, complex validation procedures, and scenarios where the validation logic is too complex for TestField's simple condition checking. FieldError is appropriate when you need custom error messages that provide specific guidance about validation failures beyond simple "field is required" messages. ## Message Customization Capabilities ### TestField Message Options TestField provides limited message customization options. The error messages follow standard patterns: field requirement messages or field value mismatch messages. Custom messaging requires using alternative validation approaches. ### FieldError Message Flexibility FieldError supports extensive message customization through the optional text parameter. This enables specific business context, correction instructions, and user-friendly explanations of validation failures. ## Validation Pattern Integration ### Simple Validation Patterns For straightforward field requirement checking, TestField provides concise, readable code that clearly expresses validation intent. The method name self-documents the validation purpose. ### Complex Validation Patterns For multi-condition validation, business rule enforcement, and context-dependent field checking, FieldError provides better control over error messaging and validation logic separation. ## Performance Considerations ### TestField Efficiency TestField performs simple field value checking with minimal overhead. The method is optimized for common validation scenarios and provides good performance for straightforward field validation. ### FieldError Efficiency FieldError has similar performance characteristics to TestField for error display but requires separate validation logic. The total performance impact depends on the complexity of the accompanying validation code. ## Code Readability and Maintenance ### TestField Clarity TestField code clearly expresses validation intent through the method name and parameters. Code readers can quickly understand that field validation is occurring and what conditions are being checked. ### FieldError Documentation FieldError requires accompanying validation logic, which can make the complete validation more verbose but also more explicit about business rules being enforced. ## Integration with Error Handling Patterns ### TestField in Try Functions TestField works seamlessly with try function patterns for non-blocking validation scenarios. The method's conditional nature aligns well with try-catch error handling approaches. ### FieldError in Validation Procedures FieldError integrates effectively with dedicated validation procedures that perform comprehensive business rule checking before reporting specific validation failures. ## Decision Framework ### Use TestField When: - Performing simple field presence validation - Checking field values against expected constants - Implementing prerequisite checks at procedure entry points - Default error messages are sufficient for user guidance ### Use FieldError When: - Custom error messages are required for user guidance - Complex business rule validation precedes error reporting - Validation logic is too complex for TestField's condition checking - Error reporting needs to occur within validation procedures