doc.Remove doesn´t work on web

Hi,

I have an embedded view with checkboxes that I made myself in the view. On the form I have a field with the same name as the checkboxes. The value of the checkboxes are “DocumentUniqueID”. But when I use this code in a agent, which I call from a button, I can se that the field on the form gets “DocumentUniqueID” from the documents I choose in the view. But the choosen document are still there. This is a webapplication. Here is my agent code:


Dim session As New NotesSession

Dim db As NotesDatabase

Dim context As NotesDocument

Dim doc As NotesDocument

Dim item As NotesItem

Dim value As String

Dim Test As String

Dim a As String



Set db = session.CurrentDatabase

Set context = session.DocumentContext

Set item  = context.GetFirstItem("DeleteDocs")

Counter = 0



Forall v In item.values

	If Counter <> "0" Then

		Set doc = db.GetDocumentByUNID(v)

		Call doc.Remove( True )

	End If

	Counter = Counter + 1

End Forall



Print "<script>"

Print "history.go(-1);"

Print "</script>"

If I print a javascript alert with all values (DocumentUniqueID, Username, Form, and so on), it looks alright.

Is something wrong with my agent?

Best Regards

Henrik

Subject: doc.Remove doesn´t work on web

The code looks fine.

Only questions is, why are you using Counter variable. It isn’t required out there.

Also, use msgbox statements to debug the code.

–Phani

Subject: RE: doc.Remove doesn´t work on web

Why would you want to use MsgBox calls to debug a web agent?

Subject: RE: doc.Remove doesn´t work on web

Because it prints out the values you use to the Domino server console, which would indicate the progress and helps you out to find what exactly went wrong.

–Phani

PS - There are various ways to debug a web agent (like - on error statement, agent log, print statements etc) which I forgot to mention in my earlier post.

Subject: RE: doc.Remove doesn´t work on web

True, but for on the fly debugging, printing to the browser page is so much more convenient. As a rule, instead of using direct Print statements, I would recommend calling a custom function, eg. “DebugPrint( debugLine As String )”, with a single line Print debugLine this line can then be remm’d/unremm’d when you need all debugging off/on.

Subject: doc.Remove doesn´t work on web

You use counter as a number, but you compare it with a string.

If Counter <> “0” Then (this is always true, so a document will never be removed)

should be

If Counter <> 0 Then

Regards,

René

Subject: RE: doc.Remove doesn´t work on web

Rene, if the statement is returning “true” then the code to remove the document will execute for sure.

–Phani

Subject: RE: doc.Remove doesn´t work on web

Phani,

D’oh! You’re right. I’m glad it’s almost weekend, i obviously can use some rest. :wink:

My guess is that the 1st value of the item does not contain a document id. (since you don’t want the remove action to be performed if counter is 0). If this is so, the agent will generate an error when the counter is 0. Because the statement returns true, the agent will try to get a document by a UNID that does not exists.

Did you try counter<>0?

Regards,

René

Subject: RE: doc.Remove doesn´t work on web

Hi Henrik,I faced the same issue in my application. I made a work around for this issue by placing some flag variable DELETEFLAG and set the value as “Y” when the deletion is required in the document and include the selection criteria of the view displayed as dELETEFLAG!=“Y”. Automatically the document is removed from the view.

Run some scheduled agent to remove all the documnets with the deleteflag as Y. Hope this may help you.

Subject: RE: doc.Remove doesn´t work on web

This is what I did too, 3 years back when developing a web application. Help file clearly says that “You cannot use the remove method on a Document object returned by the DocumentContext property.”

Subject: RE: doc.Remove doesn´t work on web

Indeed, but he in his code he isn’t deleting the DocumentContext note object.

Subject: RE: doc.Remove doesn´t work on web

Counter = 0

Forall v In item.values

If Counter <> “0” Then

Set doc = db.GetDocumentByUNID(v)

Call doc.Remove( True )

End If

Counter = Counter + 1

End Forall

To make testing easier here, put in an error trap and print out code to throw a js alert with the error number, message, and line. Alternatively, you can temporarily set this up to run from the client (hardcode the UNIDS found in the field into an array) and step through it in the debugger. For Best Practices, Use Option Explicit and declare all of your variables.

Also, I don’t see why this wouldn’t work. You should not need a counter here. Loop through the field using forall and then get a handle to the document using the unid you obtain from the field. There is no need to tally the number of passes.

Sub Initialize

Dim session As New NotesSession

Dim db As NotesDatabase

Dim context As NotesDocument

Dim doc As NotesDocument

’ Dim item As NotesItem

Dim value As String

Dim Test As String

Dim a As String



Set db = session.CurrentDatabase

’ Set context = session.DocumentContext

’ Set item = context.GetFirstItem(“DeleteDocs”)

Forall v In item

	Set doc = db.GetDocumentByUNID(v)

	Call doc.Remove( True )

End Forall



Print "<script>"

Print "history.go(-1);"

Print "</script>"

End Sub

Subject: RE: doc.Remove doesn´t work on web

That’s true. Sorry for the confusion. I didn’t mean to say that he is removing the doc object set by document context.