Subject: RE: Locking Document & Web - Agent Code
OK… I must be missing something simple. Here is each piece:
***it just opens the doc in edit mode and no lock. I access using another browser session and the edit button is still there.
- islocked view
view selection: SELECT islocked=“locked”
1st column formula: @Text(@DocumentUniqueID). The 1st column is sorted ascending.
- isLocked field
docid :=@DbLookup(“”;“”:“sample\sample.nsf”;“islocked”; @Text(@DocumentUniqueID); “”);
@If(@IsError(docid);“Unlocked”;“Locked”)
***i have the isLOcked field’s hide/when = isLocked=“Unlocked”
- Edit Button
@Command([RunAgent]; “(savelock)”)
***the edit button hide/when true = isLocked=“Locked”
- (savelock) agent
Sub Initialize
Dim session As New NotesSession
Dim db As NotesDatabase
Dim doc As NotesDocument
Dim doc2 As NotesDocument
Dim username As String
Dim curruser As Variant
On Error Goto whoops
Set db = session.CurrentDatabase
Set doc = session.DocumentContext
curruser = Evaluate({ @Name([CN];@UserName) }, doc)
username = curruser(0)
'following step is done because the user could have opened the doc and only hit the edit button a few minutes later
'by which time another user could have opened and edited/locked the same doc
Set doc2 = db.GetDocumentByUNID(doc.UniversalID)
If Not doc2 Is Nothing Then
If doc2.HasItem( "isLocked" ) Then
If doc2.isLocked(0) = "Locked" Then
If doc2.HasItem( "WhoLocked") Then
If doc2.WhoLocked(0) = curruser Then
'open in edit mode
'the person who locked it is trying to edit it, so allow this
Print("[" + db.filepath+"/0/"+doc2.UniversalID+"?EditDocument]")
Else
'open in read mode
'the person other than the person who locked it is trying to edit it, so do not allow this
Print("[" + db.filepath+"/0/"+doc2.UniversalID+"?OpenDocument]")
End If
Else
'open in read mode
'system problem as there is no person assigned to the WhoLocked field, so do not allow this
Print("[" + db.filepath+"/0/"+doc2.UniversalID+"?OpenDocument]")
End If
Else
'set locked
'not locked so set to locked
doc2.isLocked = "Locked"
doc2.WhoLocked = username
Call doc2.Save(True, False)
Print("[" + db.filepath+"/0/"+doc2.UniversalID+"?EditDocument]")
End If
Else
'set locked
'not locked so set to locked
doc2.isLocked = "Locked"
doc2.WhoLocked = username
Call doc2.Save(True, False)
Print("[" + db.filepath+"/0/"+doc2.UniversalID+"?EditDocument]")
End If
End If
Exit Sub
Whoops:
Dim strErrMsg
strErrMsg = "<HR>Error #" & Err & Chr$(10) & Error$ & Chr$(10) & "Line #" & Erl & | in sub/function: "| & Lsi_info(2) & |"|
strErrMsg = strErrMsg + "<br>Please submit an APS Online support request.<HR>"
Print strErrMsg
Exit Sub
End Sub
- Guess I need to set the isLocked field back to “unLocked,” right? I was planning on doing this with the save/submit button.