Automatic Rules

WorkWithPlus provides the possibility to insert parametric Rules which will be added to transactions whether they accomplish certain regular expression or some condition property.

The main purpose of Rules is to allow user inserting Rules in WorkWithPlus Configuration that will apply for most of transaction instances, and all transaction will inherit these Rules by default when applying the pattern the first time or when executing Update Instances action.

In order to define a Rule it is necessary to specify the regular expression or the properties that will define to which attributes the rule will be added, and the rule itself which will be able to contain tags like <TRN_NAME>, <TRN_DESCRIPTION>, <ATT_NAME> and <ATT_DESCRIPTION>.

For example, let's suppose we have Person and Country transactions.

Person transaction has the conventional attributes (Id, Name, Lastname, Phone, etc.) but also has PersonCreateDate, PersonCreateUser, PersonUpdatedDate and PersonUpdatedUser which will have to be updated automatically by transaction rules each time a change is made or when it creates. So, Person transaction has the following structure:

The GeneXus Structure editor for the Person transaction lists its attributes with Type, Description, Formula, and Nullable columns, including PersonId, PersonFirstName, PersonLastName, PersonFullName (formula trim(PersonLastName)+' '+PersonF...), PersonHobby, PersonGender, CompanyId, PersonHomeAddress, and an Email subordinate level with EmailDir, EmailDescription, EmailComentarios. A red box highlights four attributes: PersonCreateDate, PersonCreateUser, PersonUpdateDate, and PersonUpdateUser.

The same happens with Country transaction, because it is a requirement for the application to record information about user and date that a change is made, an this transaction has the following structure:

The GeneXus Structure editor for the Country transaction lists its attributes CountryId, CountryName, CountryShortName, CountryPhoneCode, CountryStateLastId, and a State subordinate level with CountryStateId and CountryStateName, with Type and Description columns. A red box highlights four attributes: CountryCreateDate, CountryCreateUser, CountryUpdateDate, and CountryUpdateUser.

The code that developer will have to insert in rules for each transaction that needs to record its last user and date update or creation is very similar, and repetitive. An alternative to avoid inserting in every transaction those rules, is by using Rules feature. 

We would have to insert in Transaction object of Instance Template the following rules:

The WorkWithPlus pattern editor for a Transaction template (Main) on the Country object shows the structure tree with TableMain, subor levels, and Attribute based rules including Rule (NoAcceptAuditing), Rule (SaveCreateDate), Rule (SaveCreateUser), Rule (SaveUpdateDate), Rule (SaveUpdateUser), and a red-boxed group of Rule (SetCreateDate), Rule (SetCreateUser), Rule (SetUpdateDate), Rule (SetUpdateUser), and Rule (noAcceptUpdate_Create Attributes). The right pane previews the generated Country General Information form with fields Id, Name, Short Name, Phone Code, Create Date, Create User, and Update Date.

We can visualize below the properties that each of these rules have set:

Rule: SetCreateDate

This rule will assign to all attributes that contains 'CreateDate' within its name (Regular Expression) the result of Now() function on afterValidate event if it is an insertion. The name of the rule is to identify it within pattern instance.

The Properties panel shows the rule "SetCreateDate" selected, with its Rule expression "<ATT_NAME> = Now() if &Mode = TrnMode.Insert on Afte..." and an "Include when att matches" section where Regular Expression is set to CreateDate.

Rule: SetCreateUser

This rule will assign to all attributes that contains 'CreateUser' (Regular Expression) the value of Context's User on afterValidate event if it is an insertion.

The Properties panel shows the rule "SetCreateUser" selected, with its Rule expression "<ATT_NAME> = &Context if &Mode = TrnMode.Insert on ..." and an "Include when att matches" section where Regular Expression is set to CreateUser.

Rule: SetUpdateDate

This rule will assign to all attributes that contains 'UpdateDate' (Regular Expression) the result of Now() function on afterValidate event if user is updating the record.

The Properties panel shows the rule "SetUpdateDate" selected, with its Rule expression "<ATT_NAME> = Now() if &Mode = TrnMode.Update on Aft..." and an "Include when att matches" section where Regular Expression is set to UpdateDate.

Rule: SetUpdateUser

This rule will assign to all attributes that contains 'UpdateUser' (Regular Expression) the value of Context's User saved on afterValidate event if user is updating the record.

The Properties panel shows the SetUpdateUser attribute-based rule for a GeneXus transaction. The Name field reads "SetUpdateUser" and the Rule field shows "ATT_NAME> = &Context if &Mode = TrnMode.Update on ...". Below, an "Include when att matches" section lists match conditions (Is Nullable, Is Foreign Key, Is Primary Key, Is Inferred, Is Formula, Is First Level, Is Visible), with Regular Expression set to "UpdateUser".

Rule: noAcceptUpdate_Create_Attributes

This rule will set the noaccept rule to all attributes that contains 'Update' or 'Create' next to 'User' or 'Date'.

The Properties panel shows the noAcceptUpdate_Create_Attributes attribute-based rule. The Name field is "noAcceptUpdate_Create_Attributes" and Rule is "noaccept(<ATT_NAME>)". The "Include when att matches" section lists nullable/foreign key/primary key/inferred/formula/first level/visible conditions, with Regular Expression set to "((Update)|(Create))((Date)|(User))".

Then we create default instance for Person transaction, and we can appreciate that it contains the rules created in Instance Template:

The Person transaction object is open with the Work With Plus pattern applied, showing its structure tree: TableMain, TableContent with tabs (General Information, Additional Data, Emails), an Action Group, and an Attribute based rules section. A red box highlights five rules: SetCreateDate, SetCreateUser, SetUpdateDate, SetUpdateUser, and noAcceptUpdate_Create_Attributes.

If we save the object, we could visualize that the rules were added successfully:

A GeneXus code editor shows Person transaction rules, with lines 28-35 highlighted in a red box: noaccept(PersonCreateDate), noaccept(PersonCreateUser), noaccept(PersonUpdateDate), noaccept(PersonUpdateUser), followed by assignments setting PersonCreateDate/User on beforeinsert and PersonUpdateDate/User on beforeupdate using now() and GAMUser.GetName().

The rules were added for the attributes that accomplish the regular expression within Person transaction.

Then we create default instance for Country transaction, and we can appreciate that it contains the rules created in Instance Template:

The Country transaction object is open with the Work With Plus pattern applied, showing TableMain, TableContent, a Panel: State with a Subor level grid, an Action Group, and Attribute based rules. A red box highlights SetCreateDate, SetCreateUser, SetUpdateDate, SetUpdateUser, and noAcceptUpdate_Create_Attributes rules.

Finally we save the object, and visualize that the rules were added successfully:

A GeneXus code editor for the Country transaction highlights lines 12-19 in a red box: noaccept(CountryCreateDate), noaccept(CountryCreateUser), noaccept(CountryUpdateDate), noaccept(CountryUpdateUser), followed by assignments setting CountryCreateDate/User on beforeinsert and CountryUpdateDate/User on beforeupdate via now() and GAMUser.GetName().