Refreshing the UIDocument after RunOnServer Agent changes back end Document

Hi there,

I’m having a classic front end / back end problem…

I have a form with a text box (CustSearchString), a result field (Result) and a “Search” Button. The button’s code looks like this:

Sub Click(Source As Button)

Dim workspace As New notesuiworkspace

Dim s As New NotesSession

Dim db As NotesDatabase

Dim doc As notesdocument

Dim uidoc As notesuidocument

Set uidoc = workspace.CurrentDocument

Set doc = uidoc.Document

Dim agent As NotesAgent

Set db = s.CurrentDatabase

Set agent = db.GetAgent(“LookupCustomer”)

If agent.RunOnServer(doc.NoteID) = 0 Then

Call uidoc.refresh

End If

End Sub

The agent “LookupCustomer” looks like this:

Sub Initialize

Dim agent As notesagent

Dim con As New ODBCConnection

Dim qry As New ODBCQuery

Dim result As New ODBCResultSet

Dim session As New NotesSession

Dim db As NotesDatabase

Dim doc As NotesDocument

Set qry.Connection = con

Set result.Query = qry

Set agent = session.currentagent

Set db = session.CurrentDatabase

Set doc = db.GetDocumentByID(agent.ParameterDocID)

Set nResult = doc.GetFirstItem(“Result”)

con.ConnectTo(“as400”)

qry.SQL = "SELECT cmcsno, cmcsnm FROM aplus2fcm.cusms WHERE cmcsno = " + doc.CustSearchString(0)

result.Execute

result.NextRow

sfield = result.GetValue(1)

sCustomerNumber = Trim(sfield)

sfield = result.GetValue(2)

sCustomerName = Trim(sfield)

sResult = sCustomerNumber + " - " + sCustomerName

doc.Result = sResult

Call doc.Save(True,True)

con.Disconnect

End Sub

Everything is working fine except the uidoc doesn’t show the the new value for the “Result” field. But if I close the uidoc and reopen it, it shows up.

I’ve tried using uidoc.reload, and also tried refreshing/reloading the uidoc outside of the button script, but only closing and reopening seems to do the trick.

Any help would be appreciated,

Regards,

Scott McCoskery

Subject: Refreshing the UIDocument after RunOnServer Agent changes back end Document

I think that’s the only thing that will work. Reload is as close as you’ll get, and that won’t do it. Y’see, there are actually three versions of the document in play here: the UI document, a NotesDocument object (the document property of the UI doc) in memory, and a document saved on disk. What Reload would do is load the in-memory document into the UI. However, what your run-on-server agent does is to make changes to the on-disk document. Neither Reload nor Refresh touches that one, and I don’t think that there are any methods that go all the way back to the saved-on-disk version.

Subject: Refreshing the UIDocument after RunOnServer Agent changes back end Document

why not do it all from the front end?

Subject: RE: Refreshing the UIDocument after RunOnServer Agent changes back end Document

I can’t do it all from the front end because I want to establish the ODBC connection from the server, therefore I don’t have to rely on a proper ODBC configuration on every client.

I think I’ve found the err of my ways, though. Right now I’m trying to create a new document in the button script to store both the search string from the uidoc and the results from the agent, then at the end of my button script I’ll delete my “tempdoc” object and set it again, hopefully grabbing the results… I think I remember reading about this technique in an agent FAQ…

Thanks,

Scott

Subject: Resolved: Refreshing the UIDocument after RunOnServer Agent changes back end Document

I got it figured out. I’m posting the changes in the code for the forum…

Here is the button script:

Sub Click(Source As Button)

Dim workspace As New notesuiworkspace

Dim s As New NotesSession

Dim db As NotesDatabase

Set db = s.CurrentDatabase

Dim id As String

Dim doc As notesdocument

Dim uidoc As notesuidocument

Dim resultdoc As NotesDocument

Set resultdoc = New NotesDocument( db )

Set uidoc = workspace.CurrentDocument

Set doc = uidoc.Document

resultdoc.SearchString = uidoc.FieldGetText(“CustSearchString”)

id = resultdoc.NoteID

Call resultdoc.Save(True,True)

Delete resultdoc

Dim agent As NotesAgent

Set agent = db.GetAgent(“LookupCustomer”)

If agent.RunOnServer( id ) = 0 Then

Set resultdoc = db.GetDocumentByID(id)

uidoc.EditMode = True

Call uidoc.FieldSetText(“ResultSelections”,resultdoc.ResultSelections(0))

End If

resultdoc.remove( True )

End Sub

And the corresponding agent…

Sub Initialize

Dim agent As notesagent

Dim con As New ODBCConnection

Dim qry As New ODBCQuery

Dim result As New ODBCResultSet

Dim session As New NotesSession

Dim db As NotesDatabase

Dim resultdoc As NotesDocument

Dim nResultSelections As NotesItem

Set qry.Connection = con

Set result.Query = qry

Set agent = session.currentagent

Set db = session.CurrentDatabase

Set resultdoc = db.GetDocumentByID(agent.ParameterDocID)

Set nResultSelections = New NotesItem( resultdoc, “ResultSelections”, “”)

con.ConnectTo(“as400”)

qry.SQL = “SELECT cmcsno, cmcsnm, cmcad1, cmcad2, cmcity, cmstat, cmzip4 FROM aplus2fcm.cusms WHERE cmcsno =” + " " + resultdoc.SearchString(0)

result.Execute

i = 0

Do

result.NextRow

sfield = result.GetValue(1)

sCustomerNumber = Trim(sfield)

sfield = result.GetValue(2)

sCustomerName = Trim(sfield)

sResultSelections = sCustomerNumber + " - " + sCustomerName

Call nResultSelections.AppendToTextList( sResultSelections )

Call resultdoc.Save(True,True)

i=0

Forall j In resultdoc.ResultSelections

i = i + 1

End Forall

If i = 50 Then Goto finish 'exit early when 50 records reached

Loop Until result.IsEndOfData

Finish:

resultdoc.ResultCount = i

Call resultDoc.save ( True, True )

con.Disconnect

End Sub