Need a little more information, is this Notes client or web?
How are you triggering the agent?
Is the agent set to work on selected documents, all or none?
Why not put the code into a computed field on the form instead of running an agent? For a notes client the agent needs the docid to process the document, so the document needs to be closed.