Muralidharan Ramakrishnan Muralidharan Ramakrishnan - 3 months ago 7
C# Question

How to override the default terms update from converting Opportunity to Sales Order

I have added custom fields Terms and Ship via in opportunity and when i pass the value to sales order. It is replaced with default terms & ship via.

The following code I am using the pass the value from Opportunity to Sales Order

public virtual IEnumerable CreateSalesOrder(PXAdapter adapter)
{

PXGraph.InstanceCreated.AddHandler<SOOrderEntry>((graph) =>
{
graph.RowInserted.AddHandler<SOOrder>((cache, args) =>
{
var soOrder = (SOOrder)args.Row;
var soOrderExt = PXCache<SOOrder>.GetExtension<SOOrderExtNV>(soOrder);

foreach (CROpportunity opportunity in adapter.Get())
{
OppertunityExtn extOpprow = PXCache<CROpportunity>.GetExtension<OppertunityExtn>(opportunity);
soOrderExt.UsrOpportunityID = opportunity.OpportunityID;
soOrderExt.UsrCustomerContact = opportunity.ContactID;
soOrderExt.UsrIsEmbroidery = extOpprow.UsrIsEmbroidery;
soOrderExt.UsrIsPromo = extOpprow.UsrIsPromo;
soOrderExt.UsrIsBlank = extOpprow.UsrIsBlank;
soOrderExt.UsrIsService = extOpprow.UsrIsService;
soOrderExt.UsrIsDigital = extOpprow.UsrIsDigital;
soOrderExt.UsrIsSample = extOpprow.UsrIsSample;
soOrderExt.UsrIsScreenPrint = extOpprow.UsrIsScreenPrint;
soOrderExt.UsrIsRushJob = extOpprow.UsrIsRushJob;
soOrderExt.UsrHardDate = extOpprow.UsrHardDate;
soOrderExt.UsrIsInHandsDate = extOpprow.UsrIsInHandsDate;
soOrderExt.UsrEventDate = extOpprow.UsrEventDate;
soOrderExt.UsrEventDescription = extOpprow.UsrEventDescription;
soOrderExt.UsrShipDate = extOpprow.UsrShipDate;
soOrderExt.UsrHoldUntil = extOpprow.UsrHoldUntil;
soOrderExt.UsrExactQuantityNeeded = extOpprow.UsrExactQuantityNeeded;
soOrderExt.UsrIsNewCustomer = extOpprow.UsrIsNewCustomer;
soOrderExt.UsrBatchShip = extOpprow.UsrBatchShip;
soOrderExt.UsrInternalRef = extOpprow.UsrInternalRef;
soOrder.ShipVia = extOpprow.UsrShipVia;
soOrder.TermsID = extOpprow.UsrTerms;
soOrder.FreightAmt = extOpprow.UsrFreightEstimate;

}
});
graph.RowPersisting.AddHandler<SOOrder>((cache, args) =>
{
var soOrder = (SOOrder)args.Row;
foreach (CROpportunity opportunity in adapter.Get())
{
OppertunityExtn extOpprow = PXCache<CROpportunity>.GetExtension<OppertunityExtn>(opportunity);
soOrder.TermsID = extOpprow.UsrTerms;
}

});
});
return Base.createSalesOrder.Press(adapter);
}

Answer

TermsID is replaced after SOOrder row is inserted. You could refer below code of OpportunityMaint.DoCreateSalesOrder. That’s why it is replaced with value set at Customer’s level.

enter image description here

You should implement logic as below. And you could use similar approach for user fields as well.

public class OpportunityMaintPXExt : PXGraphExtension<OpportunityMaint>
{
    public override void Initialize()
    {
        PXGraph.InstanceCreated.AddHandler<SOOrderEntry>((graph) =>
        {
            graph.RowUpdating.AddHandler<SOOrder>((sender, e) =>
            {
                SOOrder soData = e.NewRow as SOOrder;
                if (soData != null)
                {
                    if (sender.GetStatus(soData) == PXEntryStatus.Inserted)
                    {
                        CROpportunity opData = Base.Opportunity.Current;
                        CROpportunityPXExt opDataExt = PXCache<CROpportunity>.GetExtension<CROpportunityPXExt>(opData);

                        soData.TermsID = opDataExt.UsrTermsID;
                        soData.ShipVia = opDataExt.UsrShipVia;
                    }
                }
            });
        });
    }
}
Comments