Hi,
it seems that IVTYP 'B' will be found automatically by a given document number. IVTYP 'B' means BAPI Invoice. But your code checks for numeric types 1 ERS or 5 Cancellation. Please check domain values for domain IVTYP for that.
It seems that you are performing with a wrong document number of IVTYP 'B'.
Regards,
Klaus