From 34bdb91d0f8e67b3e0cf52c4b7f57bc613e14d84 Mon Sep 17 00:00:00 2001 From: Arthur van de Vondervoort Date: Tue, 17 Nov 2026 18:05:33 +0100 Subject: [PATCH] docs(PC0036): describe SetRecord as positioning and document the temporary-source exceptions Page.SetRecord only positions a page on the record's key, so a temporary record is only lost on a page that reads from the database. The page now explains that semantics, shows Page.RunModal and a SourceTableTemporary lookup page as the working patterns, and lists the exceptions. Co-Authored-By: Claude Fable 5.1 --- content/docs/analyzers/PlatformCop/PC0036.md | 72 ++++++++++++++++---- 1 file changed, 58 insertions(+), 14 deletions(-) diff --git a/content/docs/analyzers/PlatformCop/PC0036.md b/content/docs/analyzers/PlatformCop/PC0036.md index bca988b..e122d8e 100644 --- a/content/docs/analyzers/PlatformCop/PC0036.md +++ b/content/docs/analyzers/PlatformCop/PC0036.md @@ -1,5 +1,5 @@ +++ -title = 'SetRecord() does not support temporary records' +title = 'SetRecord() does not show the data of a temporary record' linkTitle = 'PC0036' [params] @@ -10,13 +10,11 @@ linkTitle = 'PC0036' ignoreObsolete = true +++ -`Page.SetRecord` binds a page to a specific database record so the page opens at that position. The method does not accept temporary records: +`Page.SetRecord()` sets the current record of a page by its primary key. It does not hand the record's data to the page: the page still reads its records from its own source and only positions itself on that key. When the page's source is a database table, a temporary record passed to `SetRecord()` can never be shown. The page looks the key up in the database, finds nothing or a different record, and silently opens on whatever it found. -> You cannot use a temporary record for the Record parameter. -> -> — [Page.SetRecord(var Record) Method](https://learn.microsoft.com/dynamics365/business-central/dev-itpro/developer/methods-auto/page/page-setrecord-method) on Microsoft Learn +Microsoft Learn states "You cannot use a temporary record for the Record parameter" on [Page.SetRecord(var Record) Method](https://learn.microsoft.com/dynamics365/business-central/dev-itpro/developer/methods-auto/page/page-setrecord-method). There is no runtime error, and the call is valid when the page itself holds temporary data (see the exceptions below). The rule therefore only reports a temporary record passed to a page that reads from the database. -Passing a temporary record variable to `SetRecord` causes a runtime error. Use a non-temporary record variable instead. +The record counts as temporary when its variable carries the `temporary` keyword or when its table has `TableType = Temporary`. ### Example @@ -24,30 +22,76 @@ Passing a temporary record variable to `SetRecord` causes a runtime error. Use a procedure ShowCustomer() var TempCustomer: Record Customer temporary; - CustomerPage: Page "Customer Card"; + CustomerCard: Page "Customer Card"; begin TempCustomer.Init(); TempCustomer."No." := 'TEMP001'; TempCustomer.Insert(false); - CustomerPage.SetRecord(TempCustomer); // SetRecord() does not support temporary records [PC0036] - CustomerPage.Run(); + CustomerCard.SetRecord(TempCustomer); // SetRecord() does not show the data of a temporary record [PC0036] + CustomerCard.Run(); end; {{< /highlight >}} -Use a non-temporary record variable that references an existing database record: +The Customer Card reads from the Customer table, so the page never shows `TEMP001` and opens on another customer instead. -{{< highlight al "hl_lines=3 6-7" >}} +To show temporary data on a page over a database table, open the page with `Page.Run()` or `Page.RunModal()` and the temporary record: + +{{< highlight al "hl_lines=8" >}} +procedure ShowCustomers() +var + TempCustomer: Record Customer temporary; +begin + TempCustomer.Init(); + TempCustomer."No." := 'TEMP001'; + TempCustomer.Insert(false); + Page.RunModal(Page::"Customer List", TempCustomer); +end; +{{< /highlight >}} + +To position a database-backed page on an existing record, pass a record that was read from the database: + +{{< highlight al "hl_lines=6-7" >}} procedure ShowCustomer(CustomerNo: Code[20]) var Customer: Record Customer; - CustomerPage: Page "Customer Card"; + CustomerCard: Page "Customer Card"; begin Customer.Get(CustomerNo); - CustomerPage.SetRecord(Customer); - CustomerPage.Run(); + CustomerCard.SetRecord(Customer); + CustomerCard.Run(); end; {{< /highlight >}} +### Exceptions + +The diagnostic is not reported when the page holds temporary data itself, because `SetRecord()` then selects a record the page loads on its own: + +- The page declares `SourceTableTemporary = true`. +- The page's source table has `TableType = Temporary`. +- The page declares an `OnFindRecord` trigger, which replaces the data source with the page's own records. + +This is the pattern Microsoft's own code uses, for example in the Dataverse option lookups: the page loads its records, and `SetRecord()` preselects one of them. + +{{< highlight al "hl_lines=10" >}} +procedure LookupPaymentTerm(OptionId: Integer): Boolean +var + CRMPaymentTerms: Record "CRM Payment Terms"; + CRMPaymentTermsList: Page "CRM Payment Terms List"; +begin + // The page has SourceTableTemporary = true and a LoadRecords() procedure that fills Rec. + CRMPaymentTermsList.LoadRecords(); + CRMPaymentTerms := CRMPaymentTermsList.GetRec(OptionId); + if CRMPaymentTerms."Option Id" <> 0 then + CRMPaymentTermsList.SetRecord(CRMPaymentTerms); + CRMPaymentTermsList.LookupMode(true); + exit(CRMPaymentTermsList.RunModal() = Action::LookupOK); +end; +{{< /highlight >}} + +`Page.Run()`, `Page.RunModal()`, `GetRecord()`, `SetTableView()` and `SetSelectionFilter()` are never reported: they either accept temporary records by design or do not position the page. + ### See also - [Temporary Tables](https://learn.microsoft.com/dynamics365/business-central/dev-itpro/developer/devenv-temporary-tables) on Microsoft Learn +- [Page.Run(Integer [, Record] [, Any]) Method](https://learn.microsoft.com/dynamics365/business-central/dev-itpro/developer/methods-auto/page/page-run-integer-table-joker-method) on Microsoft Learn +- [Page.RunModal(Integer [, Record] [, Any]) Method](https://learn.microsoft.com/dynamics365/business-central/dev-itpro/developer/methods-auto/page/page-runmodal-integer-table-joker-method) on Microsoft Learn