locked
Insert User ID as Default Value everytime adding Details. RRS feed

  • Question

  • Hi guys,

    Trying to frame my question in another way. In Lightswitch VB.

    How to Insert  User ID logged ( Logged with User name) as Default Value everytime adding a new line on a Working Details Screen.

    Having a Week/Dates Table,  Employee Table, Projects Table, and Working Details Table. all linked.

    Would appreciate Any Help,

    Param singh



    • Edited by ParamSingh Wednesday, May 20, 2015 9:34 AM
    Wednesday, May 20, 2015 9:10 AM

Answers

  • Hi Otis,

     I think my question was misleading and was not clear enough.. but Thanks again.

    got it solved by a simple code : with Help from 

    https://hgminerva.wordpress.com/2011/12/05/dlookup-vs-linq/

    ----          

      Private Sub Detalle_HorasItem_Created()

             Dim item = (From i In Me.DataWorkspace.ARCA_TOTALData.Empleados Where i.Nombre = Me.Application.User.Name Select i).First
                Me.EmpleadosItem = item
            End Sub

    -----

    Param Singh

    • Marked as answer by ParamSingh Wednesday, May 20, 2015 4:28 PM
    Wednesday, May 20, 2015 4:28 PM

All replies

  • I use code in the data source's SaveChanges_Executing event handler.

    /// <summary>Assign updated-by & updated-when to any entity containing those attributes.</summary>
    partial void SaveChanges_Executing()
    {
        List<IEntityObject> changedEntities = this.Details.GetChanges()
            .Where(e => e.Details.EntityState == EntityState.Added || e.Details.EntityState == EntityState.Modified)
            .ToList();
        foreach (IEntityObject entity in changedEntities)
        {
            this.AssignUpdatedByUserAndTime(entity);
        }
    }
    
    /// <summary>Assign updated-by & updated-when if the entity contains those attributes.</summary>
    private void AssignUpdatedByUserAndTime(IEntityObject entity)
    {
        if (entity.Details.Properties.Contains("UpdatedBy"))
        {
            IProperty updatedBy = entity.Details.Properties["UpdatedBy"];
            string updatedByValue = updatedBy.Value as string;
            if (updatedByValue == null || updatedByValue != Application.User.FullName)
                updatedBy.Value = Application.User.FullName;
        }
        if (entity.Details.Properties.Contains("UpdatedDateTime"))
        {
            IProperty updatedWhen = entity.Details.Properties["UpdatedDateTime"];
            DateTimeOffset? updatedWhenValue = updatedWhen.Value as DateTimeOffset?;
            //Avoid possible loops by only updating time when it has changed by at least 1 minute.
            if (updatedWhenValue == null || updatedWhenValue < DateTimeOffset.Now.AddMinutes(-1))
                updatedWhen.Value = DateTimeOffset.Now;
        }
    }
    

    Paul

    Wednesday, May 20, 2015 12:01 PM
  • Paul Could you be kind to Write in VB instead of C# ?

    Param Singh

    Wednesday, May 20, 2015 2:01 PM
  • Hello

    Check out Beth Massi's posts on lightswitch. There are loads of useful code parts including the below snippet that should get you over the hump.

    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 

    The link to the page that you need to see is (http://blogs.msdn.com/b/bethmassi/archive/2011/06/22/how-to-create-a-simple-audit-trail-change-log-in-lightswitch.aspx)

    If you found this post helpful, please mark it as helpful. If by some chance I answered the question, please mark the question as answered. That way you will help more people like me :)

    Wednesday, May 20, 2015 2:42 PM
  • Hi Otis,

     I think my question was misleading and was not clear enough.. but Thanks again.

    got it solved by a simple code : with Help from 

    https://hgminerva.wordpress.com/2011/12/05/dlookup-vs-linq/

    ----          

      Private Sub Detalle_HorasItem_Created()

             Dim item = (From i In Me.DataWorkspace.ARCA_TOTALData.Empleados Where i.Nombre = Me.Application.User.Name Select i).First
                Me.EmpleadosItem = item
            End Sub

    -----

    Param Singh

    • Marked as answer by ParamSingh Wednesday, May 20, 2015 4:28 PM
    Wednesday, May 20, 2015 4:28 PM