Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 58 additions & 14 deletions content/docs/analyzers/PlatformCop/PC0036.md
Original file line number Diff line number Diff line change
@@ -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]
Expand All @@ -10,44 +10,88 @@ 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

{{< highlight al "hl_lines=9" >}}
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
Loading