Skip to main content
Question

try to link credit memo from sale return in so module

  • Nov 12, 2026 9:45 AM UTC
  • 0 replies
  • 2 views
  • Translate All

Forum|alt.badge.img

I try to create customize code by relink credit memo in so module to sales return

but when click my customize action button show error 

this is my code: 

public PXAction<SOOrder> RestoreInvoiceLink;

[PXButton(CommitChanges = true)]
[PXUIField(
    DisplayName = "Restore Invoice Link",
    MapEnableRights = PXCacheRights.Update,
    MapViewRights = PXCacheRights.Select
)]
protected virtual IEnumerable restoreInvoiceLink(PXAdapter adapter)
{
    SOOrder order = Base.Document.Current;

    if (order == null)
        // Acuminator disable once PX1050 HardcodedStringInLocalizationMethod [Justification]
        throw new PXException("No Sales Order selected.");

    if (order.Status == SOOrderStatus.Completed ||
        order.Cancelled == true)
    {
        // Acuminator disable once PX1050 HardcodedStringInLocalizationMethod [Justification]
        throw new PXException(
            "This Sales Return cannot be restored."
        );
    }

    // Find the original shipment/invoice relationship.
    SOOrderShipment link =
        PXSelect<SOOrderShipment,
            Where<
                SOOrderShipment.orderType,
                Equal<Required<SOOrderShipment.orderType>>,
            And<
                SOOrderShipment.orderNbr,
                Equal<Required<SOOrderShipment.orderNbr>>>>>
        .Select(Base, order.OrderType, order.OrderNbr);

    if (link == null)
    {
        // Create missing SOOrderShipment relationship
        SOOrderShipment newLink = new SOOrderShipment
        {
            OrderType = order.OrderType,
            OrderNbr = order.OrderNbr,

            // These values must come from the original
            // shipment and Credit Memo relationship.
            ShipmentType = "I",
            ShipmentNbr = "<NEW>",
            CustomerID = order.CustomerID,
            CustomerLocationID = order.CustomerLocationID,
            ShipDate = order.ShipDate,
            ShipmentQty = order.OrderQty,
            ShipAddressID = order.ShipAddressID,
            ShipContactID = order.ShipContactID,
            Confirmed = true,
            Operation = "R",
            LineTotal = order.LineTotal,
            InvoiceNbr = order.CustomerOrderNbr,
            CreateINDoc = true,
            CreateARDoc = true,

            InvoiceType = ARDocType.CreditMemo
        };
        //order.Status = SOOrderStatus.Invoiced;
        Base.Caches<SOOrderShipment>().Insert(newLink);
        //Base.Save.Press();

        SOInvoiceEntry ie = PXGraph.CreateInstance<SOInvoiceEntry>();

        ARInvoice invoice = ARInvoice.PK.Find(
            ie,
            ARDocType.CreditMemo,
            order.CustomerOrderNbr
        );

        if (invoice == null)
            // Acuminator disable once PX1050 HardcodedStringInLocalizationMethod [Justification]
            throw new PXException("Original Credit Memo not found.");

        ie.Document.Current = invoice;
        // add arline here
        var lines = PXSelect<SOLine, Where<SOLine.orderType, Equal<Required<SOLine.orderType>>, And<SOLine.orderNbr, Equal<Required<SOLine.orderNbr>>>>>
                    .Select(Base, order.OrderType, order.OrderNbr)
                    .RowCast<SOLine>()
                    .ToList();
        foreach (SOLine line in lines)
        {
            ARTran tran = new ARTran
            {
                SOOrderType = line.OrderType,
                SOOrderNbr = line.OrderNbr,
                OrigInvoiceType = line.InvoiceType,
                OrigInvoiceNbr = line.InvoiceNbr,
                InventoryID = line.InventoryID,
                SiteID = line.SiteID,
                LocationID = line.LocationID,
                TranDesc = line.TranDesc,
                Qty = line.OrderQty,
                UOM = line.UOM,
                SOOrderLineNbr = line.LineNbr,

                UnitCost = line.UnitCost,
                UnitPrice = line.UnitPrice,                      
                CuryUnitPrice = line.CuryUnitPrice,
                CuryExtPrice = line.CuryExtPrice,

                TaxCategoryID = line.TaxCategoryID,

                AccountID = line.SalesAcctID,
                SubID = line.SalesSubID,
                BranchID = line.BranchID,
            };
            ie.Transactions.Insert(tran);
        }

        ie.Actions.PressSave();
    }

    return adapter.Get();
}