Template calculation when committing an entity

TLDR: What does this mean in the object log?:

User changed values set in Initialize or OnLoaded -> apply step by step
Discarding changes in read/write entity...
Applying logic changes from init and loaded...

To set the stage, I am working on an installation where we've implemented sub-identities. We have three possible vales on Person.IdentityType:

  • Primary
  • Root
  • Organizational

We also use the out of the box limited values on Person.EmployeeType:

  • Employee
  • Contractor

The template code on Person.ExitDate will automatically calculate a 90 day end date for Primary and Root IdentityTypes where the EmployeeType is Contractor.

The template code on Person.IdentityType is just:

Value = "Root"

Overwrites is set to False (unchecked).

Lastly we are using the out of the box template for Person.DateLastWorked. That template checks the ChangeLevel and if the value has been changed because ExitDate changed, it won't change again if ExitDate is has a new value calculated.

Now to the problem. I have a DialogScript that create a new sub-identity. It creates a Person entity and then applies various column values in the order so that the ExitDate calculate correctly. Essentially I set IdentityType BEFORE I set EmployeeType so that an ExitDate is not calculated. I've logged the values and ExitDate and DateLastWorked are left empty.  When I go to commit the record, DateLastWorked gets a value set. I increased debugging on the job service and I see the following in the object log:

For example, the template code on Person.ExitDate will automatically set a value of 90 days in the future for new Primary identities that are marked as contractors. Sub-Identities are no subject to this. To ensure the calculation is correct, I do the following in my DialogScript (pseudo-code):

What's happening is when I commit the object inside a IUnitofWork, ExitDate is temporarily getting a value set which then gets pulled to DateLastWorked. When ExitDate eventually reconnects, it is cleared but the out-of-the-box DateLastWorked template doesn't allow the new value to be cleared.

I increased debugging and I see these line on the object log:

User changed values set in Initialize or OnLoaded -> apply step by step
Discarding changes in read/write entity...
Applying logic changes from init and loaded...
      Running format script of Person.UID_Org with value: ce42...
            Running script Format_Person_UID_Org
            Formatted: ce42...
      Check and execute column format Single Line with Root
      Check and execute column format Limited Value with Root
      Person.IdentityType = Root
      Person.IsTerminalServerAllowed = True

What happens is the value for Person.IdentityType changes back to the default of "Root" but EmployeeType is left at Contractor. This causes an ExitDate to be calculated and pushed to DateLastWorked. The object layer re-sets my intended value on IdentityType back to Organizational, so ExitDate is cleared but DateLastWorked is left.

I believe I can work around the problem by messing with my templates, especially removing the check on ChangeLevels on the DateLastWorked template. I'm just curious why the object layer is clearing my values and reapplying them in a different order.