Skip to main content
Question

try to link credit memo from sale return in so module

  • Nov 12, 2026 9:45 AM UTC
  • 5 replies
  • 36 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();
}

5 replies

Forum|alt.badge.img+11
  • Captain II
  • Nov 17, 2026 4:39 PM UTC

What is the reasoning behind not using the ‘Add Invoice’ button?

Translate

Forum|alt.badge.img
  • Author
  • Freshman II
  • Nov 18, 2026 7:52 AM UTC

What is the reasoning behind not using the ‘Add Invoice’ button?

I used to prepare invoice first time from sale return to credit in so invoice but sometime I need to edit sale return in os. In credit memo in so invoice cannot add sale return back after I remove line of items. You can see details post here Cannot edit sale return in SO module | Community

Translate

Forum|alt.badge.img+11
  • Captain II
  • Nov 19, 2026 3:15 PM UTC

Do you have ‘Shipment Required’ set in your Order Type ‘SR’?

Translate

Forum|alt.badge.img
  • Author
  • Freshman II
  • Nov 20, 2026 2:03 AM UTC

Do you have ‘Shipment Required’ set in your Order Type ‘SR’?

When we prepare invoice, it’s automatically generated credit memo. I don’t see shipment.

Translate

Forum|alt.badge.img+11
  • Captain II
  • Nov 20, 2026 9:39 AM UTC

I think that Acumatica is looking for another reference to sales order line from the original invoice and because you manually changed the qty rather than using the add invoice action, that link between the original invoice and the SR sales order is severed.

Translate