GetFirstItem method, or somehow loop through fields on a document

Hi,I have some code at the end of this post, part of an agent I am writing.

Basically, it looks for a certain field on the document. If the field doesn’t have any embedded objects then, set the field to blank. If it has any embeddeded attachments in that field, remove them looping until they are all gone.

Having done that, if the field still isn’t blank (the user entered text and attachments for example), then set it to blank and reload user interface doc…

This is working, but there is now other fields I want to blank out. To avoid reems and reems of code, is it possible to alter this so that, it scrolls through the fields…

e.g. as well as doing all that to the RGDocumentation1 field, it does all the same to about four other fields?

Here’s the code:

'blank out RT fields. Current setup will remove Attachments & Text.

'User should not enter other type

Set item = doc.GetFirstItem(“RGDocumentation1”)

If Isempty (item.EmbeddedObjects) Then

doc.RGDocumentation1=“”

Call uidoc.Reload

Exit Sub

End If

If item.EmbeddedObjects(0).Type <> EMBED_ATTACHMENT Then

doc.RGDocumentation1=“”

Call uidoc.Reload

Exit Sub

Else

Forall o In item.EmbeddedObjects

Call item.EmbeddedObjects(0).Remove

End Forall

End If

If doc.RGDocumentation1<> “” Then

doc.RGDocumentation1= “”

Call uidoc.Reload

End If

End If

'end code=============

The other fields I’d like to blank out are called:

RGDocumentation1_1

RGDocumentation2

RGDocumentation2_1

Subject: GetFirstItem method, or somehow loop through fields on a document

doc.Items ? Keep your required fields in an array and loop through and check against your array and perform the appropriate action.

http://www.stevecastledine.com

Subject: RE: GetFirstItem method, or somehow loop through fields on a document

Ah but I don’t want all the items in the document, only the ones I specified…Items property.

Read-only. All the items in a document. An item is any piece of data stored in a document.

Subject: RE: GetFirstItem method, or somehow loop through fields on a document

I have done this to remove all instances of a RTF:

While Doc.HasItem(“yourRTField”)

Call Doc.RemoveItem(“YourRTField”)

Wend

Subject: RE: GetFirstItem method, or somehow loop through fields on a document

Exactly - loop through all items check against a pre-determined list (your array list of the fields you want to access) and only “process on the ones in the list”. This also makes it changeable in the future when you want another field just by adding to array.

You need to do a getfirstitem to get the item for edit mode - but you know it exists because it was in the doc.items

You can also just use an array and call a function that loops through your array and does the hasitem test and then does the work?

Subject: GetFirstItem method, or somehow loop through fields on a document

Hey try this…

Dim s As New notessession

Dim doc As notesdocument

Dim temp As notesdocument

Dim rtitem As notesrichtextitem

Dim rtf As string

rtf=“Body”

Set db=s.currentdatabase

Set doc=s.DocumentContext

Set rtitem=doc.getfirstitem(rtf)

Set temp=db.createdocument

Call doc.CopyAllItems(temp)

Call temp.RemoveItem(rtf)

Dim rttemp As New notesrichtextitem(temp, rtf)

Call rttemp.Appendtext(rtitem.text)

Forall o In rtitem.embeddedobjects

If o.type=EMBED_ATTACHMENT Then

Call o.extractfile("C:" + o.source)

Call rttemp.embedobject(Embed_Attachment, “”, "C:" + o.source)

Kill "C:" + o.source

End If

End Forall

Call temp.save(True, True)

Subject: GetFirstItem method, or somehow loop through fields on a document

I might be missing something here, but are you just saying that you want to blank the fields? You talk about embedded objects, but essentially all you’re doing is removing all content from the field completely.

If you’re doing this in the ui, all you need is:

call uidoc.fieldsettext(“RGDocumentation1”, “”)

That’s it - no need for a reload or anything, just set the field to blank.

I think I’m wrong obviously, because it can’t be that simple, but it’s not as if you’re saving the attachments anywhere first, so I can’t see what my code doesn’t do that yours does.

Maybe you need to explain a bit more…

Dan

Subject: RE: GetFirstItem method, or somehow loop through fields on a document

Dan,I’m not saying you are wrong…

In my experience, doing what you say, appears to create a wierd attachment at the end of the document, even with a $v2attachmentoptions set to “0”.

Additionally, I need to display the changes on the screen for the user and changing Rich Text fields, in the UI, again IMHO, means that the document would need closing and re-opening.

Anyway, thanks for your help everyone, I’ve fixed it now, here is my code:

Dim x As Integer

x = 0

Do While x <=3

If x=0 Then Set item = doc.GetFirstItem(“RGDocumentation1”)

If x=1 Then Set item = doc.GetFirstItem(“RGDocumentation2”)

If x=2 Then Set item = doc.GetFirstItem(“RGDocumentation1_1”)

If x=3 Then Set item = doc.GetFirstItem(“RGDocumentation2_1”)

If Isempty (item.EmbeddedObjects) Then

itemName = item.Name

doc.itemName=“”

Call uidoc.Reload

Exit Sub

End If

If item.EmbeddedObjects(0).Type <> EMBED_ATTACHMENT Then

itemName = item.Name

doc.itemName=“”

Call uidoc.Reload

Exit Sub

Else

Forall o In item.EmbeddedObjects

Call item.EmbeddedObjects(0).Remove

End Forall

End If

If doc.RGDocumentation1<> “” Then

itemName = item.Name

doc.itemName=“”

Call uidoc.Reload

End If

x=x+1

Loop

End If

Subject: RE: GetFirstItem method, or somehow loop through fields on a document

Don’t know about the weird attachment at the end of the document thing, so that may be a problem.

As for changing rich text items in the ui, that only applies if you’re using back-end methods (as you are in your code), so just blanking out the ui field will work, but using the methods you’re using it is a problem, yes

Dan