Auto Numbering - Customer ID

And it’s all packaged up for you here:

https://www.epiusers.help/t/lets-share-useful-functions-sharing-is-caring/100371/23?u=klincecum

2 Likes

I know this post is a bit old, but I have a question. I put this in place a while ago but it seems like the next sequence number may have gotten messed up. Where is this stored in the database so I can go see what it is?

You’re in luck, I added two functions not long ago that lets you get the current sequence.

But to directly answer your question, company sequences are in table CompanySequence while the global ones are in SysSequence, but you won’t be able to get to the globals without sql. :roll_eyes:

Thanks! I do have access to Sql and I can see it is not what I expected :frowning:

1 Like

Hi, I am using the Get and SetNextSequence functions for creating the next available Job Number that is multi-thread safe

For this use-case, it is possible that I will have to reset the sequence and check for available #s again, which means calling SetNextSequence|PerCompany when the number reaches the max would potentially get called by multiple threads.

To make the SetNextSequence|PerCompany call safer for MT use, I added the GetCurrentSequence logic to it and have the SetNext output a status to let the user/ calling function know if it was successful or not.

To call, you will now also need to know the current value of that key (or add logic to ignore this step if you set it to something like -1)

I don’t know if you can LOCK the specific sequence from being read/write while this function runs to be 100% sure no other thread can sneak in there but here is what I have:

using (ErpContext dbContext = Ice.Services.ContextFactory.CreateContext<ErpContext>()) //Erp Context
    {
        using (var nextValue = new Erp.Internal.Lib.CompanySequence(dbContext))
        {
            var curValue = dbContext.CompanySequence.FirstOrDefault(x => x.Company == companyID && x.SequenceName == sequenceKey).CurrentValue;
            if(curValue == this.expectedCurValue)
            {
                nextValue.SetCurrentCompanySequence(companyID, sequenceKey, newValue);
                this.status = 1;
                this.message = $"Success";
                return;
            }
            else
            {
                status = 2;
                this.message = $"curValue:{curValue} != expectedCurValue:{expectedCurValue}.";
                return;
            }
        }
    };