Solved

Conditionally enable/disable Custom Command Button

  • 14 November 2023
  • 6 replies
  • 87 views

Userlevel 1
Badge

I would like to enable / disable the command button below ASSEMBLE KITS REQUEST based on the checkbox Auto Assemble Kits (UsrAutoAssembleKits). The command button is linked to an action and working correctly thanks to @Naveen Boga.. I assume its connected with the Field_Updated event. the ID for the command button is CMD3

namespace PX.Objects.SO
{
public class SOOrderEntry_Extension : PXGraphExtension<PX.Objects.SO.SOOrderEntry>
{


public PXAction<PX.Objects.SO.SOOrder> UsrKitsAssembleReq;

[PXButton(CommitChanges = true)]
[PXUIField(DisplayName = "Assemble Kits Request")]
public virtual IEnumerable usrKitsAssembleReq(PXAdapter adapter)
{
if (Base.Document.Current != null)
{

SOOrderExt soOrderExt = Base.Document.Current?.GetExtension<SOOrderExt>();
SOOrder soOrder = (SOOrder)Base.Caches[typeof(SOOrder)].Current;



if(soOrder.OrderNbr.Contains("NEW") == false){



if(soOrderExt.UsrKitsAssembleReq == null || soOrderExt.UsrKitsAssembleReq == false){
soOrderExt.UsrKitsAssembleReq = true;
Base.Document.Cache.Update(Base.Document.Current);
Base.Save.Press();
Base.Document.View.Ask("Autobuild Request Sent. Please refresh in several minutes.", MessageButtons.OK);
}



}
}
return adapter.Get();
}

public PXSelect<INRegister, Where<INRegister.tranDesc, Contains<Current<SOOrder.orderNbr>>, And<INRegisterExt.usrAutoBuild, Equal<True>>>> KitAssView;


#region Event Handlers



protected void SOLine_OrderQty_FieldUpdated(PXCache cache, PXFieldUpdatedEventArgs e)
{

if (e.Row == null) return;
if (Base.Document.Current == null) return;
var row = (SOLine)e.Row;
SOLineExt soLineExt = row.GetExtension<SOLineExt>();

if((decimal?)soLineExt.UsrAutoBuildReq2 > (decimal?)row.OrderQty)
{
soLineExt.UsrAutoBuildReq2 = (row.OrderQty ?? 0m);
}

}



protected void SOOrder_UsrAutoAssembleKits_FieldUpdated(PXCache cache, PXFieldUpdatedEventArgs e)
{

if (e.Row == null) return;

//var row = (SOLine)e.Row;

SOOrderExt1 soOrderExt = Base.Document.Current.GetExtension<SOOrderExt1>();
Base.Document.Cache.AllowUpdate = true;
Base.Transactions.Cache.AllowUpdate = true;

foreach (SOLine line in Base.Transactions.Select())
{
SOLineExt soLineExt = line.GetExtension<SOLineExt>();


if(soOrderExt.UsrAutoAssembleKits == true && line.IsKit == true )
{

soLineExt.UsrAutoBuildReq2 = Convert.ToDecimal(line.OrderQty ?? 0m);
PXUIFieldAttribute.SetEnabled<SOLineExt.usrAutoBuildReq2>(Base.Transactions.Cache, line, true);
}
else
{

soLineExt.UsrAutoBuildReq2 = null;
PXUIFieldAttribute.SetEnabled<SOLineExt.usrAutoBuildReq2>(Base.Transactions.Cache, line, false);
}


Base.Transactions.Update(line);
}

Base.Transactions.View.RequestRefresh();

}






protected void SOLine_UsrAutoBuildReq2_FieldUpdating(PXCache cache, PXFieldUpdatingEventArgs e)
{

if (e.Row == null) return;
if (Base.Document.Current == null) return;
var row = (SOLine)e.Row;
SOLineExt soLineExt = row.GetExtension<SOLineExt>();


if((decimal?)e.NewValue > (decimal?)row.OrderQty)
{
e.NewValue = (row.OrderQty ?? 0m);
}

}






protected void SOOrder_RowSelected(PXCache cache, PXRowSelectedEventArgs e, PXRowSelected InvokeBaseHandler)
{

if (e.Row == null) return;
InvokeBaseHandler?.Invoke(cache, e);
//var row = (SOLine)e.Row;

SOOrderExt1 soOrderExt = Base.Document.Current.GetExtension<SOOrderExt1>();
Base.Document.Cache.AllowUpdate = true;
Base.Transactions.Cache.AllowUpdate = true;

foreach (SOLine line in Base.Transactions.Select())
{
SOLineExt soLineExt = line.GetExtension<SOLineExt>();


if(soOrderExt.UsrAutoAssembleKits == true && line.IsKit == true )
{

//soLineExt.UsrAutoBuildReq2 = Convert.ToDecimal(line.OrderQty ?? 0m);
PXUIFieldAttribute.SetEnabled<SOLineExt.usrAutoBuildReq2>(Base.Transactions.Cache, line, true);
}
else
{

//soLineExt.UsrAutoBuildReq2 = null;
PXUIFieldAttribute.SetEnabled<SOLineExt.usrAutoBuildReq2>(Base.Transactions.Cache, line, false);
}

if((decimal?)soLineExt.UsrKitQtyAvail < (decimal?)soLineExt.UsrAutoBuildReq2)
{
PXUIFieldAttribute.SetWarning<SOLineExt.usrAutoBuildReq2>(Base.Transactions.Cache, line, "Warning, not enough kits available to autobuild. Component quantities may go negative.");
}
else
{
PXUIFieldAttribute.SetWarning<SOLineExt.usrAutoBuildReq2>(Base.Transactions.Cache, line, null);
}
//Base.Transactions.Update(line);
}

Base.Transactions.View.RequestRefresh();
}



protected virtual void _(Events.RowSelected<SOLine> e, PXRowSelected baseHandler)
{

// if (e.Row == null) return;
// baseHandler?.Invoke(e.Cache, e.Args);
// var row = (SOLine)e.Row;

// SOOrderExt soOrderExt = Base.Document.Current.GetExtension<SOOrderExt>();

// foreach (SOLine line in Base.Transactions.Select())
// {
// SOLineExt soLineExt = line.GetExtension<SOLineExt>();


// if(soOrderExt.UsrAutoAssembleKits == true && line.IsKit == true )
// {

// soLineExt.UsrAutoBuildReq2 = Convert.ToDecimal(line.OrderQty ?? 0m);
//soLineExt.UsrAutoBuildReq = 10;
// }
// else
// {

// soLineExt.UsrAutoBuildReq2 = null;
// }



// }


}










protected void SOLine_SiteID_FieldUpdated(PXCache cache, PXFieldUpdatedEventArgs e, PXFieldUpdated InvokeBaseHandler)
{
InvokeBaseHandler?.Invoke(cache, e);
var row = (SOLine)e.Row;
if (row != null && row.SiteID !=null && row.InventoryID != null)
{
SOLineExt soLineExt = row.GetExtension<SOLineExt>();

//Get the KitAssy record
AutoKitAssembly.KitAssy kitAssy = PXSelectReadonly<AutoKitAssembly.KitAssy,
Where<AutoKitAssembly.KitAssy.builtItemID, Equal<Required<AutoKitAssembly.KitAssy.builtItemID>>,
And<AutoKitAssembly.KitAssy.siteId, Equal<Required<AutoKitAssembly.KitAssy.siteId>>>>>
.Select(Base, row.InventoryID, row.SiteID);
if (kitAssy != null)
{
soLineExt.UsrKitQtyAvail = (kitAssy.QtyAvail ?? 0m);
}
else
{
soLineExt.UsrKitQtyAvail = null;
}
}
}

protected void SOLine_InventoryID_FieldUpdated(PXCache cache, PXFieldUpdatedEventArgs e, PXFieldUpdated InvokeBaseHandler)
{
InvokeBaseHandler?.Invoke(cache, e);
var row = (SOLine)e.Row;
if (row != null && row.SiteID !=null && row.InventoryID != null)
{
SOLineExt soLineExt = row.GetExtension<SOLineExt>();

//Get the KitAssy record
AutoKitAssembly.KitAssy kitAssy = PXSelectReadonly<AutoKitAssembly.KitAssy,
Where<AutoKitAssembly.KitAssy.builtItemID, Equal<Required<AutoKitAssembly.KitAssy.builtItemID>>,
And<AutoKitAssembly.KitAssy.siteId, Equal<Required<AutoKitAssembly.KitAssy.siteId>>>>>
.Select(Base, row.InventoryID, row.SiteID);
if (kitAssy != null)
{
soLineExt.UsrKitQtyAvail = (kitAssy.QtyAvail ?? 0m);
}
else
{
soLineExt.UsrKitQtyAvail = null;
}
}
}







#endregion
}
}

 

