0
votes

Good day

I have a new field inside the CROpportunity Extenstion called usrGrossProfit. During CROpportunity's RowSelected it works out the values as needed. The problem I am having is that the users are using the create Quote button on the form and because of this never saves using the save button, The system does it for them. I have found that because of this the usrGrossProfit value is not saved.

Is there a way to force a save/Persist inside the RowSelected function?

protected void CROpportunity_RowSelected(PXCache cache, PXRowSelectedEventArgs e)
{
    try
    {
        var row = (CROpportunity)e.Row;
        if (row == null) return;
        CROpportunityExt SOE = PXCache<CROpportunity>.GetExtension<CROpportunityExt>(row);

        int total = 0;
        decimal TotalSales = 0;
        decimal TotalCost = 0;
        foreach (CROpportunityProducts item in this.Base.Products.Select())
        {
            total++;
            CROpportunityProductsExt2 itemExt = PXCache<CROpportunityProducts>.GetExtension<CROpportunityProductsExt2>(item);

            TotalCost += (decimal)itemExt.UsrCostPrice.Value * item.Qty.Value;
            TotalSales += (decimal)itemExt.UsrSellingprice * item.Qty.Value;

        }
        SOE.UsrGrossProfit = TotalSales - TotalCost;

        // I added this just to try and see if it helps
        cache.SetValueExt<CROpportunityExt.usrGrossProfit>(row, (decimal)(TotalSales - TotalCost));
        // we are not allowed to press the save button in the event Handler
        //this.Base.Save.Press();

    }
    catch (Exception ex)
    {
        PXTrace.WriteError(ex);
    }
}

I have also tried to override the CreateQuote Function but this doesn't work

public delegate IEnumerable CreateQuoteDelegate(PXAdapter adapter);
        [PXOverride]
        public IEnumerable CreateQuote(PXAdapter adapter, CreateQuoteDelegate baseMethod)
        {
            this.Base.Persist();
            return baseMethod(adapter);
        }

I have also made a business event to open and save the Opportunity also with no luck.

1
the issue may not be related to your logic but to the CROpportunity being a projection. Could you confirm if the record is saved when you press directly on Save? (dont invoke Save.Press in RowSelected. Test it from the UI perspective) - Fernando
Yes, it does save normally if use the button. I don't know what a Projection is? Is there a place I can read up a bit more? - JvD
It's the BQL equivalent of a SQL view (it could be used for other purposes as well though). Here is an article with more info: asiablog.acumatica.com/2018/03/… - Fernando

1 Answers

1
votes

No, you shouldn't save on row selected even if it was allowed. This is because row selected event gets fired several times and you don't want to be saving each time.

If you want to save on your CreateQuote override, try this:

Base.Save.PressButton(adapter)

Perhaps a better option, might be to force the user so that it's the user himself who saves. For example, you could check the state and throw an error in your override instead of saving.

if (Opportunity.Current != null && Opportunity.Cache.GetStatus(Opportunity.Current) == PXEntryStatus.Inserted)
{
     throw new PXException("Please save before proceeding");
}