Search code examples
acumaticaacumatica-kb

Unable to Update Custom Field in Sales Quote Screen From Sales Order on an Action Event


I have created an Action in Sales Quotes screen to create a sales order. The sales order is getting created. I want the created Order Nbr to be populated in a custom field of Sales Quote Screen and the Action button to be disabled if the Sales order is created. Though I am not getting any error in the system. When I refresh the Sales Quote Screen I get the exact result, but not during the Click of Create SO Action.

I am not sure where I am going wrong. Please refer the code and Image below: Thanks. Following is my Code :

  public class QuoteMaint_Extension : PXGraphExtension<QuoteMaint>
  {

        #region Event Handlers

        protected void CRQuote_RowSelected(PXCache cache, PXRowSelectedEventArgs e)
        {
            var row = (CRQuote)e.Row;
            CRQuoteExt quoteExt = PXCache<CRQuote>.GetExtension<CRQuoteExt>(row);

            if (quoteExt.UsrOrderNbr != null)
            {
                createSalesOrder.SetEnabled(false);
            }
            else
            {
                createSalesOrder.SetEnabled(true);
            }
    }

        #endregion

        #region Create Sales Order

        public PXAction<CRQuote> createSalesOrder;
        [PXUIField(DisplayName = "Create SO", MapEnableRights = PXCacheRights.Update, MapViewRights = PXCacheRights.Update)]
        [PXProcessButton(CommitChanges = true)]

        public IEnumerable CreateSalesOrder(PXAdapter adapter)
        {
            QuoteMaint graphObject = PXGraph.CreateInstance<QuoteMaint>();

            foreach (CRQuote quote in adapter.Get())
            {
                //Create resultset for Quote Details
                PXResultset<CROpportunityProducts> PXSetLine = PXSelect<CROpportunityProducts,
                 Where<CROpportunityProducts.quoteID,
                 Equal<Required<CROpportunityProducts.quoteID>>>>.Select(this.Base, quote.QuoteID);

                List<CROpportunityProducts> QuoteList = new List<CROpportunityProducts>();

                foreach (CROpportunityProducts line in PXSetLine)
                {
                    QuoteList.Add(line);
                }

                PXLongOperation.StartOperation(this, delegate ()
                {
                    CreateSalesOrderMethod(quote, QuoteList);
                });

                yield return quote;
            }

        }
        //Private Method for Create Sales Order
        public virtual void CreateSalesOrderMethod(CRQuote quote, List<CROpportunityProducts> QuoteList)
        {
            //Base.Save.Press();
            bool var_orderCreated = false;
            bool erroroccured = false;
            string ErrMsg = "";

            SOOrderEntry orderGraphObjet = PXGraph.CreateInstance<SOOrderEntry>();
            SOOrder orderHeaderObject = new SOOrder();
            QuoteMaint currGRPH = PXGraph.CreateInstance<QuoteMaint>();
            var Extension = this.Base.GetExtension<QuoteMaint_Extension>();
            try
            {
                BAccount customer = PXSelect<BAccount, Where<BAccount.bAccountID, Equal<Current<CRQuote.bAccountID>>>>.Select(this.Base, quote.BAccountID);
                if (customer.Type == "CU")
                {
                    orderHeaderObject.CustomerID = quote.BAccountID;
                }
                else
                {
                    throw new PXException("Business Account not converted to Customer yet");
                }
                orderHeaderObject.CuryOrderTotal = quote.CuryProductsAmount;
                orderHeaderObject.CuryTaxTotal = quote.CuryTaxTotal;
                orderHeaderObject.OrderDesc = quote.Subject;

                orderHeaderObject = orderGraphObjet.CurrentDocument.Insert(orderHeaderObject);
                orderGraphObjet.CurrentDocument.Current = orderHeaderObject;
                orderGraphObjet.Actions.PressSave();

                orderHeaderObject = orderGraphObjet.CurrentDocument.Current;

                foreach (CROpportunityProducts tran in QuoteList)
                {

                    CROpportunityProductsExt xOppProductExt = PXCache<CROpportunityProducts>.GetExtension<CROpportunityProductsExt>(tran); 
                    SOLine transline = new SOLine();

                    SOLineExt _soLext = PXCache<SOLine>.GetExtension<SOLineExt>(transline);
                   
                    transline.OrderNbr = orderHeaderObject.OrderNbr;
                    transline.BranchID = orderHeaderObject.BranchID;
                    transline.InventoryID = tran.InventoryID;
                    transline.TranDesc = tran.Descr;
                    transline.UOM = tran.UOM;
                    transline.OrderQty = tran.Quantity;
                    transline.SiteID = tran.SiteID;
                    transline.CuryUnitPrice = tran.CuryUnitPrice;
                    transline.CuryExtPrice = tran.CuryExtPrice;
                    _soLext.UsrXSeqID = xOppProductExt.UsrXSequenceID;
                    _soLext.UsrXGroupID = xOppProductExt.UsrXGroupID;
                    _soLext.UsrInternalRemk = xOppProductExt.UsrInternalRemk;

                    orderGraphObjet.Transactions.Insert(transline);
                }
                orderGraphObjet.Actions.PressSave();
                var_orderCreated = true;
            }
            catch (Exception e)
            {
                erroroccured = true;
                ErrMsg = e.Message;
            }

            if (erroroccured)
            {
                throw new PXException("Cannot create Order: " + ErrMsg);
            }
            else
            {
                if(var_orderCreated)
                {
                    CRQuote QUOT = Base.Quote.Current;
                    CRQuoteExt QUOT_EXT = PXCache<CRQuote>.GetExtension<CRQuoteExt>(QUOT);
                    QUOT_EXT.UsrOrderNbr = orderHeaderObject.OrderNbr;
                    Base.Quote.Update(QUOT);
                    Base.Save.Press();
                }

            }
            

        }

        #endregion
    }
}

DAC FIELD :

#region UsrOrderNbr
        [PXDBString(50)]
        [PXUIField(DisplayName = "Sales Order Nbr", IsReadOnly = true)]
        [PXSelector(
           typeof(Search<SOOrder.orderNbr>),
           typeof(SOOrder.orderType),
           typeof(SOOrder.orderNbr),
           typeof(SOOrder.curyOrderTotal),
           typeof(SOOrder.status),
            Filterable = true)]

        public virtual string UsrOrderNbr { get; set; }
        public abstract class usrOrderNbr : PX.Data.BQL.BqlString.Field<usrOrderNbr> { }
        #endregion

enter image description here


Solution

  • The DAC extension is fetched from transLine object before the SOLine keys are defined. Try moving the GetExtension call after transLine is inserted in the dataview.

    SOLine transline = new SOLine();
    SOLineExt _soLext = PXCache<SOLine>.GetExtension<SOLineExt>(transline);
    

    The pattern should look like this:

    1. Create empty DAC object
    2. Insert DAC object in dataview
    3. Get DAC extension and assign custom field
    4. Update DAC object in dataview