icon

Best answer by Naveen Boga 16 November 2023, 05:27

View original

6 replies

Userlevel 6
Badge +3

Well, I think UsrKitsAssembleReq is your Action linked to your button. You can enable / disable the action and it will take care of your button.

Userlevel 7
Badge +17

Hi @Dantheman88988  According to your requirement, the Button should be displayed at the GRID Header/ Toolbar button (Just like the Add Items, Line Details, and Item Availability buttons)

To enable/disable the grid button will not work with the enable/disable button code in the RowSelected event.

Please refer to the below link, it helps with your requirements. Let me know if you have any queries.

 

https://blog.zaletskyy.com/post/2015/01/27/post78

 

Userlevel 6
Badge +3

You can enable / disable your button in SOOrder_RowSelected:

UsrKitsAssembleReq.SetEnabled(soOrderExt.UsrAutoAssembleKits == true);

 

Userlevel 1
Badge

Thanks though I'm trying to enable / disable the command button not the checkbox?

Userlevel 1
Badge

Thanks that seems to work now though when the sales order form opens first on a new record its defaults to Enabled = true? Ive tried setting the property in the form editor to AutoCallback → Enabled = false but it still loads as enabled?

Userlevel 6
Badge +3

Can you show me how you disable it in RowSelected?

Reply


About Acumatica ERP system
Acumatica Cloud ERP provides the best business management solution for transforming your company to thrive in the new digital economy. Built on a future-proof platform with open architecture for rapid integrations, scalability, and ease of use, Acumatica delivers unparalleled value to small and midmarket organizations. Connected Business. Delivered.
© 2008 — 2024  Acumatica, Inc. All rights reserved