Pre populate fields when generating a document

Hi all,
I want to run a function on the server, that pulls some data from a different system.
What I want to happen is that when someone wants to generate a new document the fields in the doc type are pre rendered/populated with the data the function pulled on the server side.
Ive tried using before_insert but Im not getting any fields pre populated.
Thanks