Update fields in PostOpen

Hi!

When I open a document I want to update some fields in it. The new values do I get from a view in the same database except from one value. That value comes from another database.

I can collect all the values and show them in a messagebox when I open the document. But when I do this code:

Call uidoc.FieldSetText(“DebAnslutning”,Str(sumAnslutning))

It says that I must be i editmode. I have given all users possibility to edit the document, but those who realy got this access is in a own role. In Querymodechange i look if the current user belong to this role, and if he don´t, he can´t go in editmode.

So, how do I fix, a user open the document, the document turns into edit mode, the values changes, document changes back to read mode with the new values and saves.

I’ll post all my code I have in Postopen:

Sub Postopen(Source As Notesuidocument)

Dim session As New NotesSession

Dim ws As New NotesUIWorkspace

Dim uidoc As NotesUIDocument

Dim doc As notesDocument

Set uidoc = ws.CurrentDocument

Set doc = uidoc.Document

Dim DebAnslutning As Integer

Dim DebNatverk As Integer

Dim DebProgram As Integer

Dim DebOvrigt As Integer

Dim Debitering As Variant

Dim AntDeb As Integer

DebAnslutning = uidoc.FieldGetText(“DebAnslutning”)

DebNatverk =uidoc.FieldGetText(“DebNatverk”)

DebProgram = uidoc.FieldGetText(“DebProgram”)

DebOvrigt = uidoc.FieldGetText(“DebOvrigt”)

Debitering = Split(uidoc.FieldGetText(“debitering”), ", ",-1)

AntDeb = (Ubound(Debitering) + 1)

Dim db As NotesDatabase

Dim view As NotesView

Dim entry As NotesViewEntry

Dim vc As NotesViewEntryCollection

Dim viewdoc As NotesDocument

Dim sum As Integer

Dim sumAnslutning As Integer

Dim sumNatverk As Integer

Dim sumProgram As Integer

Dim sumOvrigt As Integer

sumAnslutning = 0

sumNatverk = 0

sumProgram = 0

sumOvrig = 0

Set db = session.CurrentDatabase

Set view = db.GetView( “Alla poster\Tjänster” )

For x = 0 To AntDeb-1

Set vc = view.GetAllEntriesByKey(Debitering(x))

Set entry = vc.GetFirstEntry()

Do Until entry Is Nothing

If entry.ColumnValues(1) = “Anslutning” Then

sumAnslutning = sumAnslutning + entry.ColumnValues(2)

End If

If entry.ColumnValues(1) = “Nätverk” Then

sumNatverk = sumNatverk + entry.ColumnValues(2)

End If

If entry.ColumnValues(1) = “Program” Then

sumProgram = sumProgram + entry.ColumnValues(2)

End If

If entry.ColumnValues(1) = “Övrigt” Then

sumOvrigt = sumOvrigt + entry.ColumnValues(2)

End If

Set entry = vc.GetNextEntry(entry)

Loop

Next

'Kontrollera PC-kostnad

Dim MaskinDb As New NotesDatabase( “”, “Maskiner.nsf” )

Dim MaskinDoc As NotesDocument

Dim MaskinView As NotesView

key = uidoc.FieldGetText(“Anr”)

Set MaskinView = MaskinDb.GetView (“Maskiner” )

Set MaskinDoc = MaskinView.GetDocumentByKey(key)

Dim sumPC As Variant

sumPC = Maskindoc.GetItemValue( “Kostnad” )

'Slut på PC-kostnadskontroll

Messagebox "PC-kost: " & sumPC(0) & Chr(13) & "Anslutning: " & sumAnslutning & Chr(13) & "Nätverk: " & sumNatverk & Chr(13) & "Program: " & sumProgram & Chr(13) & "Övrigt: " & sumOvrigt, “Riktiga värden”

Call uidoc.FieldSetText(“DebAnslutning”,Str(sumAnslutning))

Call uidoc.FieldSetText(“DebNatverk”,Str(sumNatverk))

Call uidoc.FieldSetText(“DebProgram”,Str(sumProgram))

Call uidoc.FieldSetText(“DebOvrigt”,Str(sumOvrigt))

End Sub

Thanks for your help!

best regards

Henrik

Subject: Update fields in PostOpen

The first question I have is: why bother with the UI at all?If you want to update the document, just update the back-end document and it will be available for front-end use.

I noticed this line:

Set MaskinDoc = MaskinView.GetDocumentByKey(key)

Be very careful here. Unless that key is very unique, you may well get a handle to a machine with a closely matching key.

Use this instead:

Set MaskinDoc = MaskinView.GetDocumentByKey(key, True)

Subject: RE: Update fields in PostOpen

Thank you for your answer!

Ok, but I don´t realy know how to do it…Do you think you can give me an example?

best regards

Henrik

Subject: RE: Update fields in PostOpen

I think Ken means you should change all the uidoc lines like this:

old:

Call uidoc.FieldSetText(“DebAnslutning”,Str(sumAnslutning))

new:

uidoc.document.DebAnslutning = Str(sumAnslutning)

Subject: RE: Update fields in PostOpen

Thanks for your answer!

Now I don´t get any messages, so that seems to work. But I can´t do a reload och refresh on the document. So the values does´nt change on the front-end document.

I try with:

Call uidoc.Refresh

And:

Call uidoc.Reload()

But when I use them i got the following message:

“Document command is not available”

How should I do to reload the document?

best regards

Henrik

Subject: RE: Update fields in PostOpen

You need to have a documment open in edit mode in order to use uidoc.Refresh or uidoc.Reload. You are preventing users opening the document in edit mode on the front end(notesuidocument) but allowing them to modify the fields on the back end using script(notesdocument).

To achieve what you are trying to do you need to allow the users to open the document in edit mode but restrict what they are allowed to edit. You can do this by removing the querymodechange event code and adding computed for display fields for each field that you do not want them to edit.

For your editable fields add the hide when formula @IsNotMember(“[ROLENAME]”;@UserRoles) and do the opposite for your computed for display fields. They will then not be able to amend the fields manually even though the document is in edit mode.