Reputation: 379
when i try to pass an NotesXSPDocument to an agent with runWithDocumentContext to manipulate some fields, i never get the new values to the frontend fields. I have tried a fullrefresh and a partialrefresh. When i just print the field value i get the changes successfully.
Here is an example of the XPage
<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">
<xp:panel id="thePanel">
<xp:this.data>
<xp:dominoDocument var="document" formName="from"></xp:dominoDocument>
</xp:this.data>
<xp:button value="Label" id="button1">
<xp:eventHandler event="onclick" submit="true"
refreshMode="partial" refreshId="thePanel">
<xp:this.action><![CDATA[#{javascript:
var agent = database.getAgent("test");
agent.runWithDocumentContext( document.getDocument());
print( "Value-->" + document.getDocument().getItemValueString("InMemReturn"));}]]>
</xp:this.action>
</xp:eventHandler>
</xp:button>
<xp:br></xp:br>
<xp:br></xp:br>
<xp:inputText id="inputText1" value="#{document.InMemReturn}"></xp:inputText>
</xp:panel>
</xp:view>
here is the Agent
Option Public
Option Declare
Sub Initialize
Dim session As New NotesSession
Dim doc As NotesDocument
Set doc = session.Documentcontext
doc.Appenditemvalue "InMemReturn", CStr(Now)
End Sub
Have I missed something?
Upvotes: 0
Views: 1556
Reputation: 20394
Manipulating documents in agents that bound to input fields are tricky. The XPages runtime isn't really aware what the agent is doing. Quite often you are actually better off converting an agent into a Java bean and call that one. It gives you the opportunity to clean up code and optimize performance. It also removes the performance penalty of loading the agent runtime for an operation. "I want to reuse existing code" is often overrated here...
But you asked... so here we go. The documentation suggest you can use
agent.runWithDocumentContext(document.getDocument(true));
to ensure backend changes are applied. Try that and let us know how it went. I actually would take a slightly different approach and bind the input field to a view variable and write that one only back on the save event. Something like:
<xp:inputText id="inputText1" value="#{viewScope.InMemReturn}"></xp:inputText>
and in your SSJS:
var agent = database.getAgent("test");
var doc = document.getDocument(true);
agent.runWithDocumentContext(doc);
viewScope.InMemReturn = doc.getItemValueString("InMemReturn");
and in a querySave event:
document.getDocument().replaceItemValue("InMemReturn",viewScope.InMemReturn);
Let us know how it goes.
Upvotes: 3
Reputation: 1006
You dont save the document in the Agent
Call Doc.save(false,true,false)
Upvotes: -1