UD40 Maintenance "Getting new"

Hi All,

I’m trying to use UD40 Maintenance to create a new Entry Screen.

I have added the columns to the UD Extended Maintenance for UD40. I was getting duplication errors as followed

image

so I assigned ReworkNum_c to Key5 but now I’m getting this, stuck on "Getting New…

Here is my code:

// **************************************************
// Custom code for UD40Form
// Created: 08/02/2021 11:58:41
// **************************************************
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;

public class Script
{
	// ** Wizard Insert Location - Do Not Remove 'Begin/End Wizard Added Module Level Variables' Comments! **
	// Begin Wizard Added Module Level Variables **

	private UD40Adapter _ud40Adapter;
	private EpiDataView _edvUD40;
	private DataTable ReworkUD40_Column;
	private string _Key1UD40;
	private string _Key2UD40;
	private string _Key3UD40;
	private string _Key4UD40;
	private string _Key5UD40;
	private DataView UD40_DataView;
	// End Wizard Added Module Level Variables **

	// Add Custom Module Level Variables Here **

	public void InitializeCustomCode()
	{
		// ** Wizard Insert Location - Do not delete 'Begin/End Wizard Added Variable Initialization' lines **
		// Begin Wizard Added Variable Initialization

		InitializeUD40Adapter();
		this._Key1UD40 = string.Empty;
		this._Key2UD40 = string.Empty;
		this._Key3UD40 = string.Empty;
		this._Key4UD40 = string.Empty;
		this._Key5UD40 = string.Empty;
		this.baseToolbarsManager.ToolClick += new Infragistics.Win.UltraWinToolbars.ToolClickEventHandler(this.baseToolbarsManager_ToolClickForUD40);
		this.UD40Form.BeforeToolClick += new Ice.Lib.Framework.BeforeToolClickEventHandler(this.UD40Form_BeforeToolClickForUD40);
		this.UD40Form.AfterToolClick += new Ice.Lib.Framework.AfterToolClickEventHandler(this.UD40Form_AfterToolClickForUD40);
		this.UD40_Row.EpiRowChanged += new EpiRowChanged(this.UD40_AfterRowChangeForUD40);
		this.UD40_DataView = this.UD40_Row.dataView;
		this.UD40_DataView.ListChanged += new ListChangedEventHandler(this.UD40_DataView_ListChangedForUD40);
		this.UD40_Row.BeforeResetDataView += new Ice.Lib.Framework.EpiDataView.BeforeResetDataViewDelegate(this.UD40_BeforeResetDataViewForUD40);
		this.UD40_Row.AfterResetDataView += new Ice.Lib.Framework.EpiDataView.AfterResetDataViewDelegate(this.UD40_AfterResetDataViewForUD40);
		// End Wizard Added Variable Initialization

		// Begin Wizard Added Custom Method Calls

		// End Wizard Added Custom Method Calls
	}

	public void DestroyCustomCode()
	{
		// ** Wizard Insert Location - Do not delete 'Begin/End Wizard Added Object Disposal' lines **
		// Begin Wizard Added Object Disposal

		if ((this._ud40Adapter != null))
		{
			this._ud40Adapter.Dispose();
			this._ud40Adapter = null;
		}
		this._edvUD40 = null;
		this.UD40_Column = null;
		this._Key1UD40 = null;
		this._Key2UD40 = null;
		this._Key3UD40 = null;
		this._Key4UD40 = null;
		this._Key5UD40 = null;
		this.baseToolbarsManager.ToolClick -= new Infragistics.Win.UltraWinToolbars.ToolClickEventHandler(this.baseToolbarsManager_ToolClickForUD40);
		this.UD40Form.BeforeToolClick -= new Ice.Lib.Framework.BeforeToolClickEventHandler(this.UD40Form_BeforeToolClickForUD40);
		this.UD40Form.AfterToolClick -= new Ice.Lib.Framework.AfterToolClickEventHandler(this.UD40Form_AfterToolClickForUD40);
		this.UD40_Row.EpiRowChanged -= new EpiRowChanged(this.UD40_AfterRowChangeForUD40);
		this.UD40_DataView.ListChanged -= new ListChangedEventHandler(this.UD40_DataView_ListChangedForUD40);
		this.UD40_DataView = null;
		this.UD40_Row.BeforeResetDataView -= new Ice.Lib.Framework.EpiDataView.BeforeResetDataViewDelegate(this.UD40_BeforeResetDataViewForUD40);
		this.UD40_Row.AfterResetDataView -= new Ice.Lib.Framework.EpiDataView.AfterResetDataViewDelegate(this.UD40_AfterResetDataViewForUD40);
		// End Wizard Added Object Disposal

		// Begin Custom Code Disposal

		// End Custom Code Disposal
	}

