Hiding a Form Object Based on Current Company

Hello and thanks in advance from C# Noviceland. Been using and working with Epicor since v8 and am just now biting the bullet to learn a bit more about UI customizations and C#.

My objective here is to hide a field on a form based on the Current Company value. My first attempts were centered around using the Load Form Event, and I got as far as being able to detect the current company in an if statement and show a message. After poking around a bit here and elsewhere, I thought perhaps using a Rule Wizard would be a better approach–there seems to be a preference for using the Rule Wizard in such cases, though I am not sure why.

What I soon discovered is that if I created a rule that detected the current company (using the CallContextClientData view), I could not make an action that involves the field I want to hide–RevisonNum on the Order Detail view. So, I tried creating two rules, one for using CallContextClientData and the other using Order Detail and thought I might be able to mix and match in the script editor.

Here is what gets stubbed in from the Rule Wizard…

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

	// End Wizard Added Variable Initialization

	// Begin Wizard Added Custom Method Calls
	CreateRowRuleOrderDtlPOLineEquals_5();
	CreateRowRuleCallContextClientDataCurrentCompanyEquals_FOO();

	// End Wizard Added Custom Method Calls
}

(no change on public void DestroyCustomCode()

private void CreateRowRuleOrderDtlPOLineEquals_5()
{
	// Description: HidePartRev
	// **** begin autogenerated code ****

	RuleAction invisibleOrderDtl_RevisionNum = RuleAction.AddControlSettings(this.oTrans, "OrderDtl.RevisionNum", SettingStyle.Invisible);
	RuleAction[] ruleActions = new RuleAction[] {
			invisibleOrderDtl_RevisionNum};
	// Create RowRule and add to the EpiDataView.
	RowRule rrCreateRowRuleOrderDtlPOLineEquals_5 = new RowRule("OrderDtl.POLine", RuleCondition.Equals, 5, ruleActions);
	((EpiDataView)(this.oTrans.EpiDataViews["OrderDtl"])).AddRowRule(rrCreateRowRuleOrderDtlPOLineEquals_5);

	// **** end autogenerated code ****
}

private void CreateRowRuleCallContextClientDataCurrentCompanyEquals_FOO()
{
	// Description: CurrentCompany
	// **** begin autogenerated code ****		
	RuleAction errorCallContextClientData_CurrentUserId = RuleAction.AddControlSettings(this.oTrans, "CallContextClientData.CurrentUserId", SettingStyle.Error);
	RuleAction[] ruleActions = new RuleAction[] {
			errorCallContextClientData_CurrentUserId};

	// Create RowRule and add to the EpiDataView.
	RowRule rrCreateRowRuleCallContextClientDataCurrentCompanyEquals_FOO = new RowRule("CallContextClientData.CurrentCompany", RuleCondition.Equals, "FOO", ruleActions);
	((EpiDataView)(this.oTrans.EpiDataViews["CallContextClientData"])).AddRowRule(rrCreateRowRuleCallContextClientDataCurrentCompanyEquals_FOO);
	// **** end autogenerated code ****
}

And here is my failed attempt at accomplishing my objective…

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

	// End Wizard Added Variable Initialization

	// Begin Wizard Added Custom Method Calls
	CreateRowRuleCallContextClientDataCurrentCompanyEquals_FOO();

	// End Wizard Added Custom Method Calls
}

(no change on public void DestroyCustomCode()

private void CreateRowRuleCallContextClientDataCurrentCompanyEquals_FOO()
{
	// Description: CurrentCompany
	// **** begin autogenerated code ****
	RuleAction invisibleOrderDtl_RevisionNum = RuleAction.AddControlSettings(this.oTrans, "OrderDtl.RevisionNum", SettingStyle.Invisible);
	RuleAction[] ruleActions = new RuleAction[] {
			invisibleOrderDtl_RevisionNum};

	// Create RowRule and add to the EpiDataView.
	RowRule rrCreateRowRuleCallContextClientDataCurrentCompanyEquals_FOO = new RowRule("CallContextClientData.CurrentCompany", RuleCondition.Equals, "FOO", ruleActions);
	((EpiDataView)(this.oTrans.EpiDataViews["CallContextClientData"])).AddRowRule(rrCreateRowRuleCallContextClientDataCurrentCompanyEquals_FOO);
	// **** end autogenerated code ****
}

The code tests out in the Script Editor, but I get an error when I open the form:

Error Detail

Message: Exception has been thrown by the target of an invocation.
Inner Exception Message: Invalid RuleActions - can only set ControlSettings in current view
Program: CommonLanguageRuntimeLibrary
Method: InvokeMethod

I realize that I am crossing streams here and that I may be completely on the wrong track–my SQL/Procedural coding experience is a bit lost in this object-oriented world.

Any help pointing me in the right direction would be much appreciated.

I’ve accomplished this by simply adding some code during the Form Load event that looks at the session company ID

	private void SalesOrderForm_Load(object sender, EventArgs args)
	{

		if(oTrans.CoreSession.CompanyID== "YourCompanyID")
		{		
		     DoSomething();
		}
	}
2 Likes

Aaron is right…for instance to hide epiTextBoxC1

if(oTrans.CoreSession.CompanyID== “YourCompanyID”)
{
epiTextBoxC2.Visible = false;
}

I’d go ahead a mark Aarons answer as the solution, and Joseph shows a good example. I would add for the sake of you being new, that if you want to do something like Joseph shows to a “native” component - you will need to get a reference to it

EpiTextBox JobText = (EpiTextBox)csm.GetNativeControlReference("the guid of the JobText control");
1 Like

Thanks, all, for your assistance with this. Looks like I was on the right track when I first started by using the Load Form Event. Thanks to everyone’s help, I was able to implement this as required. To put it all together, the final solution looks like this:

private void SalesOrderForm_Load(object sender, EventArgs args)
{
// Add Event Handler Code
EpiCombo RevisionNum = (EpiCombo)csm.GetNativeControlReference(“e823047b-060d-4235-b9ee-bc3f2b6539a3”);
if(oTrans.CoreSession.CompanyID== “FOO”)
{
RevisionNum.Visible = false;
}
}