Replace missing values in a view, ReplaceItemValue is not working

I want to replace the missing values in a view. I tried the below, but it is not working. Please help.

Dim session As New NotesSession

Dim db As NotesDatabase

Dim view As NotesView

Dim doc As NotesDocument

Dim item As NotesItem

Set db = session.CurrentDatabase

Set view = db.GetView(“Test”)

Set doc = view.GetFirstDocument

If doc.HasItem(“Division”) Then

While Not(doc Is Nothing)

Forall div In doc.GetItemValue(“Division”)

If Isnull(div) Then

Set item = doc.ReplaceItemValue ( “Division”, 122 )

End If

End Forall

Set doc = view.GetNextDocument(doc)

Wend

End If

Call doc.Save( True, True )

Subject: Replace missing values in a view, ReplaceItemValue is not working

One reason could be that your While loop is inside your if statement. If the first document does not have Division then it will never enter the loop.

Subject: RE: Replace missing values in a view, ReplaceItemValue is not working

I have removed the IF clause, but still it is not working. Pls suggest.

Subject: RE: Replace missing values in a view, ReplaceItemValue is not working

Arshad has the answer. You may need to check that Div = “” as well as null

Subject: Replace missing values in a view, ReplaceItemValue is not working

Hi Radhika

You need to save the document you are modifying BEFORE moving on to the next doc whereas in your code above you are only calling the Save method after you have processed all the documents and outside the loop!

HTH

A

Subject: RE: Replace missing values in a view, ReplaceItemValue is not working

I have placed Call doc.Save( True, True )

inside the while loop. But still it is not working…

Subject: RE: Replace missing values in a view, ReplaceItemValue is not working

As far as I know the method getItemValue always return a value. If the field does not exists => return value is an empty string.

So your statement isNull(div) is never true.

Also I don’t see how the forall loop can be useful except if your field Division is a richtext field. So if it’s not a richtext get rid of the forall loop.

So if you want to add the value 122 in the field division does not exist just test if the getItemValue is equal to an empty string.

Last thing with the line if doc.hasItem(“Division”), if the doc does not have a field Division, it won’t be set with the new value.

As Arshad said put the doc save inside the if statement to save the change for each doc.

Here is a modified code which should work

Dim session As New NotesSession

Dim db As NotesDatabase

Dim view As NotesView

Dim doc As NotesDocument

dim fieldValue as String

Set db = session.CurrentDatabase

Set view = db.GetView(“Test”)

Set doc = view.GetFirstDocument

While Not(doc Is Nothing)

fieldValue=doc.GetItemValue(“Division”)(0)

If fieldValue=“” Then

call doc.ReplaceItemValue ( “Division”, 122 )

Call doc.Save( True, True )

End If

Set doc = view.GetNextDocument(doc)

Wend

Renaud

Subject: RE: Replace missing values in a view, ReplaceItemValue is not working

Hi Renaud,

This code is working perfect. Thanks for your comments and the help…

Thanks Arshad and Neil.

Subject: RE: Replace missing values in a view, ReplaceItemValue is not working

Could you please post your new code?