Custom Relinking of Fields

The standard “Adopt Fields” method of relinking assumes field names and key field values are consistent between the existing links and the new data.  It was designed to link fields from one data source to another, and as such is limited to changing the key element of a link.

In some circumstances it may be necessary to implement custom re-linking logic. Possibilities with this method include:

  • Changing a key.
  • Relinking a field from one record to another.
  • Altering the field name.

To implement this logic, a LUA script can be created either by using the “Adopt Fields > Custom…” menu option or by placing a ‘.lua’ script inside of a folder called “Adopt Fields” within the data source’s “Scripts” folder.

The LUA script is called once for each link in the document, and on entry four string variables are defined. It is the responsibility of the LUA script to change the values of these variables before exiting.  EasyCatalog will then use the contents of these variables to modify the link in the document.

VariableContents
keyThe key field value that identifies the record that the field is linked to
datasourceThe name of the data source that the field is linked to
fieldnameThe name of the field that the field is linked to
fieldcontentThe content of the field in the document. For image fields, this will be empty

In addition a “records” parameter is passed, which is a RECORDSET object of the data panel selection.  The example shown below combines these to relink records placed from one group to another group based on the “Position” field, which is numbered 1..n in each group:

Another example, that changes the ‘language’ part of a key:

The Custom Link Adoption Dialog

The Custom Link Adoption dialog can be used to develop the LUA script, as it shows both the existing document links and what they will become after running the script on the document.

The dialog displays the active documents links on the left, and the links after processing by the script. Pressing the Test button will execute the code for each link and present the results in the right hand column. Each element of the link is color coded to show validity, with green valid (the field links to a valid data source, record and field) and red invalid.

Saving Scripts To the Adopt Fields Menu

Custom adoption can be used as a regular part of a workflow, and as such scripts can be added to the “Adopt Fields” menu. Selecting “Save..” on the popup at the bottom of a dialog will prompt for a name. This is then permanently added to the menu for the current data source.   These are stored inside the Scripts folder inside the Data source folder and can be removed manually.