How to pre-instantiate a FK value

Introduction

Transactions can be invoked in different scenarios. Imagine this data model:

SampleStructure
Sample structure

In this case, the “Invoice” transaction may be called (in “Insert” mode):

  1. From the “Work With Invoices” object.
  2. From the “Customer” Entity Manager.
  3. From the “Provider” Entity Manager.

In the first case, all fields (including those corresponding to Foreign Key attributes) should be empty (or have their default value), and all fields are editable by the end user (or are readonly depending on the transaction’s business logic).

In the second and third cases, the reality is different. In these cases, some values are already set when the user enters the transaction, and these values cannot be modified. For example, in the second case, the “Customer” field will be initialized automatically with the Customer that was viewed in the Entity Manager, and in the third case the “Provider” field will be initialized.

RuntimeFKExample

* The image on the left shows the transaction's normal state, the one on the right shows the transaction when the Customer FK is instantiated.

Foreign Key Instantiation (Runtime Appearance)

Solution

A transaction may have many Foreign Keys, which could be potential cases for the automatic instantiation scenario. Because of that, it would be impractical to include all these Foreing Keys in the transaction’s Parm Rule.

Instead, the K2BTrnContext variable is used. This variable’s structure includes a collection of Key-Value pairs to include all FK’s in a flexible manner.

K2BTrnContextStructure
K2BTrnContext structure

When K2BEntityServices is applied on a transaction, two code slots are included to read this collection and initialize all FK’s.

K2BESFKRulesSlots
Generated Rules Slot

K2BESFKEventsSlots
Generated Events Slot

The “Generate Slots For FK” property may be used to deactivate the generation of these slots.

Manual Invokation

When patterns are used, this solution is transparent to the developer.

If a developer wishes to invoke a transaction from their own code, the K2BTrnContext variable variable may be used to initialize the FK’s involved. To do this, the developer must include the corresponding Key-Value pairs in the “Attributes” collection.

FKManualInvokation
Manual Foreign Key Initialization

As shown in thte example above, after loading these values the developer must call the procedure "K2BSetTrnContextByName", passing the transaction's name as the first parameter.