Use the TestField function
With the TestField function, you can check if a field has a value or if it is blank. If the field is empty, the TestField function generates a run-time error.
customer.TestField("Salesperson Code");
You can also use the TestField function to test whether a field contains a specific value or not. If it doesn't, the function generates an error. In the following example, the TestField function checks if the Salesperson Code contains the value ZX. The value is set to DK, so this should generate an error.
customer."Salesperson Code" := 'DK';
customer.TestField("Salesperson Code", 'ZX');
When the function generates an error, it produces a multilanguage error with the translated Caption property of that field.
The TestField function is often used in the OnInsert trigger of master and document tables. These functions test if a number series is configured within the setup table before assigning a new number to the record based on a number series. The following example shows how these functions are programmed for the Customer table.
trigger OnInsert()
var
Customer: Record Customer;
#if not CLEAN24
NoSeriesMgt: Codeunit NoSeriesManagement;
#endif
IsHandled: Boolean;
begin
IsHandled := false;
OnBeforeInsert(Rec, IsHandled);
if IsHandled then
exit;
if "No." = '' then begin
SalesSetup.Get();
SalesSetup.TestField("Customer Nos.");
#if not CLEAN24
NoSeriesMgt.RaiseObsoleteOnBeforeInitSeries(SalesSetup."Customer Nos.", xRec."No. Series", 0D, "No.", "No. Series", IsHandled);
if not IsHandled then begin
#endif
"No. Series" := SalesSetup."Customer Nos.";
if NoSeries.AreRelated("No. Series", xRec."No. Series") then
"No. Series" := xRec."No. Series";
"No." := NoSeries.GetNextNo("No. Series");
Customer.ReadIsolation(IsolationLevel::ReadUncommitted);
Customer.SetLoadFields("No.");
while Customer.Get("No.") do
"No." := NoSeries.GetNextNo("No. Series");
#if not CLEAN24
NoSeriesMgt.RaiseObsoleteOnAfterInitSeries("No. Series", SalesSetup."Customer Nos.", 0D, "No.");
end;
#endif
end;
if "Invoice Disc. Code" = '' then
"Invoice Disc. Code" := "No.";
if (not (InsertFromContact or (InsertFromTemplate and (Contact <> '')) or IsTemporary)) or ForceUpdateContact then
UpdateContFromCust.OnInsert(Rec);
if "Salesperson Code" = '' then
SetDefaultSalesperson();
DimMgt.UpdateDefaultDim(
DATABASE::Customer, "No.",
"Global Dimension 1 Code", "Global Dimension 2 Code");
UpdateReferencedIds();
SetLastModifiedDateTime();
OnAfterOnInsert(Rec, xRec);
end;
In the Sales Header table, a separate TestNoSeries function is created because it has to test different fields in the setup table, depending on the selected Document Type in the Sales Header table.
/// <summary>
/// Checks if the number series for different types of sales documents are filled in the sales setup.
/// </summary>
procedure TestNoSeries()
var
IsHandled: Boolean;
begin
GetSalesSetup();
IsHandled := false;
OnBeforeTestNoSeries(Rec, IsHandled);
if not IsHandled then begin
case "Document Type" of
"Document Type"::Quote:
SalesSetup.TestField("Quote Nos.");
"Document Type"::Order:
SalesSetup.TestField("Order Nos.");
"Document Type"::Invoice:
SalesSetup.TestField("Invoice Nos.");
"Document Type"::"Return Order":
SalesSetup.TestField("Return Order Nos.");
"Document Type"::"Credit Memo":
SalesSetup.TestField("Credit Memo Nos.");
"Document Type"::"Blanket Order":
SalesSetup.TestField("Blanket Order Nos.");
end;
GLSetup.GetRecordOnce();
if not GLSetup."Journal Templ. Name Mandatory" then
case "Document Type" of
"Document Type"::Invoice:
SalesSetup.TestField("Posted Invoice Nos.");
"Document Type"::"Credit Memo":
SalesSetup.TestField("Posted Credit Memo Nos.");
end
else begin
SalesSetup.GetRecordOnce();
if not IsCreditDocType() then begin
SalesSetup.TestField("S. Invoice Template Name");
if "Journal Templ. Name" = '' then
GenJournalTemplate.Get(SalesSetup."S. Invoice Template Name")
else
GenJournalTemplate.Get("Journal Templ. Name");
end else begin
SalesSetup.TestField("S. Cr. Memo Template Name");
if "Journal Templ. Name" = '' then
GenJournalTemplate.Get(SalesSetup."S. Cr. Memo Template Name")
else
GenJournalTemplate.Get("Journal Templ. Name");
end;
GenJournalTemplate.TestField("Posting No. Series");
GlobalNoSeries.Get(GenJournalTemplate."Posting No. Series");
GlobalNoSeries.TestField("Default Nos.", true);
end;
end;
OnAfterTestNoSeries(Rec, SalesSetup);
end;