Dex - How to work with a passed anonymous table without changing table buffer contents

David Meego - Click for blog homepageThis cool technique comes out of an issue that I was working on as part of a support case.  I will not go into specifics, but will explain what was happening.

We have a Dexterity window which reads a table and displayed information from that table to the window.  However, not all the fields in the table have associated window fields, so some of the fields are read and changed using the table buffer.  When the record is saved, the table buffer changes are written down to the physical table.  All sounds fine.

What we found is that when the browse buttons on the window were used, the scripts that ran called a generic procedure.  The window's table buffer was passed anonymously to this generic procedure.  Inside that procedure, the script used a get table command to check if the record passed in the table buffer actually exists. Due to the get table command, side effect of this generic procedure is that the contents of the passed in table buffer are populated with the data stored in the physical table on SQL Server. 

So, what is the issue?  Well, the call to the generic procedure occurs before the call to save the record is made.  As this window is relying on the data in the table buffer, it means that any changes made to the table buffer were lost when the browse buttons were used and save was selected from the "Do you want to save?" dialog.  Clicking the save button worked fine as this does not call the generic code.

Below is some code that shows the situation:

{ Example Procedure Code }
in anonymous table tbl;
out boolean found;

get table tbl;
if err() = OKAY then
found = true;
else
found = false;
end if;

As the generic procedure is called from more that one location, I wanted to find a way to fix the procedure so that it could still look up a table record, but leave the original passed in table buffer untouched. Normally, you would say to use a function or procedure and don't pass the table buffer (just pass the key values), this creates a new instance of the table buffer and the original table buffer is untouched.  However, that method can not work for an anonymous table where we don't know the table or what are its key fields.

Below is some example code showing the changes made to make the code work as desired:

{ Example Procedure Code }
in anonymous table tbl;
out boolean found;

local anonymous table tbl2;

open table tbl2 with name technicalname(table tbl);
copy from table tbl to table tbl2;

get table tbl2;
if err() = OKAY then
found = true;
else
found = false;
end if;

This technique creates a second anonymous table buffer based on the technical name of passed in table and then copies the contents of from the passed in table to the second table buffer.  We can then perform whatever table operations we like on this second anonymous table without affecting the original table buffer passed as a parameter.

[Edit] I just tested this technique using 3rd party tables called from a 3rd party script and confirmed that it does work from any dictionary.

I hope you find this advanced Dexterity technique a useful addition to your arsenal.

David

// Copyright © Microsoft Corporation. All Rights Reserved.
// This code released under the terms of the
// Microsoft Public License (MS-PL, https://opensource.org/licenses/ms-pl.html.)