	private void InitializeUD40Adapter()
	{
		// Create an instance of the Adapter.
		this._ud40Adapter = new UD40Adapter(this.oTrans);
		this._ud40Adapter.BOConnect();

		// Add Adapter Table to List of Views
		// This allows you to bind controls to the custom UD Table
		this._edvUD40 = new EpiDataView();
		this._edvUD40.dataView = new DataView(this._ud40Adapter.UD40Data.UD40);
		this._edvUD40.AddEnabled = true;
		this._edvUD40.AddText = "New UD40";
		if ((this.oTrans.EpiDataViews.ContainsKey("UD40View") == false))
		{
			this.oTrans.Add("UD40View", this._edvUD40);
		}

		// Initialize DataTable variable
		this.UD40_Column = this._ud40Adapter.UD40Data.UD40;

		// Initialize EpiDataView field.
		this._edvUD40 = ((EpiDataView)(this.oTrans.EpiDataViews["UD40"]));

		// Set the parent view / keys for UD child view
		string[] parentKeyFields = new string[1];
		string[] childKeyFields = new string[1];
		parentKeyFields[0] = "ReworkNum_c";
		childKeyFields[0] = "Key5";
		this._edvUD40.SetParentView(this._edvUD40, parentKeyFields, childKeyFields);

		if ((this.oTrans.PrimaryAdapter != null))
		{
			// this.oTrans.PrimaryAdapter.GetCurrentDataSet(Ice.Lib.Searches.DataSetMode.RowsDataSet).Tables.Add(this._edvUD40.dataView.Table.Clone())
		}

	}

	private void GetUD40Data(string key1, string key2, string key3, string key4, string key5)
	{
		if ((this._Key1UD40 != key1) || (this._Key2UD40 != key2) || (this._Key3UD40 != key3) || (this._Key4UD40 != key4) || (this._Key5UD40 != key5))
		{
			// Build where clause for search.
			string whereClause = "Key1 = \'" + key1 + "\' And Key2 = \'" + key2 + "\' And Key3 = \'" + key3 + "\' And Key4 = \'" + key4 + "\'";
			System.Collections.Hashtable whereClauses = new System.Collections.Hashtable(1);
			whereClauses.Add("UD40", whereClause);

			// Call the adapter search.
			SearchOptions searchOptions = SearchOptions.CreateRuntimeSearch(whereClauses, DataSetMode.RowsDataSet);
			this._ud40Adapter.InvokeSearch(searchOptions);

			if ((this._ud40Adapter.UD40Data.UD40.Rows.Count > 0))
			{
				this._edvUD40.Row = 0;
			} else
			{
				this._edvUD40.Row = -1;
			}

			// Notify that data was updated.
			this._edvUD40.Notify(new EpiNotifyArgs(this.oTrans, this._edvUD40.Row, this._edvUD40.Column));

			// Set key fields to their new values.
			this._Key1UD40 = key1;
			this._Key2UD40 = key2;
			this._Key3UD40 = key3;
			this._Key4UD40 = key4;
			this._Key5UD40 = key5;
		}
	}

