0
votes

I am trying to pre-populate some fields on an XPage (that creates a new doc) using an old LotusScript agent. My code on the XPage is:

 <xp:dominoDocument var="document1"
        formName="myForm">
        <xp:this.postNewDocument><![CDATA[#{javascript:
            var agent = database.getAgent("MyAgent");
            document1.save();
            agent.runOnServer(document1.getNoteID());
            }]]></xp:this.postNewDocument>
</xp:dominoDocument>


<xp:inputText value="#{document1.fname}" id="fname"
            styleClass="formInputText">
            <xp:this.defaultValue><![CDATA[#{javascript:
                document1.getItemValueString("fname");}]]></xp:this.defaultValue>
</xp:inputText>

The agent (for this example) is:

Dim agent As NotesAgent
Dim db As NotesDatabase

Sub Initialize
    Dim rDoc As NotesDocument 

    Dim s As New NotesSession
    Set db = s.CurrentDatabase
    Set agent = s.CurrentAgent

    Set rDoc = db.GetDocumentByID(agent.Parameterdocid)

    rDoc.fname = "Barney"       
    rDoc.lname = "Rubble"
    Call rDoc.Save(True, True)

End Sub

I know the agent is running (Agent log shows this and the fields are completed on the doc if I check the doc properties in Notes Client) however the field on the XPage is always blank? Is it possible to prepopulate from a LS agent? I added the document1.save() so I know I get a valid NoteID passed over (again which is the same - checked by logging) - any insight gratefully received...

4

4 Answers

5
votes

You can pass a document into an agent run. The method passing the unid into the agent context won't get you there. You need

  agent.runwithDocumentContext(doc)

See some example here: http://www.wissel.net/blog/d6plinks/SHWL-8SF7AH

Don't save the document. You will want to recast your agent into a JavaBean. Shaves off processing time.

What I actually would do: use a bean as the data source, makes it easier to deal with validation, default values etc.

It is less work than it might look like and it allows to pay down some technical debt (there is always technical debt).

Update

I tried it in a sample application. This is my agent:

Sub Initialize
    Dim db As NotesDatabase
    Dim rDoc As NotesDocument 

    Dim s As New NotesSession
    Set db = s.CurrentDatabase

    Set rDoc = s.Documentcontext

    rDoc.fname = "Barney"       
    rDoc.lname = "Rubble"

End Sub

2 key differences to your code: a) DocumentContext instead of parameterdocid and no saving of the document.

Then the page looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">
    <xp:this.data>
        <xp:dominoDocument var="document1" formName="person">
            <xp:this.postNewDocument><![CDATA[#{javascript:var agent = database.getAgent("Background");
var doc = document1.getDocument();
agent.runWithDocumentContext(doc);}]]></xp:this.postNewDocument>
        </xp:dominoDocument>
    </xp:this.data>
    <xp:label value="First name:" id="fName_Label1" for="fName1">
    </xp:label>
    <xp:inputText value="#{document1.fName}" id="fName1"></xp:inputText>
    <br />
    <xp:label value="Last name:" id="lName_Label1" for="lName1"></xp:label>
    <xp:inputText value="#{document1.lName}" id="lName1"></xp:inputText>
    <xp:button value="Save" id="button1"><xp:eventHandler event="onclick" submit="true" refreshMode="complete" immediate="false" save="true"></xp:eventHandler></xp:button>
</xp:view>

Key differences here:

  • No saving of a document
  • No population of default values
  • calling of the agent with DocumentContext

Finally: the agent must be set to "run as web user" (which it is probably already). Works like a charm (and I still would go for a bean).

1
votes

Be careful with saving the document from different events/sources as you do with your agent. Consider to re-code your agent's code to the postNewDocument event of your datasource of your Xpage. You can set values there, too. If you want to compute fields when creating a new document you can achieve this by setting the values in the postNewDocument event:

datasourceName.setValue("fieldName", "value")
1
votes

The cause for not getting the fields set by agent in XPage is that the agent runs "too late". The XPage fields are already set by the empty new values. Seems, there is no easy way to refresh the data source after agent execution.

So, I'd suggest to create an additional XPage ("XAgent") "MyFormNew.xsp" which:

  • creates the document,
  • runs the agent to set the values in that document and
  • redirects strait to your original XPage with the documentId as parameter.

The additional XPage could look like this:

<?xml version="1.0" encoding="UTF-8"?>
<xp:view
    xmlns:xp="http://www.ibm.com/xsp/core"
    rendered="false">
    <xp:this.beforePageLoad><![CDATA[#{javascript:
            var doc:NotesDocument = database.createDocument();
            doc.replaceItemValue("form", "myForm");
            doc.save();
            var agent = database.getAgent("MyAgent");
            agent.runOnServer(doc.getNoteID());
            var pageUrl = "MyForm.xsp?action=editDocument&documentId=" + 
                                               doc.getUniversalID().toString();
            context.redirectToPage(pageUrl);
            }]]></xp:this.beforePageLoad>
</xp:view>

The data source in your original XPage wouldn't need the postNewDocument event anymore.

This is a quick and dirty solution for staying with your LotusScript agent (as you pointed out in your comments you want/have to).

0
votes

Its all about timing. I am using this:

<xp:this.beforePageLoad><![CDATA[#{javascript:var     tempdoc:NotesDocument=compositeData.WFDoc.getDocument(true)
if (tempdoc.isNewNote())
{
var agent:NotesAgent=database.getAgent("(A_WF)")
  if(agent!=null)
  {
      agent.runWithDocumentContext(tempdoc)
  }
}
}]]></xp:this.beforePageLoad>

By using the before pageload event you can set the document before something might come from the xpage itself. You dont even have to save the document in the agent.

With this approach you can stay in the current document and you dont have to use any temporary documents as suggested by ibm.