Change button to read only based on field value

Hello,
I am customizing the AR invoice entry screen so that users can click a button that will open up a customer payment portal. With help from Jose I was able to get the button working but I would like to make it read only on load and when a record is selected the field “CustPortal_c” is checked, if empty the button should be read only if not its enabled.
I’ve read a few forums and can’t use a row rule because AR entry doesn’t a text field to not be read only. Here’s my stab at this

// **************************************************
// Custom code for ARInvoiceForm
// Created: 2/12/2021 1:11:07 PM
// **************************************************

extern alias Erp_Contracts_BO_ARInvoice;
extern alias Erp_Contracts_BO_ARPromissoryNotes;
extern alias Erp_Contracts_BO_ARInvSearch;
extern alias Erp_Contracts_BO_ARInvcDtlSearch;
extern alias Erp_Contracts_BO_Company;
extern alias Erp_Contracts_BO_Customer;
extern alias Erp_Contracts_BO_Part;
extern alias Erp_Adapters_Customer;

using System;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Windows.Forms;
using Erp.Adapters;
using Erp.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 EpiDataView edvCustomer;

	private EpiDataView edvInvcHead;
	// 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

		this.edvCustomer = ((EpiDataView)(this.oTrans.EpiDataViews["Customer"]));
		this.edvInvcHead = ((EpiDataView)(this.oTrans.EpiDataViews["InvcHead"]));
		this.edvInvcHead.EpiViewNotification += new EpiViewNotification(this.edvInvcHead_EpiViewNotification);
		// End Wizard Added Variable Initialization
		
		// Begin Wizard Added Custom Method Calls
		this.btnCustPortal.Click += new System.EventHandler(this.btnCustPortal_Click);

		// 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

		this.btnCustPortal.Click -= new System.EventHandler(this.btnCustPortal_Click);
		
		this.edvInvcHead.EpiViewNotification -= new EpiViewNotification(this.edvInvcHead_EpiViewNotification);
		this.edvInvcHead = null;
		// End Wizard Added Object Disposal

		// Begin Custom Code Disposal

		// End Custom Code Disposal
	}
	
	private void ARInvoiceForm_Load(object sender, EventArgs args)
	{
		// Add Event Handler Code
		btnCustPortal.ReadOnly = true;
	}



	private void edvInvcHead_EpiViewNotification(EpiDataView view, EpiNotifyArgs args)
	{
		// ** Argument Properties and Uses **
		// view.dataView[args.Row]["FieldName"]
		// args.Row, args.Column, args.Sender, args.NotifyType
		// NotifyType.Initialize, NotifyType.AddRow, NotifyType.DeleteRow, NotifyType.InitLastView, NotifyType.InitAndResetTreeNodes
		if ((args.NotifyType == EpiTransaction.NotifyType.AddRow))
		{
			if ((args.Row > -1))
			{
				EpiDataView edvCustomer = ((EpiDataView)(this.oTrans.EpiDataViews["Customer"]));
				System.Data.DataRow edvCustomerRow = edvCustomer.CurrentDataRow;

				string cport = edvCustomerRow["CustPortal_c"].ToString();
				if ((!String.IsNullOrEmpty(cport)))
					{
						btnCustPortal.ReadOnly = true;
					}

				else
					{
						btnCustPortal.ReadOnly = false;
					}
			}
		}
	
	}

			
	private void btnCustPortal_Click(object sender, System.EventArgs args)
	{
		// ** Place Event Handling Code Here **		
		var myDv = oTrans.Factory("Customer");//Assuming Customer is the name of your Customer DataView
		string url=(string) myDv.dataView[myDv.Row]["CustPortal_c"];
	
		Process.Start(url); 			
		
	}
}

Row Rules will always be the best option. I would recommend that the field get mapped to something (like the web address of the customer portal?) so the row rule can apply logic to the field. However, you can use custom code as the action instead to disable the button control.

1 Like

I added a text box mapped to this field so I could monitor it’s value while testing.
Now with your suggestion i’ve added this, but I’m not sure how to disable the button in this

