0
votes

I have a custom line number field in opportunity product tab for customer to re-sequence the selected products and the grid is sorted on custom field value.

I am trying to pass the value from opportunity to sales order which also having a similar field.

the following code i have tried and it did not work

            PXGraph.InstanceCreated.AddHandler<SOOrderEntry>((graph) =>
            {
                graph.RowUpdated.AddHandler<SOLine>((cache, args) =>
                    {

                        CROpportunityProducts product = (adapter.View.Graph as OpportunityMaint).Products.Current;
                        CROpportunityProductsExtNV productext = PXCache<CROpportunityProducts>.GetExtension<CROpportunityProductsExtNV>(product);
                        SOLine soline = (SOLine)args.Row;
                        SOLineExtNV solineext = PXCache<SOLine>.GetExtension<SOLineExtNV>(soline);
                        solineext.UsrLineNo = productext.UsrLineNo;

                    });
            });

The following piece of code returns same value for all line numbers

2

2 Answers

2
votes

You can implement RowInserting Event handler as below:

graph.RowInserting.AddHandler<SOLine>((cache, args) =>
{
    var soLine = (SOLine)args.Row;
    CROpportunityProducts opProduct = PXResult<CROpportunityProducts>.Current;
    SOLineExtNV soLineExt = PXCache<SOLine>.GetExtension<SOLineExtNV>(soLine);
    CROpportunityProductsExtNV opProductExt = PXCache<CROpportunityProducts>.GetExtension<CROpportunityProductsExtNV>(opProduct);
    soLineExt.UsrLineNo = opProductExt.UsrLineNo;
});
1
votes

wish they could split up the call to create the order and the call to insert the lines to make it easier to customize. We have done something similar. Here is a sample from what I tested using a graph extension and overriding the DoCreateSalesOrder call in the opportunitymaint graph. (This assumes the select on products is the same order the transaction on the sales order were inserted. I am sure there could be a better answer, but this is an example I have handy.)

public class CROpportunityMaintExtNV : PXGraphExtension<OpportunityMaint>
{
    [PXOverride]
    public virtual void DoCreateSalesOrder(Action del)
    {
        try
        {
            del();
        }
        catch (PXRedirectRequiredException redirect)
        {
            var products = this.Products.Select().ToArray();

            int rowCntr = 0;
            foreach (SOLine soLine in ((SOOrderEntry)redirect.Graph).Transactions.Select())
            {
                // Assumes inserted rows in same order as products listed (default should be the key)

                //Current product
                CROpportunityProducts currentProduct = products[rowCntr];
                var productExtension = currentProduct.GetExtension<CROpportunityProductsExtNV>();

                ((SOOrderEntry) redirect.Graph).Transactions.Cache.SetValueExt<SOLineExtNV.usrLineNo>(soLine, productExtension.UsrLineNo);

                rowCntr++;
            }

            throw redirect;
        }
    }
}

The problem you had with your code is the Current product was always the same which resulted in the same value.