	private void GetNewUD40Record()
	{
		DataRow parentViewRow = this._edvUD40.CurrentDataRow;
		// Check for existence of Parent Row.
		if ((parentViewRow == null))
		{
			return;
		}
		if (this._ud40Adapter.GetaNewUD40())
		{
			string reworknum_c = parentViewRow["ReworkNum_c"].ToString();

			// Get unique row count id for Key5
			int rowCount = this._ud40Adapter.UD40Data.UD40.Rows.Count;
			int lineNum = rowCount;
			bool goodIndex = false;
			while ((goodIndex == false))
			{
				// Check to see if index exists
				DataRow[] matchingRows = this._ud40Adapter.UD40Data.UD40.Select("Key5 = \'" + lineNum.ToString() + "\'");
				if ((matchingRows.Length > 0))
				{
					lineNum = (lineNum + 1);
				} else
				{
					goodIndex = true;
				}
			}

			// Set initial UD Key values
			DataRow editRow = this._ud40Adapter.UD40Data.UD40.Rows[(rowCount - 1)];
			editRow.BeginEdit();
			editRow["Key1"] = reworknum_c;
			editRow["Key2"] = string.Empty;
			editRow["Key3"] = string.Empty;
			editRow["Key4"] = string.Empty;
			editRow["Key5"] = lineNum.ToString();
			editRow.EndEdit();

			// Notify that data was updated.
			this._edvUD40.Notify(new EpiNotifyArgs(this.oTrans, (rowCount - 1), this._edvUD40.Column));
		}
	}

	private void SaveUD40Record()
	{
		// Save adapter data
		this._ud40Adapter.Update();
	}

	private void DeleteUD40Record()
	{
		// Check to see if deleted view is ancestor view
		bool isAncestorView = false;
		Ice.Lib.Framework.EpiDataView parView = this._edvUD40.ParentView;
		while ((parView != null))
		{
			if ((this.oTrans.LastView == parView))
			{
				isAncestorView = true;
				break;
			} else
			{
				parView = parView.ParentView;
			}
		}

		// If Ancestor View then delete all child rows
		if (isAncestorView)
		{
			DataRow[] drsDeleted = this._ud40Adapter.UD40Data.UD40.Select("Key1 = \'" + this._Key1UD40 + "\' AND Key2 = \'" + this._Key2UD40 + "\' AND Key3 = \'" + this._Key3UD40 + "\' AND Key4 = \'" + this._Key4UD40 + "\'");
			for (int i = 0; (i < drsDeleted.Length); i = (i + 1))
			{
				this._ud40Adapter.Delete(drsDeleted[i]);
			}
		} else
		{
			if ((this.oTrans.LastView == this._edvUD40))
			{
				if ((this._edvUD40.Row >= 0))
				{
					DataRow drDeleted = ((DataRow)(this._ud40Adapter.UD40Data.UD40.Rows[this._edvUD40.Row]));
					if ((drDeleted != null))
					{
						if (this._ud40Adapter.Delete(drDeleted))
						{
							if ((_edvUD40.Row > 0))
							{
								_edvUD40.Row = (_edvUD40.Row - 1);
							}

							// Notify that data was updated.
							this._edvUD40.Notify(new EpiNotifyArgs(this.oTrans, this._edvUD40.Row, this._edvUD40.Column));
						}
					}
				}
			}
		}
	}

	private void UndoUD40Changes()
	{
		this._ud40Adapter.UD40Data.RejectChanges();

		// Notify that data was updated.
		this._edvUD40.Notify(new EpiNotifyArgs(this.oTrans, this._edvUD40.Row, this._edvUD40.Column));
	}

	private void ClearUD40Data()
	{
		this._Key1UD40 = string.Empty;
		this._Key2UD40 = string.Empty;
		this._Key3UD40 = string.Empty;
		this._Key4UD40 = string.Empty;
		this._Key5UD40 = string.Empty;

		this._ud40Adapter.UD40Data.Clear();

		// Notify that data was updated.
		this._edvUD40.Notify(new EpiNotifyArgs(this.oTrans, this._edvUD40.Row, this._edvUD40.Column));
	}

	private void baseToolbarsManager_ToolClickForUD40(object sender, Infragistics.Win.UltraWinToolbars.ToolClickEventArgs args)
	{
		// EpiMessageBox.Show(args.Tool.Key);
		switch (args.Tool.Key)
		{
			case "EpiAddNewNew UD40":
				GetNewUD40Record();
				break;

			case "ClearTool":
				ClearUD40Data();
				break;

			case "UndoTool":
				UndoUD40Changes();
				break;
		}
	}

