If you create new sales order and create credit note of any previous sales order, then here is the way to find reference of previous sales order (invoice number) through newly created credit note sales order using x++.
InventTransOrigin::findByInventTransId(SalesLine::Find(this.SalesId).InventTransIdReturn).ReferenceId;
Note:
If you create a credit note simply by using the "Credit Note" button on the sales order form, Ax copies the previous invoice lines into the original sales order with a negative sign. In this case you do not need any additional reference to the original sales order as you are still using the same sales order.
If you create a credit note simply by using the "Credit Note" button on the sales order form, Ax copies the previous invoice lines into the original sales order with a negative sign. In this case you do not need any additional reference to the original sales order as you are still using the same sales order.