How to autogenerate a unique id number for a customer RRS feed

  • Question

  • Hi,

    I'am new to visual lightswitch. I have a customer database and when I add a new customer i want the customer ID to be autogenerate with C# code. I have seen a few post on this forum how to do this for example with the code:
    result = String.Format("MSC{0}",this.Id);

    But that only puts a value of MSC0 in every customer id.

    Can anyone help me with this

    Gr. Tim

    Friday, March 13, 2015 7:50 AM

All replies

  • I do this in the following way. I create a generic handler(ashx) and in the ashx file I execute the following code:

    Using Context As ServerApplicationContext = ServerApplicationContext.CreateContext()
             Dim sem As New Semaphore(1, 1, "MyPONumber") 'Allows only 1 to get a number per access. This ensure a unique Number
             pDate = Request.QueryString("OrderDetailDate") 'Your Parameter with the Order Date
       dim PoNumber as String = "PO"+pDate.Year.ToString("D2")+  pDate.Month.ToString("D2")+ pDate.DayOfWeek.ToString("D2")
       'Getting a unique number is more difficult. I store a counter in the database retrieve the value and store the value
       Dim oPoNr = (From s In Context.DataWorkspace.yourdataspace.table Where s.titel = "PoNumberUniquePart").First
       dim neuePonummer as ULong
            ULong.TryParse(oPoNr.PoNumberUniquePart, neuePonummer)
                 neueNummer += 1
                 oPoNr.PoNumberUniquePart = neueNummer.ToString
      'some possibly execptions
      End Try

    In my LS Screen. You can hit a Button to get the PO Number or do it automatically in a Render function.

     myapp.Screen.Button_execute = function (screen) {
         var paraDate = screen.orderdetaildate;
            type: 'post',
            data: {
                DateID: paraDate,
            url: '../WebRequests/yournameofthe.ashx',
            success: function success(result) {
                // Show result
                if (result != "") {
                     screen.Ponumber = result;
                else {

    These are some extracts from my code. Please use this as an Idea. When you fit it into your Environment it should work.

    Kind Regards


    Friday, March 13, 2015 8:39 AM
  • That is a bit to complex for my skills. But the code is suggested is working now on a computed property. But I need to build a screen where I can search on the customerid. Since it is a computed property I cannot search on the customerid.

    So I think I need to write the code when the customer is created. When I put the same code in the code editor at the place when the customer is created is says that result insn't possible so I need the code that can be filled in there with the exporssions possible since it inst a computed property anymore.

    what I need it to convert: result = String.Format("MSC{0}",this.Id); into a code without the result since this expression cant be set.

    Friday, March 13, 2015 12:06 PM