{"record":{"id":"12ab333fe2fcfcbd","repo":"nopSolutions/nopCommerce","slug":"order-cannot-be-loaded-12ab33","errorCode":null,"errorMessage":"Order cannot be loaded","messagePattern":"Order cannot be loaded","errorType":"exception","errorClass":"NopException","httpStatus":null,"severity":"error","filePath":"src/Plugins/Nop.Plugin.Payments.PayPalCommerce/Services/PayPalCommerceServiceManager.cs","lineNumber":2916,"sourceCode":"    /// <param name=\"settings\">Plugin settings</param>\n    /// <param name=\"shipment\">Shipment</param>\n    /// <returns>\n    /// A task that represents the asynchronous operation\n    /// The task result contains the operation result; error message if exists\n    /// </returns>\n    public async Task<(bool Result, string Error)> SetTrackingAsync(PayPalCommerceSettings settings, Shipment shipment)\n    {\n        return await HandleFunctionAsync(async () =>\n        {\n            if (!IsConfigured(settings))\n                throw new NopException(\"Plugin not configured\");\n\n            var carrier = await _genericAttributeService.GetAttributeAsync<string>(shipment, PayPalCommerceDefaults.ShipmentCarrierAttribute);\n            if (string.IsNullOrEmpty(carrier))\n                return false;\n\n            var nopOrder = await _orderService.GetOrderByIdAsync(shipment?.OrderId ?? 0)\n                ?? throw new NopException(\"Order cannot be loaded\");\n\n            if (!string.Equals(nopOrder.PaymentMethodSystemName, PayPalCommerceDefaults.SystemName, StringComparison.InvariantCultureIgnoreCase))\n                return false;\n\n            var customValues = new CustomValues();\n            customValues.FillByXml(nopOrder.CustomValuesXml);\n            var orderIdKey = await _localizationService.GetResourceAsync(\"Plugins.Payments.PayPalCommerce.Order.Id\");\n            if (!customValues.TryGetValue(orderIdKey, out var orderIdValue))\n                throw new NopException(\"Failed to get PayPal order info\");\n\n            var order = await _httpClient\n                .RequestAsync<GetOrderRequest, GetOrderResponse>(new GetOrderRequest { OrderId = orderIdValue.Value }, settings) as Order;\n            if (order.Status?.ToUpper() != OrderStatusType.COMPLETED.ToString())\n                throw new NopException($\"Unable to assign tracking information to orders in {order.Status} status\");\n\n            if (order.PurchaseUnits?.FirstOrDefault() is not PurchaseUnit unit || unit.Shipping is null)\n                throw new NopException(\"No shipping info found for PayPal order\");\n","sourceCodeStart":2898,"sourceCodeEnd":2934,"githubUrl":"https://github.com/nopSolutions/nopCommerce/blob/64bdf2ff08c8b39e65717bcf974fb43dc2ef68f2/src/Plugins/Nop.Plugin.Payments.PayPalCommerce/Services/PayPalCommerceServiceManager.cs#L2898-L2934","documentation":"Thrown inside SetTrackingAsync when the nopCommerce order associated with the shipment cannot be loaded. The order ID is taken from shipment.OrderId (or 0 if the shipment is null) and passed to GetOrderByIdAsync. A null return means no order exists with that ID, making it impossible to look up the PayPal order reference or verify the payment method.","triggerScenarios":"SetTrackingAsync is called with a shipment whose OrderId does not correspond to any order in the database, or the shipment parameter itself is null causing OrderId to default to 0.","commonSituations":"The order was deleted but the shipment record remains (orphaned shipment); shipment was created in a transaction that was later rolled back but the shipment record persisted; data import created shipments without matching orders; race condition where shipment is processed before the order is committed.","solutions":["Verify the shipment's OrderId references a valid order: SELECT * FROM [Order] WHERE Id = <shipment.OrderId>","If the order was deleted, remove the orphaned shipment or mark it as processed without PayPal sync","Add a null-check on the shipment parameter before calling SetTrackingAsync","Ensure shipment creation and order creation are in the same transaction to prevent orphans"],"exampleFix":"// before\nvar (result, error) = await _serviceManager.SetTrackingAsync(settings, shipment);\n\n// after — verify order exists before calling\nif (shipment is null)\n    return;\nvar order = await _orderService.GetOrderByIdAsync(shipment.OrderId);\nif (order is null)\n{\n    _logger.Warning($\"Shipment {shipment.Id} references missing order {shipment.OrderId}\");\n    return;\n}\nvar (result, error) = await _serviceManager.SetTrackingAsync(settings, shipment);","handlingStrategy":"validation","validationCode":"// Verify shipment and its order exist before calling SetTrackingAsync\nif (shipment is null)\n    return;\nvar order = await _orderService.GetOrderByIdAsync(shipment.OrderId);\nif (order is null)\n{\n    _logger.Warning($\"Shipment {shipment.Id} has no associated order; skipping tracking sync\");\n    return;\n}","typeGuard":null,"tryCatchPattern":"// HandleFunctionAsync catches internally; check the returned error tuple\nvar (result, error) = await _serviceManager.SetTrackingAsync(settings, shipment);\nif (!string.IsNullOrEmpty(error))\n    _logger.Warning($\"Tracking sync failed for shipment {shipment.Id}: {error}\");","preventionTips":["Never delete orders that have associated shipments without cleaning up the shipment records first","Add referential integrity checks or database constraints between Shipments and Orders","Validate shipment.OrderId is non-zero before calling SetTrackingAsync","Run periodic data integrity audits to detect orphaned shipments"],"tags":["paypal","shipment-tracking","data-integrity","orphaned-record","nopcommerce"],"backgroundTag":null,"analyzedSha":"64bdf2ff08c8b39e65717bcf974fb43dc2ef68f2","analyzedAt":"2026-08-13T21:19:38.062Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}