Add sales order to child table based on sales agent

Dear all,

I would appreciate a little help for the following use case:

There is a field called “Sales Agent” within the Sales Order DocType. Sales Agent is a DocType that contains a child table that looks like this:

I want the sales order to automatically be displayed in the child table for the chosen sales agent when the document (=sales order) has been submitted and the status is not = cancelled. If the sales order is being cancelled, I would like the row to be deleted as well since I am calculating the agent’s sales volume via custom script.

Any ideas on how to auto-populate the table or fetching the sales order ID when the sales order is submitted?

Thanks in advance!