	private void UD40Form_BeforeToolClickForUD40(object sender, Ice.Lib.Framework.BeforeToolClickEventArgs args)
	{
		// EpiMessageBox.Show(args.Tool.Key);
		switch (args.Tool.Key)
		{
			case "SaveTool":
				SaveUD40Record();
				break;
		}
	}

	private void UD40Form_AfterToolClickForUD40(object sender, Ice.Lib.Framework.AfterToolClickEventArgs args)
	{
		// EpiMessageBox.Show(args.Tool.Key);
		switch (args.Tool.Key)
		{
			case "DeleteTool":
				if ((args.Cancelled == false))
				{
					DeleteUD40Record();
				}
				break;
		}
	}

	private void UD40_AfterRowChangeForUD40(EpiRowChangedArgs args)
	{
		// ** add AfterRowChange event handler
		string reworknum_c = args.CurrentView.dataView[args.CurrentRow]["ReworkNum_c"].ToString();
		GetUD40Data(reworknum_c, string.Empty, string.Empty, string.Empty, string.Empty);
	}

	private void UD40_DataView_ListChangedForUD40(object sender, ListChangedEventArgs args)
	{
		// ** add ListChanged event handler
		string reworknum_c = UD40_DataView[0]["ReworkNum_c"].ToString();
		GetUD40Data(reworknum_c, string.Empty, string.Empty, string.Empty, string.Empty);
	}

	private void UD40_BeforeResetDataViewForUD40(object sender, EventArgs args)
	{
		// ** remove ListChanged event handler
		this.UD40_DataView.ListChanged -= new ListChangedEventHandler(this.UD40_DataView_ListChangedForUD40);
	}

	private void UD40_AfterResetDataViewForUD40(object sender, EventArgs args)
	{
		// ** reassign DataView and add ListChanged event handler
		this.UD40_DataView = this.UD40_Row.dataView;
		this.UD40_DataView.ListChanged += new ListChangedEventHandler(this.UD40_DataView_ListChangedForUD40);
	}
}





























































Thank you
Aaron.

What are you trying to do that the base UD40 form doesn’t already do?

Make “Rework Num” autogenerated? If so, you could do like Epicor does on native forms and have it default to 0. Then in a pre-proc update BPM, find the largest existing ReworkNum and add one to it.

Yes exactly that

How would I go about doing that in a PreProc?

Actualy… Its even easier than I thought (no need to make a record with “0” as the key).

No form customization required. It all happens automatically in the UD40.GetNewUD BPM.

The following uses your UD field ReworkNum_c for ordering the records (sorting them to find the last one). Then adds one to that last ReworkNum_c, and also sets Key1 as that value.

In a Post-Proc for GetNewUD:

  1. Create a variable of type Integer named ReworkNum

  2. Use a Set Arg/Var widget to set ReworkNum with the expression:

(Int32)((Db.UD40.Where( r =>r.Company == callContextClient.CurrentCompany)
  .Select( r => r.ReworkNum_c)
  .DefaultIfEmpty(0))
  .OrderByDescending(u => u)
  .First())
  1. Use a SetField widget to set ttUD40.Key1
    image
    using expression:
    (ReworkNum+1).ToString()

  2. Add another SetField widget:
    image
    with the expression
    ReworkNum+1
    (replace the Number01 from my test with your field ReworkNum_c)

During my testing, I used UD40.Number01 in place of your ReworkNum_c. So probaly don’t need the cast to type (Int32) in the Set Arg/Var widget.

1 Like

Hi Calvin,

I’m getting this.

image

You’ll probably need to cleanup the UD40 table first.

Make sure Key1 = your ReworkNum_c for each row.

If UD40 is currently blank, create one record (Key1 = “0”, and ReworkNum_c = 0)

And I would get that error if it failed once, and I failed to refresh before trying again. So while you’re testing, hit the Refresh button if you get that error.

EDIT

I was going with the assumption that ReworkNum_c will be unique. That is the case, right?

Also still stuck on this bleedy.

“Getting new…” do I clear out this custom code?

Yes. Try your BMP on an uncustomized UD40 form. (or one without the code)

If you still have problems, Start by just having the Set Arg/Var and a Show Message widgets, to make sure that Set Arg/Var is working

image