2

is there any easy way to track what is changed on a lightswitch app screen?

i have a form that shows information about a customer (listdetail). When I save it, i want to write to a history table what was changed.

Alex J
  • 1,547
  • 2
  • 26
  • 41

1 Answers1

3

found the answer here.

Just use the following code in the _updating, _inserting events for the controls.

    Private Sub Employees_Updating(entity As Employee)
       Dim change = entity.EmployeeChanges.AddNew()
       change.ChangeType = "Updated" change.Employee = entity
       change.Updated = Now()
       change.ChangedBy = Me.Application.User.FullName

       Dim newvals = "New Values:"    
       Dim oldvals = "Original Values:"    

       For Each prop In entity.Details.Properties.All().
            OfType(Of Microsoft.LightSwitch.Details.IEntityStorageProperty)()

           If prop.Name <> "Id" Then             

                If Not Object.Equals(prop.Value, prop.OriginalValue) Then                 
                    oldvals += String.Format("{0}{1}: {2}", vbCrLf, prop.Name, prop.OriginalValue)
                    newvals += String.Format("{0}{1}: {2}", vbCrLf, prop.Name, prop.Value)
                End If         
           End If    

       Next   

      change.OriginalValues = oldvals
      change.NewValues = newvals

End Sub 
Alex J
  • 1,547
  • 2
  • 26
  • 41