Preventing Auto Assigned Customer ID Generated with NextValue from Incrementing on Save Error

Hi! So let me preface that I am very new to EPICOR. I’ve been following some threads on here about auto assigning a customer ID and I have followed along with the YT videos and links and have that working using the following:

Customer.GetNewCustomer POST Processing: Have a simple Set Field Widget to set ds.Customer.CustID field of the changed row to “AutoAssign” expressions.

Then in Customer.Update PRE Processing I have a condition to check if ds.Customer.CustID is equal to “AutoAssign” and then if it is I can implement my set field and change it to new Ice.Lib.NextValue(Db).GetNextSequence(“CustIDSequence”).ToString().

My issues lies in when you create a customer and hit save but say you forgot to enter a Name, you get an error. Then once you finally save after fixing the error, the GetNextSequence was called so your counter that should have been incremented by 1 has been incremented by the number of times you saved.

What is the best method to go about either grabbing the max CustID so I can set the next customer to +1 of that, or to detect when there is an error from another field and either not increment/ set the NextValue sequence. I have gotten GetNextSequence(), SetSequenceCurrentValue(), and GetSequenceCurrentValue() to work but cannot get a foreach to grab the MaxID from ds.Customer.CustID without some sort of error.

Thanks in advance!

Hi
I create a User Code called “Next” and then within there setup child records for each type of Auto-Assign that i want to do (e.g. Cust, Part, Supp etc.) where the Long Description holds the first number I want to use/assign:

Then I do the Method Directives as you detail, however on the Pre-Processing directive on Customer.Update I just have a Custom Code widget containing the following code:

Ice.Tables.UDCodes UDCodes = null;

foreach (var MyCustID in (from ThisCustID in ds.Customer where
    ThisCustID.RowMod ==  IceRow.ROWSTATE_ADDED &&
    ThisCustID.CustID ==  "AutoAssign"
    select ThisCustID))
    {
        using (var txScope = IceContext.CreateDefaultTransactionScope())
        {
          foreach(var MYUDCodes in (from ThisUDCodes in Db.UDCodes.With(LockHint.UpdLock) where
          ThisUDCodes.Company == Session.CompanyID &&
          ThisUDCodes.CodeTypeID == "Next" &&
          ThisUDCodes.CodeID == "CUSTID"
          select ThisUDCodes))
          {
            int NextCustID;
            int length = 6;
            int.TryParse(MYUDCodes.LongDesc,out NextCustID); 
            MyCustID.CustID = "C" + NextCustID.ToString().PadLeft(length,'0'); 
            NextCustID += 1; //Increment the next number
            MYUDCodes.LongDesc = NextCustID.ToString(); //save the new next id in UDCodes
          }
        Db.Validate();
        txScope.Complete();
        }
    }

This grabs the current number in the UD Code, pads it out with leading zeros and appends a pre-fix before going and incrementing the UD Code by 1

2 Likes

Once you call next sequence assign it to the customer.custid field at that point it
Should no longer be set to “autoassign” so this BPM shouldn’t trigger any more. (make sure you have a condition that says only auto assign if custid =“autoassign”)

Then when you finally save it will use the sequence it generates the first time

Also in the grand scheme of things it doesn’t matter numbers are infinite! But you are doing it correctly by using a sequence.

2 Likes

Thank you, I actually got it working via using a data tag that I set when I GetNewCustomer and remove right after incrementing the sequence so that continual errored saves don’t increment it/ it will only get incremented once after a new customer is created.

I did have one question just to better understand the way EPICOR is working on Update. So when I Click save on a customer, Update is run. If there is an error though or a 400 Bad Request, these changes I made will not get stored to the DB will it? So the CustID will not get written to in the DB until a successful update or 200 return meaning the CustID will still show as AutoAssign?

Just wondering more about exactly what you meant by ‘assign it to the customer.custid field at that point’ since I do run a Set Field and it doesn’t update yet because of the 400 Bad Request.

Just want to come here and say thank you so much for this! Really clean and efficient in my opinion to increment in the description.