Can't access lines of receipt of invoice

SUGGESTED

I created a receipt of invoice, applied PO to it, changed some info- and in the end I called

record.nWrite();

where record is the pvxDynamic representing PO_Receipt_bus. I know that after the write, I have a new receipt in Sage and it's valid

right after the call to write I want to go through the lines of the invoice I just created.

If I call record.oLines I get the error - "You cannot select a purchase order number that has not been applied to this entry."

If I call record.oLines after calling setKeyValue for ReceiptType$ and ReceiptNo$ (followed by setKey()), I get the error - "Missing or Invalid Header".

Does anyone know why does it happen and how to fix it?