The exact moment when a document-level script is executed is not known, and that means that a field may not yet have been created when the script runs.
In my experience, when it comes to set field values when the document opens, it is much safer to use the pageOpen event of the page at which the document opens.
Note: when you have a multi-page document, you will need to make sure that this piece of code gets executed only once; this is done in a way like this:
In a document-level script define:
var loaded = 0 ;
In the pageOpen script have this construct:
if (loaded < 1) {
// execute code when document opens
loaded++ ;
} else {
// execute code when returning to the page
}
And that takes care of the situation.