从Xpages运行Agent报错:调用runWithDocumentContext时出现异常
agent.runWithDocumentContext(doc) Error in XPages JavaScript Let me walk through the common issues and fixes for this problem, since you've already confirmed the document and agent are being found correctly—great first step!
First, let's break down the possible causes and fixes:
Agent Permissions Mismatch
When running agents via XPages, the default "Run as web user" setting often causes permission headaches. Web users rarely have the full access an agent might need to modify documents, access other databases, or run restricted operations.- Fixes:
- Open your agent in Domino Designer, go to the Security tab, and switch "Run as" from "Web user" to "Agent signer". Make sure the signer has sufficient access to the database (e.g., Editor or higher, depending on what the agent does).
- Verify the agent signer's ACL entry for the database includes necessary privileges like "Create documents" or "Delete documents" if the agent performs those actions.
- Fixes:
Hidden Errors in the Agent Itself
Just because the agent is found doesn't mean its internal code plays nice with the document you're passing. The error could be happening inside the agent—like referencing a missing field, null object, or invalid operation.- Fixes:
- Test the agent directly in Domino Designer: Open the agent, click Run, and select a test document that matches the one you're passing via XPages. This will surface any runtime errors in the agent code immediately.
- Check the detailed error log at
E:/IBM/Domino/data/domino/workspace/log/error-log-0.xml—the generic CLFAD0131E message hides the actual root cause, and this log will have a full stack trace pointing to the exact line of code (either in your XPages script or the agent) that's failing.
- Fixes:
Document Context Issues
Even if the document isn't null, it might be in an unexpected state when passed to the agent. For example, it could be a stale copy, or (rarely) deleted between your null check and the agent call.- Fixes:
- Add a
doc.refresh()call right beforeagent.runWithDocumentContext(doc)to ensure you're working with the latest version of the document. - If this is a newly created document, make sure it's saved (
doc.save()) before passing it to the agent—unsaved docs can cause weird behavior in agent contexts.
- Add a
- Fixes:
XPages Database Context Discrepancy
Your code is fetching the database viasession.getDatabase(datasources["server"], datasources["database"]), which might create a separate database object than the one the XPages runtime is using. This can sometimes cause context mismatches.- Fix:
Replace that line withvar _db: NotesDatabase = database;(assuming your agent is in the same database) to use the current XPages database context directly. This avoids potential issues with server/database path typos or session context differences.
- Fix:
Revised Code with Error Handling & Best Practices:
function assignToMeAgent() { var rowId = viewScope.get("rowId"); if (rowId) { try { // Use current XPages database context instead of re-fetching var doc: NotesDocument = database.getDocumentByUNID(rowId); if (doc) { var agent: NotesAgent = database.getAgent("ABC"); if (agent) { // Refresh document to ensure latest state doc.refresh(); agent.runWithDocumentContext(doc); viewScope.put("operationStatus", "Agent executed successfully!"); } else { print("[assignToMeAgent] Agent 'ABC' not found in database"); } } else { print("[assignToMeAgent] Document with UNID " + rowId + " not found"); } } catch (e) { print("[assignToMeAgent] Error occurred: " + e.toString()); // Print full stack trace for debugging if (e.stack) { print(e.stack); } viewScope.put("operationStatus", "Error: " + e.toString()); } } else { print("[assignToMeAgent] No rowId found in viewScope"); } }
The most critical step here is checking that detailed error log—it will tell you exactly what's broken, whether it's a permission issue, a bug in the agent, or something else.
内容的提问来源于stack exchange,提问作者Patrick Kwinten

