Hi, you must check what No. the header actually got in BC. ORD-01014-R9B4R9 looks like the CRM number. If your code does Insert(true) without filling "No." first, BC is using its own number from the No. Series. Then your lines point at something that never existed. You must fill the CRM number in External Document No., not Document No.
Other thing I seen is a Commit between the header and the lines. Header fails validation, customer not coupled or whatever. But the lines still go in. So you get orphans. Keep both in one transaction, then a bad header takes the lines with it.
Glad to help - follow up if anything is unclear.
► If this solved it, marking it verified helps others too.
Regards,
Grigorios Mavrogeorgis
Business Central Consultant & AL Developer
Work: Gmsoft Limited
Blog: insidebusinesscentral
LinkedIn: linkedin.com/in/gregorymavrogeorgis