locked
Creating New unique field, lookup existing RRS feed

  • Question

  • I'm using a lightswitch computed property to assign usernames to workers, based on their first name, and their last name.

    So, I have a simple calculation that checks to make sure those fields aren't null and if not, computer the username as Me.firstname + Me.lastname

    What I'd like to do, is verify that no one else has that username first.  If they do, I want to loop through their first name, adding a character to the username until it's unique.

    For example, lets say I enter:

    John Smith... his username would be jsmith

    now I go to enter Jane Smith, but she can't be jsmith, because there already is one... so she would be: jasmith.
    if I entered June Smith, she would be jusmith.

    Now, if I wanted to enter Janice Smith, she would end up being jansmith.

    Does this make sense?

    Thanks!

    Sunday, February 7, 2016 11:10 PM

Answers

  • Like I said, I don't know your setup.

    When I check for next order number I'm doing something like this on server (narudzba = order, broj_narudzbe = order no, prodavaona = warehouse):

    Private Sub Narudzbe_Inserting(entity As Narudzba)
                If entity.Broj_narudzbe <> 0 Then Exit Sub 'if already have some number (0 is from entity_created method)
                If entity.Broj_narudzbe = 0 Then
                    Dim wss = Me.DataWorkspace.ApplicationData
                    Dim changes = wss.Details.GetChanges() 'get changes on datasource
                    
                    Dim expr As System.Linq.Expressions.Expression(Of System.Func(Of Narudzba, Boolean)) = _
                        Function(o) o.Prodavaona.Id = entity.Prodavaona.Id
                    Dim exprc = expr.Compile
                    Dim o_pred = wss.Narudzbe.Where(expr).Execute 'orders already in database 
                    Dim n_pred = changes.AddedEntities().OfType(Of Narudzba)().Where(exprc) 'orders waiting to be inserted
                    
                    entity.Broj_narudzbe = o_pred.Union(n_pred).Select(Function(x) x.Broj_narudzbe).DefaultIfEmpty(0).Max() + 1 'next order number
                    entity.Oznaka_narudzbe = String.Format("{0}-{1}", entity.Prodavaona.Naziv, entity.Broj_narudzbe) 'next order description
                End If
            End Sub

    this works also when inserting multiple new entities (like in SL grid).. hth..

    Kivito


    Nobody expects the Spanish Inquisition! (M.P.F.C.)

    • Marked as answer by Angie Xu Wednesday, February 24, 2016 12:38 AM
    Friday, February 19, 2016 11:51 AM

All replies

  • I know what code I can use to do this, if I were just using a normal lookup in .NET, however, I'm not sure what changes I might need to make, in order to use it in Lightswitch.
    Tuesday, February 9, 2016 6:46 PM
  • Hello!

    You should be able to do that on server in *_inserting method, however, implementation is up to you (since I don't know your schema and relationships)..

    perhaps in pseudo-code:

    - calculate username

    - check database for that username

    - if username exist then recalculate username 

    probably in some while loop..

    except, what if you have 4 persons named Ian Smith (splitting hairs but..)? hth..

    Kivito


    Nobody expects the Spanish Inquisition! (M.P.F.C.)

    • Proposed as answer by Angie Xu Wednesday, February 17, 2016 7:55 AM
    Thursday, February 11, 2016 8:43 AM
  • OK, so I'm still struggling with the actual code for this.  I've got a data table called "Users" and in it, there is a computed field called "username", along with fields for First Name (FName) and Last Name (LName).

    Right now, my code-behind for username_compute is just this:

                If Not String.IsNullOrEmpty(Me.FirstName) Then
                    If Not String.IsNullOrEmpty(Me.LastName) Then
    
                        Dim fname As String
                        fname = Me.FirstName
                        fname = fname.Substring(0, 2)
    
                        result = fname + Me.LastName
    
                        'check to see if any other records match this username
    
    
                        result = result.ToLower
                    End If
                End If

    So, how exactly would I iterate through each existing "User" entity, check to see if the username exists and if so, follow the rules I specified above?

    Sorry, but I haven't coded in .NET in quite a while and i'm new to lightswitch, so I'm not sure how to initiate this loop.  

    Thursday, February 18, 2016 7:09 PM
  • Like I said, I don't know your setup.

    When I check for next order number I'm doing something like this on server (narudzba = order, broj_narudzbe = order no, prodavaona = warehouse):

    Private Sub Narudzbe_Inserting(entity As Narudzba)
                If entity.Broj_narudzbe <> 0 Then Exit Sub 'if already have some number (0 is from entity_created method)
                If entity.Broj_narudzbe = 0 Then
                    Dim wss = Me.DataWorkspace.ApplicationData
                    Dim changes = wss.Details.GetChanges() 'get changes on datasource
                    
                    Dim expr As System.Linq.Expressions.Expression(Of System.Func(Of Narudzba, Boolean)) = _
                        Function(o) o.Prodavaona.Id = entity.Prodavaona.Id
                    Dim exprc = expr.Compile
                    Dim o_pred = wss.Narudzbe.Where(expr).Execute 'orders already in database 
                    Dim n_pred = changes.AddedEntities().OfType(Of Narudzba)().Where(exprc) 'orders waiting to be inserted
                    
                    entity.Broj_narudzbe = o_pred.Union(n_pred).Select(Function(x) x.Broj_narudzbe).DefaultIfEmpty(0).Max() + 1 'next order number
                    entity.Oznaka_narudzbe = String.Format("{0}-{1}", entity.Prodavaona.Naziv, entity.Broj_narudzbe) 'next order description
                End If
            End Sub

    this works also when inserting multiple new entities (like in SL grid).. hth..

    Kivito


    Nobody expects the Spanish Inquisition! (M.P.F.C.)

    • Marked as answer by Angie Xu Wednesday, February 24, 2016 12:38 AM
    Friday, February 19, 2016 11:51 AM