Developer forum

Forum » Integration » setting Cancel to true in OnBeforeSendingOrderToErp after cart creation logs an error

setting Cancel to true in OnBeforeSendingOrderToErp after cart creation logs an error

Mikkel Hammer
Mikkel Hammer
Reply

Hi DW,

We have a scenario where some customers orders need to be approved before it's fully "completed" and sent to the ERP.

In our current setup, when the customer goes through the checkout flow, the order gets completed and in a CheckoutDone Observer, we set the order state to PENDING.
Im then trying to cancel the ERP request in OnBeforeSendingOrderToErp like this. Because we only want fully completed orders in the ERP.


But when i look in the LiveIntegration log i get an error that the order was not created in the ERP.



But shouldn't that request cancel via the OnBeforeSendingOrderToErp or am i doing something wrong?

This is our live integration setup


We're running on a DW 9.15.9 with LiveIntegration updated to 7.2.11, and the ERP is NAV.

Best regards,
Mikkel Hammer


Replies

 
Dmitriy Benyuk Dynamicweb Employee
Dmitriy Benyuk
Reply

Hi Mikkel,
sure the request is Cancelled but the code is logging it as an error as in this case there is no request sending to ERP and thats why there is no response, so the Live integration considers null response as an error, so this is not "correct" log message for this case and it should be improved.
BR, Dmitrij

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

I think it is cancelling it; it just cannot deal with a missing response afterwards all too well. The code that logs your last line looks like this:

            XmlDocument response = GetResponse(settings, requestXml, order, createOrder, logger);
            if (response != null && !string.IsNullOrWhiteSpace(response.InnerXml))
            {
                bool processResponseResult = ProcessResponse(settings, response, order, createOrder, successOrderStateId, failedOrderStateId, logger);
                Diagnostics.ExecutionTable.Current.Add("DynamicwebLiveIntegration.OrderHandler.UpdateOrder END");
                return processResponseResult;
            }
            else
            {
                // error occurred                
                if(createOrder)
                {
                    HandleIntegrationFailure(settings, order, failedOrderStateId, orderId, null, logger);
                    Services.OrderDebuggingInfos.Save(order, $"ERP communication failed with null response returned.", OrderErpCallFailed, DebuggingInfoType.Undefined);
                }

                Diagnostics.ExecutionTable.Current.Add("DynamicwebLiveIntegration.OrderHandler.UpdateOrder END");
                return false;
            }

 

When the order is cancelled, there's no response document hence the last bit of code logs the error.

Could it be that it's working, the order is not sent and only the logs are confusing?

Imar

 
Mikkel Hammer
Mikkel Hammer
Reply

Hi both,

Thanks for answering.

I tried looking into the source code aswell, and into that part that Imar points out aswell.
When looking into the method in the first line with GetResponse, i found the OnBeforeSendingOrderToErp part which looks like it should log a cancel message aswell which it doesn't.



That's why im thinking it's not cancelling correctly.
But unfortunately i don't have access atm to test further.

But i guess i can try to debug the source code, or test if it sends an error email to see if it's only an "visual" log error when i get access again :)

Cheers for the responses.

Best regards,
Mikkel Hammer

 
Mikkel Hammer
Mikkel Hammer
Reply

I just tried adding myself as notification recipient e-mail with a old template, and it does trigger an error mail.


So i guess there is some sort of bug involved.

Best regards,
Mikkel Hammer

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Hi Mikkel,

I think it follows the flow I outlined earlier. Here are the relevant bits from GetResponse:

private static XmlDocument GetResponse(Settings settings, string requestXml, Order order, bool createOrder, Logger logger)
{
  XmlDocument response = null;

  ..
  
  Notifications.Order.OnBeforeSendingOrderToErpArgs onBeforeSendingOrderToErpArgs = new Notifications.Order.OnBeforeSendingOrderToErpArgs(order, createOrder, settings, logger);
  NotificationManager.Notify(Notifications.Order.OnBeforeSendingOrderToErp, onBeforeSendingOrderToErpArgs);

  if (!onBeforeSendingOrderToErpArgs.Cancel)
  {
    response = Connector.CalculateOrder
    ...
  }
  else
  {
    OrderDebuggingInfo.Save(order, DateTime.Now, "Order not sent to ERP because a subscriber cancelled sending it", OrderErpCallCancelled, DebuggingInfoType.Undefined);
  }
}

It defines the response as null, then calls the subscriber. When Cancel is false it creates the response and returns it. When Cancel is set to true however, it returns null. The last log line you refer to doesn't log to the LI log file but to the OrderDebuggingInfo table in the database. Pretty sure you'll find records for these orders there.

The null response from GetResponse then causes issues in UpdateOrder:

XmlDocument response = GetResponse(settings, requestXml, order, createOrder, logger);
if (response != null && !string.IsNullOrWhiteSpace(response.InnerXml))
{
    bool processResponseResult = ProcessResponse(settings, response, order, createOrder, successOrderStateId, failedOrderStateId, logger);
    Diagnostics.ExecutionTable.Current.Add("DynamicwebLiveIntegration.OrderHandler.UpdateOrder END");
    return processResponseResult;
}
else
{
    // error occurred                
    if(createOrder)
    {
        HandleIntegrationFailure(settings, order, failedOrderStateId, orderId, null, logger);
        Services.OrderDebuggingInfos.Save(order, $"ERP communication failed with null response returned.", OrderErpCallFailed, DebuggingInfoType.Undefined);
    }

    Diagnostics.ExecutionTable.Current.Add("DynamicwebLiveIntegration.OrderHandler.UpdateOrder END");
    return false;
}

Because response is null, HandleIntegrationFailure is called which records the last error log line.

So, I think it works in the sense that it does cancel the error. However, it also seems to log false positive error messages as the ERP communication didn't really fail, it just never happened. It might be nice if GetResponse could somehow flag that it didn't fail but that it just didn't do anything so the error can be skipped. Maybe GetResponse could return some kind of constant Null object that signals the calling code that it didn't do anything.

@Dmitriy: any thoughts on this?

Imar

 
Mikkel Hammer
Mikkel Hammer
Reply

Hi Imar,

Thanks for the explanation and break down, it makes sense. I appreciate it! :)
Sorry for my confusion.

You're right, it does log it to the EcomOrderDebuggingInfo table. So it does cancel it.



But yeah, it would be great with some reworked handeling of the cancelation so it doesn't "fail".
Looking forward to what you find out.

Best regards,
Mikkel Hammer

 
Mikkel Hammer
Mikkel Hammer
Reply

bump

 
Dmitriy Benyuk Dynamicweb Employee
Dmitriy Benyuk
Reply

Hi Mikkel,
that needs to be fixed, I have created a new item #15986 to fix that.
BR, Dmitrij

 
Mikkel Hammer
Mikkel Hammer
Reply

Thanks a lot Dmiriy (and Imar ofc), looking forward to the fix :)

Best regards,
Mikkel Hammer

 
Dmitriy Benyuk Dynamicweb Employee
Dmitriy Benyuk
Reply
This post has been marked as an answer

Hi Mikkel,
it is now available in the new Live integration 7.3.1 version.
BR, Dmitrij

Votes for this answer: 1
 
Mikkel Hammer
Mikkel Hammer
Reply

Hi Dmitriy,

Thanks for the quick fix and for letting me know it's released, will try it out next week.

Best regards,
Mikkel Hammer

 

You must be logged in to post in the forum