{"title":"Field calculation methods","url":"https://jonas-paprotka.de/docs/dev-itpro/developer/devenv-calcfields-calcsums-fielderror-fieldname-init-testfield-and-validate-methods/","by":"JOP-AL","license":"Copyright (c) Microsoft Corporation. CC BY 4.0, https://creativecommons.org/licenses/by/4.0/; code samples MIT. From https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/devenv-calcfields-calcsums-fielderror-fieldname-init-testfield-and-validate-methods, MicrosoftDocs/dynamics365smb-devitpro-pb at 828b6ee (2026-11-19). Changed: includes filled in, links to this site, and for agents shortened without dropping a fact.","credit":"Microsoft Corporation, CC BY 4.0","origin":"https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/devenv-calcfields-calcsums-fielderror-fieldname-init-testfield-and-validate-methods","source":{"name":"Business Central developer and IT pro docs","repo":"https://github.com/MicrosoftDocs/dynamics365smb-devitpro-pb","commit":"828b6ee72fcd75755627cd6011f3d20899217ab2","date":"2026-11-19"},"checked":"2026-11-19","description":"CalcFields, CalcSums, FieldError, FieldName, Init, TestField, and Validate Methods in Business Central.","date":"2026-06-29","meta":{"topic":"how-to"},"outline":[{"level":1,"title":"Field calculation methods","slug":"field-calculation-methods","at":0,"chars":8309},{"level":2,"title":"CalcFields method","slug":"calcfields-method","at":412,"chars":1094},{"level":2,"title":"CalcSums method","slug":"calcsums-method","at":1506,"chars":854},{"level":2,"title":"FieldError method","slug":"fielderror-method","at":2360,"chars":1561},{"level":2,"title":"FieldName","slug":"fieldname","at":3921,"chars":456},{"level":2,"title":"Init","slug":"init","at":4377,"chars":333},{"level":2,"title":"TestField method","slug":"testfield-method","at":4710,"chars":2056},{"level":3,"title":"Automatic navigation behavior","slug":"automatic-navigation-behavior","at":5190,"chars":1576},{"level":2,"title":"Validate method","slug":"validate-method","at":6766,"chars":1488},{"level":2,"title":"Related information","slug":"related-information","at":8254,"chars":55}],"text":"# Field calculation methods\nThe following methods perform various actions on fields:\n methods-auto/record/record-calcfields-method\n methods-auto/record/record-calcsums-method\n methods-auto/fieldref/fieldref-fielderror-string-method\n methods-auto/record/record-fieldname-method\n methods-auto/record/record-init-method\n methods-auto/record/record-testfield-joker-method\n methods-auto/record/record-validate-method\n## CalcFields method\nCalcFields updates FlowFields. FlowFields are automatically updated when they're the direct source expressions of controls, but they must be explicitly calculated when they're part of a more complex expression. For more information about Flowfields, see devenv-flowfields.\nCalcFields has the following syntax.\n```al\n[Ok :=] Record.CalcFields(Field1, [Field2],...);\n```\nWhen you use FlowFields in AL methods, you must use the CalcFields method to update them.\nIn the following example, the SETRANGE method sets a filter and then the CalcFields method calculates the Balance and Balance Due fields by using the current filter and performing the calculations that are defined as the CalcFormula properties of the FlowFields.\n```al\nvar\n  Customer: Record Customer;\n\nCustomer.Get('01454545');\nCustomer.SetRange(\"Date Filter\",0D,TODAY);\nCustomer.CalcFields(Balance,\"Balance Due\");\nMessage('The Balance is %1 and your Balance Due is %2',Customer.Balance,Customer.\"Balance Due\");\n```\nThe following message is displayed:\nThe Balance is 342,529.44 and your Balance Due is 342,529.44\n## CalcSums method\nCalcSums calculates the sum of one or more fields that are SumIndexFields in the record.\nCalcSums has the following syntax.\n```al\n[Ok :=] Record.CalcSums (Field1, [Field2],...);\n```\nFor CalcSums, a key that contains the SumIndexFields must be selected as the current key. Similar to CalcFields, CalcSums uses the current filter settings when it performs the calculation.\nIn the following example, an appropriate key is selected, some filters are set, the calculation is performed and then a message is displayed.\n```al\nvar\n  custledgerentry: Record \"Cust. Ledger Entry\";\n\ncustledgerentry.SetCurrentKey(\"Customer No.\");\ncustledgerentry.SetRange(\"Customer No.\",'10000','50000');\ncustledgerentry.SetRange(\"Posting Date\",0D,TODAY);\ncustledgerentry.CalcSums(\"Sales (LCY)\");\nMessage('%1 calculated sales',custledgerentry.\"Sales (LCY)\");\n```\n## FieldError method\nFieldError triggers a runtime error after it displays a field-related error message.\nFieldError has the following syntax.\n```al\nRecord.FieldError(Field, [Text]);\n```\nThis method is similar to the Error method. However, in the FieldError method, if the name of a field is changed, for example, translated to another language, in the Table Designer, the message from the FieldError method reflects the current name of the field.\nThe following examples show how to use the FieldError method.\n```al\nvar\n  Item: Record Item;\n\nItem.Get('70000');\nIf Item.Class <> 'HARDWARE' then\n   Item.FieldError(Class);\n```\nIf item 70000 has a Class other than HARDWARE, then you receive the following error message:\nClass must not be OTHER in Item No. ='70000'.\nIf the text or code field contains the empty string, then you receive the following error message:\nYou must specify Class in Item No.='70000'.\nIf the field is a numeric field and is empty, it's treated as if it contains the value 0 (zero), and then you receive the following error message:\nClass must not be 0 in Item No.='70000'.\nYou can change the default text that is displayed in the error message. The following example shows how to use the FieldError method and change the default text. This example requires that you create the following variable.\nVariable|Data type\nClass|Code\n```al\nvar\n  Class: Code[30];\n\nif Item.Class < '4711' then\n   Item.FieldError(Class,'must be greater than 4711');\n```\nThe following error message is displayed:\nClass must be greater than 4711 in Item No.='70000'.\n## FieldName\nFieldName returns the name of a field. It has the following syntax.\n```al\nName := Record.FieldName(Field);\n```\nYou could just use the name of the field. However, using FieldName lets you create messages that always contain the name of the field, even if the name of the field is changed.\nThis example shows how to use FieldName together with FieldError.\n```al\nFieldError(Quantity,'must not be less than ' + FieldName(\"Quantity Shipped\"));\n```\n## Init\nInit initializes a record. It has the following syntax.\n```al\nRecord.Init();\n```\nIf a default value for a field has been defined by using the InitValue property, this value is used for the initialization. Otherwise, the default value of each data type is used.\n> NOTE\n> Init doesn't initialize the fields of the primary key.\n## TestField method\nTestField tests whether a field contains a specific value. It has the following syntax.\n```al\nRecord.TestField(Field, [Value]);\n```\nIf the test fails, that is, if the field doesn't contain the specified value, an error message is displayed and a run-time error is triggered. This means that any changes that were made to the record are discarded. If the value that you test against is an empty string, the field must have a value other than blank or 0 (zero).\n### Automatic navigation behavior\nThe single-parameter overload `TestField(Field)` and the two-parameter overload `TestField(Field, Value)` behave differently when the test fails:\nOverload|Error dialog behavior\n`TestField(Field)`|Displays a Show [Record] button that navigates to the card page of the related record. This automatic navigation is built into the platform and doesn't require ErrorInfo.\n`TestField(Field, Value)`|Displays only an OK button. No automatic navigation.\nThe platform resolves the navigation target by finding a Card-type page whose SourceTable matches the record's table. For example, `Vendor.TestField(\"Subcontracting Location Code\")` shows a \"Show Vendor Card\" button because the Vendor Card page (page 26) is a Card page with `SourceTable = Vendor`.\n> NOTE\n> The automatic navigation doesn't highlight the specific field that needs attention. If you want field highlighting, a custom button caption, or navigation to a non-default page, use one of the overloads that accepts an methods-auto/errorinfo/errorinfo-data-type parameter. See devenv-actionable-errors.\nTo add custom navigation to any TestField call, use one of the overloads that accepts an methods-auto/errorinfo/errorinfo-data-type parameter. The ErrorInfo object lets you specify a page ID and record ID for the navigation action.\nThe following example tests the Language Code field for customer number 10000 in the Customer table and tests whether the Language Code is ZX.\n```al\nvar\n   customer: Record Customer;\n\ncustomer.Get('10000');\ncustomer.TestField(\"Language Code\",'ZX');\n```\n## Validate method\nValidate calls the OnValidate trigger of a field. It has the following syntax.\n```al\nRecord.Validate(Field [, NewValue]);\n```\nWhen you enter an account number in a ledger, code in a table trigger is executed to transfer the name of the account from the chart of accounts. If you enter an account number in a batch job, the code which transfers the name of the account isn't automatically executed. The following example executes the appropriate field-level trigger code.\n```al\nvar\n  GeneralLedgerEntry: Record \"G/L Entry\";\n\nGeneralLedgerEntry.Validate(\"G/L AccountNo\", '100');\n```\nThis corresponds to the following code.\n```al\nGeneralLedgerEntry.\"G/L AccountNo\" := '100';\nGeneralLedgerEntry.Validate(\"G/L AccountNo\");\n```\nThe Validate method is useful for centralizing processing, which makes your application easier to maintain.\nFor example, if the OnValidate trigger of the Total Amount field performs a calculation that uses values from three other fields as operands, the calculation must be performed again if the contents of any one of these fields changes. You should avoid entering the calculation formula in the OnValidate triggers of each field because this can create errors if the calculation formula has to be changed later and you have to update the code in all the triggers. Instead, you should enter the calculation formula in the OnValidate trigger of only one of the fields and call this trigger code from the OnValidate triggers of the other fields.\n## Related information\nAL methods<methods-auto/library>"}