How to combine several queries into one function?

It looks like you are trying to insert a parent record, then one or more child records, and then make an update to a third table. I've just seen and responded to a similar-ish question here:

You can certainly write PL/SQL functions in your DB, or you can try something like the linked question as well, triggering each subsequent step from the success of the one before (or in a JS block).