Codeunit 80 with temporary Sales Header

Hello, Can I run Codeunit 80 with a temporary record Sales Header as parameter. Through an interface I insert a Sales Invoice in tabel 36 en 37. If an error occurs during the posting the Sales Invoice should be deleted. The error should be shown, but after that error the proces will end. So there is no possiblitity to delete the Sales Invoice in code. Main question is if I insert a temporary record Sales Header and temporary record Sales Line is the temporary record Sales Line available in codeunit 80 if I run codeunit 80 with the temporary record Sales Header.

Quick answer is no. If you pass a Temporary Sales Header, it will still look for any related tables (including sales line) as permanent tables.

You can catch an error by evaluating the return value of a codeunit. Let’s assume that MyCodeunit will error out. MyCodunit.RUN; This will error out and the transaction will abort. IF NOT MyCodeunit.RUN THEN BEGIN //now you can delete your invoice END; This will not actually ‘catch’ the error, but your own transaction will not be aborted. So, without actually paying attention to syntax, here’s what you could do: // create Sales Header SalesHeader.INSERT; // create sales line SalesLine.INSERT IF NOT SalesPost.RUN(SalesHeader) THEN SalesHeader.DELETE(TRUE); Of course this code is not complete by any means, but this how I’d try and catch the error. I didn’t spend a lot of time on this post, so send me a PM if you want more help.

I solved the problem. I only use a COMMIT-statement when I use the returnvalue of codeunit 80. If I use codeunit80.RUN i don’t use the commit. So therefor the commit rollback takes care of deleting the sales Invoice because I’m running codeunit 80 in the same proces as I insert the salesinvoice.