How do I delete a location document using a default passthru server from the personal address book using LotusScript?

niccilaw
niccilaw used Ask the Experts™
on
I created a button using LotusScript to delete any location documents that are using a default passthru server, but it is not working.  I am getting a type mismatch error.  I am a novice to LotusScript so I would like someone to post the corrected code.
Sub Click(Source As Button)
	Dim session As New NotesSession
	
	Dim db As New NotesDatabase("", "names.nsf" )
	Dim view As NotesView
	Dim count As Integer
	Dim doc As NotesDocument
	Dim item As NotesItem
	Dim itemName As String
	Set view = db.GetView("Locations")
	
	Set doc = view.GetFirstDocument
	itemName=doc.GetItemValue("DefaultPassthruServer")
	
	While Not(doc Is Nothing)
		If Not ItemName="" Then
			Call doc.Remove( True )
		End If
		Set doc = view.GetNextDocument(doc)
	Wend
End Sub

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
The field is called "PassthruServer", not "DefaultPassthruServer".

But the reason for your error is that
    doc.GetItemValue("DefaultPassthruServer")

returns array with all values of the field, which you're trying to store in a String variable.


Do it like this:
    itemName = doc.GetItemValue( "PassthruServer" )(0) 'returns first value from the field values array

Commented:
You have to know that, even if a field has only one value, this expression:
    doc.FieldName

which is the same as this expression:
    doc.GetItemValue("FieldName")

will return array with one element.
That's why we use:
    doc.FieldName(0)

when we know that it's not a multi-value field.

Author

Commented:
When I checked the document properties for the location document, the name of the field is DefaultPassthruServer.  Even after making your suggested change to the code, I cannot delete the location document that contains a value in this field.  I need to code to check to see if the value in the "DefaultPassthruServer" is null and if it is not null, delete the location document.
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Author

Commented:
Here is the updated code.  However, when it runs, I get a ADT argument error.

Sub Click(Source As Button)
      Dim session As New NotesSession
      
      Dim db As New NotesDatabase("", "names.nsf" )
      Dim view As NotesView
      Dim count As Integer
      Dim doc As NotesDocument
      Dim item As NotesItem
      Dim itemName As String
      Set view = db.GetView("Locations")
      
      Set doc = view.GetFirstDocument
      While Not(doc Is Nothing)
            itemName = doc.GetItemValue( "Name" )(0) 'returns first value from the field values array
            If ItemName="Home (Notes Direct Dialup)" Then
                  Call doc.Remove( True )
            End If
            Set doc = view.GetNextDocument(doc)
      Wend
End Sub
Commented:
The problem is that you're trying to use

    view.GetNextDocument(doc)

after you called

    Call doc.Remove( True )

so the code is asking you "Move from where? Where's the doc?"

This is the way to delete documents while looping through any kind of document collection (I wrote it here, so it could contain errors):

	Dim tmpDoc As NotesDocument
	Dim flgDeleted As Boolean
	
	Set doc = view.GetFirstDocument
	Do Until doc Is Nothing
		flgDeleted = false
		itemName = doc.GetItemValue( "Name" )(0) 'returns first value from the field values array
		If ItemName = "Home (Notes Direct Dialup)" Then
			Set tmpDoc = doc 'save the reference to doc that needs to be deleted
			flgDeleted = True
		End If

		Set doc = view.GetNextDocument(doc) 'get next doc

		If flgDeleted Then
			Call tmpDoc.Remove( True ) 'now, after you moved away from the doc to be deleted, delete it
		End If
	Loop

Open in new window

Commented:
Your code looks good to me Sjef. Not bad for on the fly! To have it use his parameters, and using a variant technique
Dim next As NotesDocument
        Dim itemValue As String '"itemName" was badly named
	
	Set doc = view.GetFirstDocument
	Do Until doc Is Nothing
		Set next = view.GetNextDocument(doc) 'before delete

		itemValue = doc.GetItemValue(itemName)(0)
 		If itemValue <> "" Then
                    doc.Remove True
		End If
		Set doc = next
                    'works even on delete 'cuz already got next doc
	Loop

Open in new window

Commented:
Yes, that's cleaner.

BTW, I'm not Sjef, I'm Marko :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial