Alpha Software Mobile Development Tools:   Alpha Anywhere    |   Alpha TransForm subscribe to our YouTube Channel  Follow Us on LinkedIn  Follow Us on Twitter  Follow Us on Facebook

Announcement

Collapse

The Alpha Software Forum Participation Guidelines

The Alpha Software Forum is a free forum created for Alpha Software Developer Community to ask for help, exchange ideas, and share solutions. Alpha Software strives to create an environment where all members of the community can feel safe to participate. In order to ensure the Alpha Software Forum is a place where all feel welcome, forum participants are expected to behave as follows:
  • Be professional in your conduct
  • Be kind to others
  • Be constructive when giving feedback
  • Be open to new ideas and suggestions
  • Stay on topic


Be sure all comments and threads you post are respectful. Posts that contain any of the following content will be considered a violation of your agreement as a member of the Alpha Software Forum Community and will be moderated:
  • Spam.
  • Vulgar language.
  • Quotes from private conversations without permission, including pricing and other sales related discussions.
  • Personal attacks, insults, or subtle put-downs.
  • Harassment, bullying, threatening, mocking, shaming, or deriding anyone.
  • Sexist, racist, homophobic, transphobic, ableist, or otherwise discriminatory jokes and language.
  • Sexually explicit or violent material, links, or language.
  • Pirated, hacked, or copyright-infringing material.
  • Encouraging of others to engage in the above behaviors.


If a thread or post is found to contain any of the content outlined above, a moderator may choose to take one of the following actions:
  • Remove the Post or Thread - the content is removed from the forum.
  • Place the User in Moderation - all posts and new threads must be approved by a moderator before they are posted.
  • Temporarily Ban the User - user is banned from forum for a period of time.
  • Permanently Ban the User - user is permanently banned from the forum.


Moderators may also rename posts and threads if they are too generic or do not property reflect the content.

Moderators may move threads if they have been posted in the incorrect forum.

Threads/Posts questioning specific moderator decisions or actions (such as "why was a user banned?") are not allowed and will be removed.

The owners of Alpha Software Corporation (Forum Owner) reserve the right to remove, edit, move, or close any thread for any reason; or ban any forum member without notice, reason, or explanation.

Community members are encouraged to click the "Report Post" icon in the lower left of a given post if they feel the post is in violation of the rules. This will alert the Moderators to take a look.

Alpha Software Corporation may amend the guidelines from time to time and may also vary the procedures it sets out where appropriate in a particular case. Your agreement to comply with the guidelines will be deemed agreement to any changes to it.



Bonus TIPS for Successful Posting

Try a Search First
It is highly recommended that a Search be done on your topic before posting, as many questions have been answered in prior posts. As with any search engine, the shorter the search term, the more "hits" will be returned, but the more specific the search term is, the greater the relevance of those "hits". Searching for "table" might well return every message on the board while "tablesum" would greatly restrict the number of messages returned.

When you do post
First, make sure you are posting your question in the correct forum. For example, if you post an issue regarding Desktop applications on the Mobile & Browser Applications board , not only will your question not be seen by the appropriate audience, it may also be removed or relocated.

The more detail you provide about your problem or question, the more likely someone is to understand your request and be able to help. A sample database with a minimum of records (and its support files, zipped together) will make it much easier to diagnose issues with your application. Screen shots of error messages are especially helpful.

When explaining how to reproduce your problem, please be as detailed as possible. Describe every step, click-by-click and keypress-by-keypress. Otherwise when others try to duplicate your problem, they may do something slightly different and end up with different results.

A note about attachments
You may only attach one file to each message. Attachment file size is limited to 2MB. If you need to include several files, you may do so by zipping them into a single archive.

If you forgot to attach your files to your post, please do NOT create a new thread. Instead, reply to your original message and attach the file there.

When attaching screen shots, it is best to attach an image file (.BMP, .JPG, .GIF, .PNG, etc.) or a zip file of several images, as opposed to a Word document containing the screen shots. Because Word documents are prone to viruses, many message board users will not open your Word file, therefore limiting their ability to help you.

Similarly, if you are uploading a zipped archive, you should simply create a .ZIP file and not a self-extracting .EXE as many users will not run your EXE file.
See more
See less

Multi Tenant Auto Increment

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Multi Tenant Auto Increment

    Hi All,
    I am looking to create a multi tenant application, i have a settings table which will store the increment number for orders, invoices etc for each tenant. When saving a new order in a dialog, how do i lookup the next order id and increment that number in the settings table?
    Thanks,

    Beau

  • #2
    Re: Multi Tenant Auto Increment

    Are you trying to create a system where the ID is 1,2,3,4 etc. for each tenant? So, the first order ID for each tenant is 1, the second is 2, etc? In other words, it's not a unique ID, but a sequential "order" number by tenant?

    Comment


    • #3
      Re: Multi Tenant Auto Increment

      Hi
      Yes sequential order for each tenant. So tenant 1 could have 1,2,3,4 etc another could have 9001,9002,9003 etc. I can do it on desktop but the webside is stumping me

      Comment


      • #4
        Re: Multi Tenant Auto Increment

        Lots of variables here however, if you mean that each user in the multi-tenant application might create invoices and you want to auto-increment the invoice number to the NEXT number - whatever that might be per tenant, then THAT would be done by doing a lookup to the table and then assigning that returned result to the field.
        I use Mysql as a database and this code auto-increments the invoice number by one after getting the MAX value for the tenant in the invoice number column.
        Code:
        function maxinv as c (e as p)
        	'debug(1)
        	dim cn as sql::Connection
        	dim result as l = .f.
        	dim args as sql::Arguments
        	dim rs as sql::ResultSet
        	dim sqlstm as c 
        	dim invnum as n
        	invnum = session.coid
        	
        	result = cn.open("::Name::Connection1")
        	if result then
        			sqlstm = "SELECT MAX(Invoice_Number) FROM company_invoices WHERE company_id = :argcoid"
        			args.Set("argcoid",invnum)
        			result = cn.execute(sqlstm,args)
        				if result then 
        				rs = cn.ResultSet
        				invnum = rs.data(1)
        				invnum = invnum+1
        				e._set.InvoiceNumber.value = invnum
        	end if
        	cn.close()
        end if
        SO the idea is I get the tenant ID (session.coid) and lookup the MAX value in the table and add one then set the field value to the returned and final value.

        I do not know for sure if that is what you are looking for but if it is you would still need to describe your issue better such as what database your using. I was given help in figuring this out myself!
        NWCOPRO: Nuisance Wildlife Control Software My Application: http://www.nwcopro.com "Without forgetting, we would have no memory at all...now what was I saying?"

        Comment


        • #5
          Re: Multi Tenant Auto Increment

          you should consider when in a multi user environment there is a possibility that the increment number may be duplicated when you get the last number and manually increment that number, this does not happen when you use sql server generated auto increment number. if you want to reduce that possibility then you should get the max number and immediately insert the new record. this will reduce but not eliminate the duplication, i think.
          since the id field is only a record number and no way will influence the customer and there is no use for them and they probably will not see it anyway why not just do it sql server generated autoincrement number?
          thanks for reading

          gandhi

          version 11 3381 - 4096
          mysql backend
          http://www.alphawebprogramming.blogspot.com
          [email protected]
          Skype:[email protected]
          1 914 924 5171

          Comment


          • #6
            Re: Multi Tenant Auto Increment

            Could be true if you have multiple users of a tenant entering invoices!
            They could grab the MAX record at the same time and submit a duplicate, me personally I see no reason for concern because no matter what the invoice is tied to the customer with a UID as well at least in my case. I also warn users that it is possible to create a dupliate number AND they can manually enter in whatever number they want including letters! SO it is entirely up to them. The auto-increment is set when they click the button for NEW invoice only, so after that they can edit it. I guess you could get the max invoice number and then save and submit - and not close the record so as to ensure youv'e got a pretty good chance of creating a unique number, but again, it is really all about how you decide what works for you - like Ghandi said. Seems like a decent method, but I disagree on the auto-increment at the table level due to gaps.


            I don't think auto-increment at the table level for an invoice number is a good idea in a multi-tenant database setup. I don't think it's a good idea, even in a single user setup - you will have gaps in the numbers either way on delete. I guess it's really up to you how you handle it.
            NWCOPRO: Nuisance Wildlife Control Software My Application: http://www.nwcopro.com "Without forgetting, we would have no memory at all...now what was I saying?"

            Comment


            • #7
              Re: Multi Tenant Auto Increment

              in a good accounting system there are no deletes only adjustments. i know quickbooks lets you do it ( i use the product, but not delete). when you delete it takes away the audit trail. in accounting, just like in time, it marches one way.
              thanks for reading

              gandhi

              version 11 3381 - 4096
              mysql backend
              http://www.alphawebprogramming.blogspot.com
              [email protected]
              Skype:[email protected]
              1 914 924 5171

              Comment


              • #8
                Re: Multi Tenant Auto Increment

                Good point on the audit trail.
                NWCOPRO: Nuisance Wildlife Control Software My Application: http://www.nwcopro.com "Without forgetting, we would have no memory at all...now what was I saying?"

                Comment


                • #9
                  Re: Multi Tenant Auto Increment

                  It seems to me that we're over thinking this Beau. I don't see, given the example, any real reason to "store" the sequential number. What it seems to me we want is to "show" a sequential number. In this case, we know order 12002 is after 11997, both of which are orders related to customer id 2343. So, when I query customer id 2343 I read the returned list in an array and order the array and insert a new variable into the array that is computed. I only need to code this function once, and call whenever I want to order sequentially. You could even then push this new data into a list and sort by the new column. This would prevent you from having to worry about writing anything to the server. Hopefully this makes sense.

                  Comment


                  • #10
                    Re: Multi Tenant Auto Increment

                    i do not agree. you don't have to show an imaginary number by inserting an arbitrary value just to show. you can actually set the invoice number to a real sequential number by using a trigger if you use sql server of some flavor.
                    the general trigger statement will be

                    Code:
                    DROP TRIGGER `add_acctno`;
                    
                    CREATE DEFINER=`root`@`localhost` TRIGGER `add_invoiceno` BEFORE INSERT ON `customer`
                    FOR EACH ROW SET NEW.invoice_number = (SELECT MAX(invoice_number) FROM customer) + 1;
                    replace any value to match your table.

                    edit: i haven't tested this in a multi tenant situation.
                    an alternate approach can be to create a folder for each company and add the tables to each folder then auto increment and uniqueness will be natural.

                    here is an example:
                    https://www.screencast.com/t/CRaeAsexj43


                    corrected for multi tenant multiuser scenario
                    Code:
                    DROP TRIGGER `add_acct_number`;
                    
                    CREATE DEFINER=`root`@`localhost` TRIGGER `add_acct_number` BEFORE INSERT ON `customer`
                    FOR EACH ROW SET NEW.account_number = (SELECT MAX(account_number) FROM customer WHERE NEW.company_id = company_id)+1;
                    https://www.screencast.com/t/m1icXUFLP
                    Last edited by GGandhi; 10-24-2017, 07:42 AM.
                    thanks for reading

                    gandhi

                    version 11 3381 - 4096
                    mysql backend
                    http://www.alphawebprogramming.blogspot.com
                    [email protected]
                    Skype:[email protected]
                    1 914 924 5171

                    Comment


                    • #11
                      Re: Multi Tenant Auto Increment

                      Hi Gandhi,

                      I'm not saying to not use normal triggers and sequential keys in the database., I'm saying there isn't a need to write to the database a separate number when the primary key of the record is already being done anyway. If he must create a new column to store this value then what you are recommending is a good way to do it.

                      Comment


                      • #12
                        Re: Multi Tenant Auto Increment

                        Thanks all for your help, i will try to go through these when i return to the office. My reasoning for wanting to store value in a setup table is that some customer like to set there own invoice number at the beginning of each year, i thought it would be easier to just store the next value in another table and when they want to change it, just have a form for this.

                        Comment


                        • #13
                          Re: Multi Tenant Auto Increment

                          So use an internal autoincrement field to set a unique value for your invoice header table in your database. Then the linkages to children will use that. Some call it SID or TransId.

                          Make another field called invoice_number that uses a field on the customer table called Next_Invoice_Number (for that customer) and increment it when a new invoice_number is used for that customer.
                          Al Buchholz
                          Bookwood Systems, LTD
                          Weekly QReportBuilder Webinars Thursday 1 pm CST

                          Occam's Razor - KISS
                          Normalize till it hurts - De-normalize till it works.
                          Advice offered and questions asked in the spirit of learning how to fish is better than someone giving you a fish.
                          When we triage a problem it is much easier to read sample systems than to read a mind.
                          "Make it as simple as possible, but not simpler."
                          Albert Einstein

                          http://www.iadn.com/images/media/iadn_member.png

                          Comment


                          • #14
                            Re: Multi Tenant Auto Increment

                            Seems like using javascript you could set a default value in the new field of 1 (if no other records exist) when you create a new record. If a record(s) exists, cycle through them to find the max value, and use that plus 1 as the default. Your list is basically an array than can easily be cycled through. I already use something similar to this in another project and it works well. However, if you have multiple users editing underneath a particular tenant, at the same time, then you need to use a server side database trigger. Lot's of ways to handle your request Beau.

                            Comment


                            • #15
                              Re: Multi Tenant Auto Increment

                              Hey guys! Thanks for this. Just what I was looking for.

                              Question on a modification. Freshbooks.com allows you to specify the invoice number and it will increment based on what you selected. Similar to this. However, they allow you to put a letter in it such as 2001A and then the next invoice will then be something like 2001B, etc. It will switch to letters and increment those until someone changes the next invoice back to something else.

                              Anyone seen anything like this or can this be done by modifying this? Or am I thinking way above my pay level. :)

                              Comment

                              Working...
                              X