I want to display the Sales Order ID only when a Sales Invoice has a related Sales Order

i want to display the Sales Order ID only when a Sales Invoice has a related Sales Order in Print Format(jinja)

If the sales invoice is created from the sales order then in the line items of the sales invoice you will have a prevdoc_docname field which stores the sales order ID.
(Use doc.items[0].prevdoc_docname in sales invoice jinja template)

If you’re not creating the sales invoice from the sales order, then you won’t be able to map it to a sales order.