0
votes

I have a need to modify the behavior of the Create Sales Order action on the Opportunity screen.

I'm trying to find the best / least intrusive method possible. I have a snippet of the base code below.

The changes I need to make are relatively simple. I need to populate some custom fields that exist on new SO.

What's the best approach?

public PXAction<CROpportunity> createSalesOrder;
[PXUIField(DisplayName = Messages.CreateSalesOrder, MapEnableRights = PXCacheRights.Update, MapViewRights = PXCacheRights.Select)]
[PXButton(ImageKey = PX.Web.UI.Sprite.Main.DataEntry)]
public virtual IEnumerable CreateSalesOrder(PXAdapter adapter)
{
    foreach (CROpportunity opportunity in adapter.Get<CROpportunity>())
    {
        Customer customer = (Customer)PXSelect<Customer, Where<Customer.bAccountID, Equal<Current<CROpportunity.bAccountID>>>>
            .SelectSingleBound(this, new object[] { opportunity });
        if (customer == null)
        {
            throw new PXException(Messages.ProspectNotCustomer);
        }

        if (CreateOrderParams.AskExtFullyValid((graph, viewName) => { }, DialogAnswerType.Positive))
        {
            Actions.PressSave();
            PXLongOperation.StartOperation(this, delegate()
            {
                var grapph = PXGraph.CreateInstance<OpportunityMaint>();
                grapph.Opportunity.Current = opportunity;
                grapph.CreateOrderParams.Current = CreateOrderParams.Current;
                grapph.DoCreateSalesOrder(CreateOrderParams.Current);
            });
        }
        yield return opportunity;
    }

}

protected virtual void DoCreateSalesOrder(CreateSalesOrderFilter param)
{
    bool recalcAny = param.RecalculatePrices == true ||
                     param.RecalculateDiscounts == true ||
                     param.OverrideManualDiscounts == true ||
                     param.OverrideManualDocGroupDiscounts == true ||
                     param.OverrideManualPrices == true;

    var opportunity = this.Opportunity.Current;
    Customer customer = (Customer)PXSelect<Customer, Where<Customer.bAccountID, Equal<Current<CROpportunity.bAccountID>>>>.Select(this);
1

1 Answers

0
votes

A simple override should accomplish your goal. Below is an example, which injects custom logic into the event handler SOOrder.RowInserted, during the Create Sales Order action. Then allow the action to finish normally. This approach extends the action with your custom logic, while retaining base code of the Action.

    public class OpportunityMaint_Extension : PXGraphExtension<OpportunityMaint>
{
    public delegate IEnumerable CreateSalesOrderDelegate(PXAdapter adapter);
    [PXOverride]
    public IEnumerable CreateSalesOrder(PXAdapter adapter, CreateSalesOrderDelegate baseMethod)
    {
        PXGraph.InstanceCreated.AddHandler<SOOrderEntry>((graph) =>
        {
            graph.RowInserted.AddHandler<SOOrder>((sender, e) =>
            {
                SOOrder order = (SOOrder)e.Row;
                SOOrderExt orderExt = PXCache<SOOrder>.GetExtension<SOOrderExt>(order);

                orderExt.UsrCustomOne = "Howdy";  //assign anything you want here
            });
        });

        return baseMethod(adapter);
    }
}

}