private void CreateRowRuleCustomerCustPortal_cEquals_x()
{
	// Description: btnDisable
	// **** begin autogenerated code ****
	ControlSettings controlSettings1EpiReadOnly = new ControlSettings();
	controlSettings1EpiReadOnly.SetStyleSetName("EpiReadOnly");
	RuleAction epireadonlyCustomer_CustPortal_c = RuleAction.AddControlSettings(this.oTrans, "Customer.CustPortal_c", controlSettings1EpiReadOnly);
	RuleAction[] ruleActions = new RuleAction[] {
			epireadonlyCustomer_CustPortal_c};
	// Create RowRule and add to the EpiDataView.
	RowRule rrCreateRowRuleCustomerCustPortal_cEquals_x = new RowRule("Customer.CustPortal_c", RuleCondition.Equals, "", ruleActions);
	((EpiDataView)(this.oTrans.EpiDataViews["Customer"])).AddRowRule(rrCreateRowRuleCustomerCustPortal_cEquals_x);
	// **** end autogenerated code ****
}

@Jason_Woods I found the answer after searching this again with your suggestion in mind.
I must have missed the unused field being used.

Rowrule enable/disable button click - ERP 10 - Epicor User Help Forum (epiusers.help)

Good news!

spoke too soon…AR invoice entry is proving difficult and I can’t seem to find a field that will not turn read only on this screen by default.

Likely this is not a good example if there isn’t already a field associated with the Customer Portal.
For this, you will need custom code in the Row Rule Action.

Ahh!! I got it!
After reading that the binding doesn’t have to be real I changed it and now its good.

// **************************************************
// Custom code for ARInvoiceForm
// Created: 2/16/2021 10:11:37 AM
// **************************************************

extern alias Erp_Contracts_BO_ARInvoice;
extern alias Erp_Contracts_BO_ARPromissoryNotes;
extern alias Erp_Contracts_BO_ARInvSearch;
extern alias Erp_Contracts_BO_ARInvcDtlSearch;
extern alias Erp_Contracts_BO_Company;
extern alias Erp_Contracts_BO_Customer;
extern alias Erp_Contracts_BO_Part;

using System;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Windows.Forms;
using Erp.Adapters;
using Erp.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 **

	// 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

		// End Wizard Added Variable Initialization

		// Begin Wizard Added Custom Method Calls

		this.epiButtonC1.Click += new System.EventHandler(this.epiButtonC1_Click);
		CreateRowRuleCustomerCustPortal_cEquals_x();;
		// 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

		this.epiButtonC1.Click -= new System.EventHandler(this.epiButtonC1_Click);
		// End Wizard Added Object Disposal

		// Begin Custom Code Disposal

		// End Custom Code Disposal
	}

	private void epiButtonC1_Click(object sender, System.EventArgs args)
	{
		// ** Place Event Handling Code Here **
		var myDv = oTrans.Factory("Customer");//Assuming Customer is the name of your Customer DataView
		string url=(string) myDv.dataView[myDv.Row]["CustPortal_c"];
	
		Process.Start(url); 
	}

	private void CreateRowRuleCustomerCustPortal_cEquals_x()
	{
		// Description: btnDisable
		// **** begin autogenerated code ****
		RuleAction disabledCustomer_btnCustPortal = RuleAction.AddControlSettings(this.oTrans, "Customer.btnCustPortal", SettingStyle.Disabled);
		RuleAction[] ruleActions = new RuleAction[] {
				disabledCustomer_btnCustPortal};
		// Create RowRule and add to the EpiDataView.
		RowRule rrCreateRowRuleCustomerCustPortal_cEquals_x = new RowRule("Customer.CustPortal_c", RuleCondition.Equals, "", ruleActions);
		((EpiDataView)(this.oTrans.EpiDataViews["Customer"])).AddRowRule(rrCreateRowRuleCustomerCustPortal_cEquals_x);
		// **** end autogenerated code ****
	}

}