We have about 30 years of serial number data from our old equipment that I’m bringing into Epicor. I’ve been asked to add the data and create a screen to enter new equipment and save new serial numbers with it. We have chosen not to use Epicor’s serial numbering and go with data populating a UD table. I have much of it working the way we want to see it. I created a new form using UD02 and Key1 for the serial number (SN). When I click New, enter a part number and the SN field populates with the next number in the series and I save. There’s more there but that’s the important parts.
My problem is when I close the form and reopen it to add more equipment the SN goes back to my starting number instead of the next number available in the series. I’m not good at C# and my code is a bit cobbled together. Could someone look at it and tell me what I’m missing? TIA.
// **************************************************
// Custom code for UD02Form
// Created: 4/23/2025
// **************************************************
extern alias Erp_Adapters_SalesOrder;
extern alias Erp_Adapters_Customer;
extern alias Erp_Contracts_BO_Part;
extern alias Erp_Contracts_BO_SalesOrder;
extern alias Erp_Contracts_BO_Customer;
using System;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Windows.Forms;
using Ice.BO;
using Ice.UI;
using Ice.Lib;
using Ice.Adapters;
using Ice.Lib.Customization;
using Ice.Lib.ExtendedProps;
using Ice.Lib.Framework;
using Ice.Lib.Searches;
using Ice.UI.FormFunctions;
using Infragistics.Win;
using Ice.Adapters;
using Erp.Adapters;
public class Script
{
private EpiDataView edvUD02;
private bool isSettingSerial = false;
public void InitializeCustomCode()
{
this.UD02_Column.ColumnChanged += new DataColumnChangeEventHandler(this.UD02_AfterFieldChange);
this.edvUD02 = (EpiDataView)(this.oTrans.EpiDataViews["UD02"]);
this.edvUD02.dataView.ListChanged += new ListChangedEventHandler(this.edvUD02_AfterRowChange);
SetExtendedProperties();
}
public void DestroyCustomCode()
{
this.UD02_Column.ColumnChanged -= new DataColumnChangeEventHandler(this.UD02_AfterFieldChange);
this.edvUD02.dataView.ListChanged -= new ListChangedEventHandler(this.edvUD02_AfterRowChange);
}
private void SetExtendedProperties()
{
if (edvUD02.dataView.Table.Columns.Contains("Date02"))
{
edvUD02.dataView.Table.Columns["Date02"].ExtendedProperties["ReadOnly"] = false;
}
}
private void UD02_AfterFieldChange(object sender, DataColumnChangeEventArgs args)
{
EpiDataView edv = (EpiDataView)oTrans.EpiDataViews["UD02"];
switch (args.Column.ColumnName)
{
case "ShortChar01": // PartNum
string partNum = args.ProposedValue.ToString();
if (!string.IsNullOrEmpty(partNum))
{
var partAdapter = new Erp.Adapters.PartAdapter(oTrans);
partAdapter.BOConnect();
if (partAdapter.GetByID(partNum))
{
string desc = partAdapter.PartData.Part[0].PartDescription ?? "";
if (desc.Length > 150) desc = desc.Substring(0, 150);
edv.dataView[edv.Row]["Character02"] = desc;
}
partAdapter.Dispose();
}
break;
case "ShortChar06": // OrderNum
string orderNumStr = args.ProposedValue.ToString();
if (!string.IsNullOrEmpty(orderNumStr))
{
int orderNum;
if (int.TryParse(orderNumStr, out orderNum))
{
var orderAdapter = new Erp.Adapters.SalesOrderAdapter(oTrans);
orderAdapter.BOConnect();
if (orderAdapter.GetByID(orderNum))
{
var order = orderAdapter.SalesOrderData.OrderHed[0];
string poNum = order.PONum ?? "";
DateTime reqDate = order.RequestDate;
int custNum = order.CustNum;
edv.dataView[edv.Row]["ShortChar04"] = poNum;
edv.dataView[edv.Row]["Date01"] = reqDate;
var custAdapter = new Erp.Adapters.CustomerAdapter(oTrans);
custAdapter.BOConnect();
if (custAdapter.GetByID(custNum))
{
string custName = custAdapter.CustomerData.Customer[0].Name ?? "";
edv.dataView[edv.Row]["Character01"] = custName;
}
custAdapter.Dispose();
}
orderAdapter.Dispose();
}
}
break;
case "Key1": // Serial number lookup
string serialNumber = args.Row["Key1"].ToString().Trim();
if (!string.IsNullOrEmpty(serialNumber))
{
if (edv.Row >= 0)
{
string rowMod = edv.dataView[edv.Row]["RowMod"].ToString();
if (rowMod != "A" && !isSettingSerial)
{
var ud02Adapter = new Ice.Adapters.UD02Adapter(oTrans);
ud02Adapter.BOConnect();
bool found = ud02Adapter.GetByID(
"IKI",
serialNumber,
"", "", "", "", ""
);
if (found)
{
foreach (DataColumn col in ud02Adapter.UD02Data.UD02.Columns)
{
edv.dataView[edv.Row][col.ColumnName] = ud02Adapter.UD02Data.UD02[0][col];
}
}
else
{
MessageBox.Show(
"Serial Number not found.\nClick 'New' to create a new serial number entry.",
"Not Found",
MessageBoxButtons.OK,
MessageBoxIcon.Information
);
}
ud02Adapter.Dispose();
}
}
}
break;
}
}
private void edvUD02_AfterRowChange(object sender, ListChangedEventArgs args)
{
TryAutoGenerateSerial();
}
private void TryAutoGenerateSerial()
{
if (edvUD02 != null && edvUD02.Row >= 0)
{
string rowMod = edvUD02.dataView[edvUD02.Row]["RowMod"].ToString();
if (rowMod == "A" && string.IsNullOrWhiteSpace(edvUD02.dataView[edvUD02.Row]["Key1"].ToString()))
{
isSettingSerial = true;
int startingSerial = 19310;
int nextSerial = startingSerial;
foreach (DataRow dr in edvUD02.dataView.Table.Rows)
{
if (dr.RowState != DataRowState.Deleted && !string.IsNullOrEmpty(dr["Key1"].ToString()))
{
int existingSerial;
if (int.TryParse(dr["Key1"].ToString(), out existingSerial))
{
if (existingSerial >= nextSerial)
{
nextSerial = existingSerial + 1;
}
}
}
}
edvUD02.dataView[edvUD02.Row]["Key1"] = nextSerial.ToString();
EpiTextBox txtKey1 = (EpiTextBox)csm.GetNativeControlReference("txtKeyField");
if (txtKey1 != null)
{
txtKey1.ReadOnly = true;
}
isSettingSerial = false;
}
}
}
}'''