Validate Custom Field =- Code example?

That’s odd.

One thing I didn’t mention is that the default RetriverCombo will let you enter text that is not one of the values retrieved. You’ll need to add a On Form Load function to tweak the control to prevent manually entering another value

That makes a lot more sense! That’s exactly what I was doing when I started that thread about field validation on leave. Read through that thread (yeah I know it’s super long, but it explains things pretty well)

You will want a method directive BPM on on the update method of UD04. This is the easiest way to check on save. This will make it so you can’t mess it up on entry. You don’t need any code to make that work. You just do conditions that run a query to make sure the ID exists.

Then for ease of use you can add the customization as outlined in that thread. That gives you some soft validation, you can make hard validations, but you might piss off users as it becomes a PITA. That’s why I did soft validations with the customization (just warning messages) and then the hard validation on the BPM.

Read through those and get started, then when you get stuck on some details, let us know.

I have also seen this:

// Add as a global variable inside class Script
EpiTextBox txtKeyField;

// Add to your InitializeCustomCode()
this.txtKeyField = ((EpiTextBox)csm.GetNativeControlReference("46567b2e-6bc0-4967-be35-a0ec6843838f")); // REPLACE GUID with your GUID
txtKeyField.Validating += new CancelEventHandler(keyFieldValidating);

// Add to DestroyCustomCode
txtKeyField.Validating -= new CancelEventHandler(keyFieldValidating);

// Add method inside class Script
private void keyFieldValidating(object sender, CancelEventArgs e)
{
	// Example 1 (straight to the point)
	if (!string.IsNullOrEmpty(this.txtKeyField.Text.Trim()) )
	{
		// It has a value yay
	}
	else {
		e.Cancel = true; // failed validation you can throw a messagebox
	}

	// Example 2
    if (sender is Ice.Lib.Framework.EpiTextBox)
    {
        Ice.Lib.Framework.EpiTextBox tb = (Ice.Lib.Framework.EpiTextBox)sender;
        e.Cancel = !trans.ValidateKeyField(tb);
    }
}

And someone using Key Events to Validate

private void txtDesc_KeyDown(object sender, System.Windows.Forms.KeyEventArgs args)
{
	if (args.KeyCode == Keys.Tab | args.KeyCode == Keys.Enter)
	{
		string desc = this.txtDesc.Text;
		if (desc == "") {
			return;
		}

		string key1 = SearchForPrograms(desc);

		if (key1 == "") {
			return;
		}

		string key2 = String.Empty;
		string key3 = String.Empty;
		string key4 = String.Empty;
		string key5 = String.Empty;

		// Call Adapter method
		bool result = this.oTrans.GetByID(key1, key2, key3, key4, key5);
	}
}

Also one quick thing to note on UD Key Fields, Epicor requires them all behind the scenes, unless! you use Extended Properties and mark them as “ReadOnly” then Epicor’s Engine will not require them and actually prompt you for a new Record as soon as you have the non read-only Key fields populated.

Using Extended Properties Mark the ones you don’t need as ReadOnly and IsHidden, otherwise they will show up again.

// Example:

if (edvUD100.dataView.Table.Columns.Contains("Key4"))
{
	// Begin Wizard Added ExtendedProperty Settings: edvUD100-Key4
	edvUD100.dataView.Table.Columns["Key4"].ExtendedProperties["IsHidden"] = true;
	edvUD100.dataView.Table.Columns["Key4"].ExtendedProperties["ReadOnly"] = true;
	// End Wizard Added ExtendedProperty Settings: edvUD100-Key4
}
if (edvUD100.dataView.Table.Columns.Contains("Key5"))
{
	// Begin Wizard Added ExtendedProperty Settings: edvUD100-Key5
	edvUD100.dataView.Table.Columns["Key5"].ExtendedProperties["IsHidden"] = true;
	edvUD100.dataView.Table.Columns["Key5"].ExtendedProperties["ReadOnly"] = true;
	// End Wizard Added ExtendedProperty Settings: edvUD100-Key5
}
2 Likes

Along the lines of @Banderson solution, you can do pretty easily in a code block on same method similar to:

foreach(var ud10 in ttUD10.Where( u => u.Added()))
{
  if(!Db.Customer.Where(c => c.CustID == ud10.Key2).Any())
  {
     throw new BLException("Hey! This aint no customer of mine!");
  }
}

Cant remember if CustID is a string or int, but assuming its an int, you’d have to do this instead:

  if(!Db.Customer.Where(c => c.CustID.ToString() == ud10.Key2).Any())
2 Likes

Is there a downside to simply adding the RetriverCombo?
(Aside from the OP’s issue with the adapter not found when running the customization) :slight_smile:

Just:

  1. Add the Retriever
  2. Select the appropriate connection in the Retriever Combo List
  3. Bind it to the desired _UD field
  4. Add an FormLoad event with: custIDComboC1.LimitToList = true; in the function.

That last part limits you from leaving the control if the value typed isn’t valid.

Thanks - I’ve read your thread but am not much wiser, I’ve tried to enter your code bt amend it to fit my needs but I just get errors that don’t explain what the issue is.

On your advice above, to do a Method DirectiveBPM, would this be pre or post processing?

Pre - you have to stop the method BEFORE it completes.

I would add with all due respect, you may consider getting a consultant to make your changes (or to give you guidance). You can break lots of things very easily with code or even BPMs.

Hi
Do you know what all arguments of CRMCallArgs mean?