Skip to Content
0
Jul 28, 2023 at 06:51 AM

ByD - sometimes different result after execution

74 Views Last edit Aug 30, 2023 at 06:03 AM 10 rev

Hi SAP Community,

We have a solution, where we save some additional information on the CustomerContract. We are calculating the ordered, the delivered and the billed amount. Sometimes, the amount is correct and sometimes not. It's seems like the latest entry is sometimes missing. If we create a new Sales Order / Delivery / Customer Invoice the quantity of the second last gets included in the calculation. We

When we debug the solution, the values are more correct.The logic in the Before_Save in SalesOrder.xbo is always working. We just have the problems with the two Before_Save Scripts in the BO_CTSO_DeliveryToSonctractHelper.bo and BO_CTSO_InvoiceTOContractHelper.bo. Both are connected with a Internal Communication.

AND SORRY FOR THE FORMATTING, I TRIED 100X TO PUT IT INSIDE A CODE BLOCK.

Code Before_Save inside the Helper BO:

```import ABSL;

import AP.CRM.Global;

import AP.CustomerInvoicing.Global;

var invoice = CustomerInvoice.Retrieve(this.invoiceUUID);

if (invoice.IsSet())

{

foreach (var invItem in invoice.Item)

{

var invItemID = "";

if (invItem.IsSet() &&

invItem.ItemProduct.IsSet() &&

!invItem.ItemProduct.ProductKey.IsInitial() &&

!invItem.ItemProduct.ProductKey.ProductID.IsInitial())

{

// Get item ID

invItemID = invItem.ItemProduct.ProductKey.ProductID.content;

// Get SalesOrder on which the Invoice is based on

foreach (var docSsalesOrder in invItem.ItemBusinessTransactionDocumentReference)

{

var docSBusinessTrRef = docSsalesOrder.BusinessTransactionDocumentReference;

if (docSBusinessTrRef.TypeCode != "114")

{

continue;

}

var baseSalesOrder = SalesOrder.Retrieve(docSsalesOrder.BusinessTransactionDocumentReference.ID);

//// Loop over all items of the SalesOrder

var amount = 0;

// Get Contract on which the SalesOrder is based on

foreach (var docContract in baseSalesOrder.BusinessTransactionDocumentReference)

{

var docCBusinessTrRef = docContract.BusinessTransactionDocumentReference;

if (docCBusinessTrRef.TypeCode != "1092")

{

continue;

}

var contract = CustomerContract.Retrieve(docCBusinessTrRef.ID);

// Loop over all items of the Contract

foreach (var contrItem in contract.Item)

{

// Find line in Contract with the appropriate item

if (contrItem.ItemProduct.ProductKey.ProductID.content == invItemID)

{

// Loop over all SalesOrders linked to the Contract

foreach (var salesOrderID in contract.BusinessTransactionDocumentReference)

{

var salesBusinessTrRef = salesOrderID.BusinessTransactionDocumentReference;

if (salesBusinessTrRef.TypeCode != "114")

{

continue;

}

var salesOrder = SalesOrder.Retrieve(salesBusinessTrRef.ID);

// Loop over all Invoices linked to the SalesOrder

foreach (var invoiceID in salesOrder.BusinessTransactionDocumentReference)

{

var salesOrderBusinessTrRef = invoiceID.BusinessTransactionDocumentReference;

if (salesOrderBusinessTrRef.TypeCode != "28")

{

continue;

}

var invoice2 = CustomerInvoice.Retrieve(salesOrderBusinessTrRef.UUID);

// Loop over all items in Invoice to find the appropriate item

//var invoiceItems = invoice2.Item.Where(it => it.ID == invItemID);

foreach (var invoiceItem in invoice2.Item)

{

// salesItem.ItemActualValues.FulfilledQuantity.content;

if (invoiceItem.ItemProduct.ProductKey.ProductID.content == invItemID)

{

var quantity = invoiceItem.Quantity;

amount = amount + quantity.content;

}

}

}

}

contrItem.Z_InvoicedQuantity1.unitCode = "EA";

contrItem.Z_InvoicedQuantity1.content = amount;

}

}

}

}

}

}

}```

Code in the Communication Scenario, Condition Tab - Start Condition:

import ABSL;

if (this.Status.ReleaseStatusCode == "3")

{

return true;

}

else

{

// TODO: change back to "false" after testing!!!

return true;

}

Many thanks and wishes, Mischa