Run a codeunit on release of purchase document

I am using US Business Central 21.4
The goal of what I am trying to achieve is to generate a pdf and place it in sharepoint when a purchase order is released.

I use Lasernet which takes a request from BC and does everything I need mentioned above (generation of pdf and sharepoint).

All I need to do on the BC side is send that request when a PO is released. If the PO is reopened and released again, then the pdf would be overwritten, which is not a problem.

This is what I have right now for my code. I also do the same thing for sales orders and they seem to work with no issues.

codeunit 50360 GenerateDocOnRelease
    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Release Sales Document", 'OnAfterReleaseSalesDoc', '', false, false)]
    local procedure OnAfterReleaseSalesDoc(var SalesHeader: Record "Sales Header"; PreviewMode: Boolean; var LinesWereModified: Boolean);

        if not Codeunit.Run(CODEUNIT::GenerateSalesOrder, SalesHeader) then;

    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Release Purchase Document", 'OnAfterReleasePurchaseDoc', '', false, false)]
    local procedure OnAfterReleasePurchaseDoc(var PurchaseHeader: Record "Purchase Header"; PreviewMode: Boolean; var LinesWereModified: Boolean)
        if not Codeunit.Run(CODEUNIT::GeneratePurchaseOrder, PurchaseHeader) then;


That calls this other codeunit i created

codeunit 50362 GeneratePurchaseOrder
    TableNo = "Purchase Header";

    trigger OnRun()

    local procedure sendPurchaseOrder(var PurchaseHeader: Record "Purchase Header")
        TempPrintRequest: Record "FPL Print Request" temporary;
        recOrd: Record "Purchase Header";
        recOrd.Get(PurchaseHeader."Document Type", PurchaseHeader."No.");
        TempPrintRequest."Document Type" := "FPL Document Type"::PurchaseOrder;
        TempPrintRequest.MainTableID := Database::"Purchase Header";
        TempPrintRequest."Print Method" := TempPrintRequest."Print Method"::Fax;
        TempPrintRequest.Archive := TempPrintRequest.CheckDefaultArchive();


The problem seems to be only with purchase orders that are drop ships for sales orders.
It also doesn’t happen on every drop ship purchase order.
When the purchase order is posted (receive only) the qty shipped field on the sales order is not getting populated and since it is not showing shipped it will not let us invoice. It even generates a sales shipment with the correct quantities.

Originally I had the sendPurchaseOrder procedure in the EventSubscriber procedure.
If there was an error with sending the request to lasernet it seemed to break the sales order that the purchase order was for.
The error we got originally was a variable not defined that would cause the issue.

I was told by a developer that if I call the second codeunit with a run command (as shown above) it would not stop on an error.
I tested this in sandbox and when i tired to release a po i got an AL run module error and that was resolved with adding the commit();

Well, i pushed this to prod and had them post a drop ship po and it worked. they checked the shipped quantities on the order and everything was good.
They tried another and got the error below, BUT the sales order was good.

Then they did another, got the error above and the qty shipped field on the sales order did not getting populated. It created a sales shipment but now the sales order cannot be invoiced because of this.

What am I doing wrong? This seems like such a simiple task. I just wanted to “listen” when a PO was released to run a codeunit. I am not altering the po or anything like that.

Any advice is greatly appreciated.