Declaring a view

Hi guys,

I have a web application. On a form I have an agent that runs on the WebQuerySave. Nothing special in the agent, just the basic send email. The agent is written in LotusScript. I have a problem with declaring/setting a view. This is how I declare my view:

Dim AccessRequest As notesview

This is how I set my set my view:

Set AccessRequest = db.getview(“AccessRequest”)

I have two issues. When the above code is written one line after each other i.e.:

Dim AccessRequest As notesview

Set AccessRequest = db.getview(“AccessRequest”)

The details that were filled in on the form can be viewed in the view however, the code within the agent does not run and I get this error message on the server:

Agent ‘agentname’ error: Object variable not set

When I place the Dim and Set away from each other (20 lines apart with other bits of code in between) the agent runs fine however the details on the view are not processed/saved in the view.

Any ideas?

Subject: Declaring a view

“When I place the Dim and Set away from each other (20 lines apart with other bits of code in between) the agent runs fine”

db is set somewhere in between.

“however the details on the view are not processed/saved in the view”

I have no idea what you mean by that.

Subject: RE: Declaring a view

Hi Harkpabst,

db is set at the top of the code, before the Dim and Set.

Sorry i meant to say ‘‘however details on the form are not processed/saved in the view’’

Viraj

Subject: RE: Declaring a view

OK, you should probably post more bits of the code then. There is absolutely nothing, that would keep the initialization of a Notes object from working, if it is placed directly behind the declaration. So, the solution must be somewhere in the code in between.

If you make changes to the doc, that should be reflected in the view right in this piece of code, you might have to call the view’s refresh method to pick up the changes. However, in this case, why do you have to access the data from the view, instead of from the document directly?

Subject: RE: Declaring a view

Hi guys,

Thank for all your help. I have managed to resolve it now. Simple spelling mistake!!

Viraj

Subject: RE: Declaring a view

Hi guys,

I thought i had fixed the problem unfortunately it did not work.

Here is some of my code:

Sub Initialize

Dim session As New NotesSession

Dim db As NotesDatabase

Dim names As NotesDatabase

Dim view As notesview



Dim AccessRequest As notesview

Set AccessRequest = db.getview("AccessRequest")



Dim namesView As NotesView

Dim emailView As NotesView

Dim NameView As NotesView

Dim shortNView As NotesView



Dim doc As NotesDocument

Dim doc2 As NotesDocument

Dim doc3 As NotesDocument

Dim doc4 As NotesDocument

Dim doc5 As NotesDocument

Dim doc6 As NotesDocument

Dim emailDoc As NotesDocument

Dim nameDoc As NotesDocument

Dim userNameDoc As NotesDocument

Dim userDoc As NotesDocument



Dim emailCol As NotesDocumentCollection

Dim userCol As NotesDocumentCollection

Dim nameCol As NotesDocumentCollection

Dim uNameCol As notesdocumentcollection



Dim email As String

Dim shortName As String

Dim Uname As String

Dim User As String

Dim FirstApprover As String

Dim FirstApproverEmail As String

Dim SecondApprover As String

Dim SecondApproverEmail As String

Dim status As String

Dim SaveRef As String

Dim DocID As String

	

Dim UNID List As String

Dim outp As String

Dim i As Integer



Dim z As String

Dim resetDocID As String

Dim namesDocID As String

Dim resetDoc As NotesDocument

Dim namesDoc As NotesDocument

'Dim outp As String



Set db = session.CurrentDatabase

Set doc = session.DocumentContext

Set doc2 = session.DocumentContext

Set doc3 = session.DocumentContext

Set doc4 = session.DocumentContext

Set doc5 = session.DocumentContext

Set doc6 = session.DocumentContext



'Set doc = db.createdocument



user = doc.user(0)

email = doc.EmailAddressAuto(0)

FirstApprover = doc.FirstApprover(0)

FirstApproverEmail = doc.FirstApproverEmail(0)

SecondApprover = doc.SecondApprover(0)

SecondApproverEmail = doc.SecondApproverEmail(0)

SaveRef = doc.SaveRef(0)

DocID = doc.DocID(0)

Subject: RE: Declaring a view

Did you read the previous answers?

They suggested you hadn’t set db before trying to use it to get the view. This is exactly what has happened - your set db = session.CurrentDatabase line is way below you db.getview line.

Simply move the set db line to the top as previously suggested

Subject: RE: Declaring a view

Hi Dan,

Yes i did read the previous answers.

I have already this. Yes the agent runs and send emails but the detail that were input onto the form are niot saved in the view.

Viraj

Subject: RE: Declaring a view

Well, as you don’t post your whole code (or updated code, as the code you’ve posted would still give you an error), it’s hard to tell what the problem is. In the code you’ve given you don’t save doc, but then no-one can tell if you’ve done that later or not. And why you’ve declared multiple documents as documentcontext is anybody’s guess.

Subject: RE: Declaring a view

whole code:

Sub Initialize

Dim session As New NotesSession

Dim db As NotesDatabase

Dim names As NotesDatabase

Dim view As notesview

'Set db = session.CurrentDatabase



Dim AccessRequest As notesview

Set AccessRequest = db.getview("AccessRequest")





Dim namesView As NotesView

Dim emailView As NotesView

Dim NameView As NotesView

Dim shortNView As NotesView



Dim doc As NotesDocument

Dim doc2 As NotesDocument

Dim doc3 As NotesDocument

Dim doc4 As NotesDocument

Dim doc5 As NotesDocument

Dim doc6 As NotesDocument

Dim emailDoc As NotesDocument

Dim nameDoc As NotesDocument

Dim userNameDoc As NotesDocument

Dim userDoc As NotesDocument



Dim emailCol As NotesDocumentCollection

Dim userCol As NotesDocumentCollection

Dim nameCol As NotesDocumentCollection

Dim uNameCol As notesdocumentcollection



Dim email As String

Dim shortName As String

Dim Uname As String

Dim User As String

Dim FirstApprover As String

Dim FirstApproverEmail As String

Dim SecondApprover As String

Dim SecondApproverEmail As String

Dim status As String

Dim SaveRef As String

Dim DocID As String

Dim Team As String



Dim UNID List As String

Dim outp As String

Dim i As Integer



Dim z As String

Dim resetDocID As String

Dim namesDocID As String

Dim resetDoc As NotesDocument

Dim namesDoc As NotesDocument

'Dim outp As String



Set db = session.CurrentDatabase

Set doc = session.DocumentContext

Set doc2 = session.DocumentContext

Set doc3 = session.DocumentContext

Set doc4 = session.DocumentContext

Set doc5 = session.DocumentContext

Set doc6 = session.DocumentContext



'Set doc = db.createdocument



user = doc.user(0)

email = doc.EmailAddressAuto(0)

FirstApprover = doc.FirstApprover(0)

FirstApproverEmail = doc.FirstApproverEmail(0)

SecondApprover = doc.SecondApprover(0)

SecondApproverEmail = doc.SecondApproverEmail(0)

SaveRef = doc.SaveRef(0)

DocID = doc.DocID(0)

Team = "Viraj.Patel@europe.co.uk"

		

Select Case SaveRef

	

	

Case "FirstApproverReject"

	

	Msgbox "START - First Approver Reject"

	

	doc.form = "Memo"

	doc.from = "New"

	doc.subject = " Access Request"

	doc.Body = " Access Request has been Rejected"

	

	Call doc.send(False, email)

	

	Msgbox "END - First Approver Reject"

	

	Call doc.Save(True, False)

	

				

Case "FirstApprove"

	

	Msgbox "START - First Approve"

	

	doc.form = "Memo"

	doc.from = "New"

	doc.subject = " Access Request"

	doc.Body = "(user email) "

	

	Call doc.send(False, email)

	

	doc2.form = "Memo"

	doc2.from = "New "

	doc2.subject = " Access Request"

	doc2.Body = " Approver"

	

	Call doc2.send(False, FirstApproverEmail)

	

	Msgbox "END - First Approve"

	

	Call doc.Save(True, False)

	

			

Case "FinalApprove"

	

	Msgbox "START - Final Approve"

	

	doc.form = "Memo"

	doc.from = "New "

	doc.subject = " Access Request"

	doc.Body = " Access Request is now complete (user email) "

	

	Call doc.send(False, email)

	

	doc4.form = "Memo"

	doc4.from = "New "

	doc4.subject = " Access Request"

	doc4.Body = "Team"

	

	Call doc4.send(False,Team)

	

	Msgbox "END - Final Approve"

	

	Call doc.Save(True, False)

	

End Select

End Sub

Subject: RE: Declaring a view

From what I can tell, the line of code where it initilizes the db variable is commented out (it has a ` next to “Set db…”).

Hope that helps.

Subject: RE: Declaring a view

I think you need to tell us what you’re trying to achieve here. What does the user update on the web, and as a result of the update what are you trying to do with the form, and what alerts are you wanting to send?

There’s a lot of code you don’t need here - for instance your original question was about setting the view (which still won’t work with the code you’ve just posted). But then you don’t use the view, so why are you getting it? There’s also no need to set 6 documents to the current document, you only need 1, but you still only use 4 so why 6?

As I say write it down in plain English and maybe we can help you out.

Dan

Subject: RE: Declaring a view

Hi Dan,

I have a form with various text, editable, computed… fields on it. Users can log onto the form and fill in the details. Once form has been completed the user clicks on a ‘submit’ button. This button has some simple JavaScript validation with a submit() at the end.

As the form is submitted a WebQuerySave agent is launched (name - sendEmail). This agent currently sends an email to the user that completed the form. Another email is also sent to an approver.

I have a view named AccessRequest. When I open this view from the client I want to be able to see the details that the user completed on the form. I have already configured this view to show specific details that were completed.

The problem I having is that:

i. When the Set AccessRequest = db.getview(“AccessRequest”) is placed BEFORE the Set db = session.CurrentDatabase the code within the agent does not fully execute. However, the view within the client IS updated with a new record.

ii. When the Set AccessRequest = db.getview(“AccessRequest”) is placed AFTER the Set db = session.CurrentDatabase the code within the agent does fully execute. However, the view within the client IS NOT updated with a new record.

Subject: RE: Declaring a view

OK, a few things then.

You don’t need to get the view at all - you’re not actually doing anything with it, the view is simply showing the documents, so when the document updates the view will show the new values.

In the agent you’re changing the documentcontext document and sending it by email. You don’t want to do this as this will mess up your access requests view (which is why when the agent completes your view is now wrong).

This is completely untested but should be roughly what you need. Notice I don’t do anything, or resave doc, as this is done anyway, I’m creating new documents to email. I’ve also removed a load of code and variables that you weren’t using.

Sub Initialize

Dim session As New NotesSession

Dim db As NotesDatabase

Dim names As NotesDatabase

Dim view As notesview

Set db = session.CurrentDatabase

Dim doc As NotesDocument

Dim emailDoc As NotesDocument

Dim email As String

Dim shortName As String

Dim Uname As String

Dim User As String

Dim FirstApprover As String

Dim FirstApproverEmail As String

Dim SecondApprover As String

Dim SecondApproverEmail As String

Dim Team As String

Dim SaveRef As String

'**** the current document, used to retrieve values for the emails only **********

Set doc = session.DocumentContext

‘******* create a new document to send by email *******’

set emaildoc = db.createdocument

user = doc.user(0)

email = doc.EmailAddressAuto(0)

FirstApprover = doc.FirstApprover(0)

FirstApproverEmail = doc.FirstApproverEmail(0)

SecondApprover = doc.SecondApprover(0)

SecondApproverEmail = doc.SecondApproverEmail(0)

Team = “Viraj.Patel@europe.co.uk

SaveRef = doc.SaveRef(0)

Select Case SaveRef

Case “FirstApproverReject”

Msgbox “START - First Approver Reject”

emaildoc.form = “Memo”

emaildoc.from = “New”

emaildoc.subject = " Access Request"

emaildoc.Body = " Access Request has been Rejected"

Call emaildoc.send(False, email)

Msgbox “END - First Approver Reject”

Case “FirstApprove”

Msgbox “START - First Approve”

emaildoc.form = “Memo”

emaildoc.from = “New”

emaildoc.subject = " Access Request"

emaildoc.Body = "(user email) "

Call emaildoc.send(False, email)

'******* just create emaildoc again to send another email

set emaildoc = db.createdocument

emaildoc.form = “Memo”

emaildoc.from = "New "

emaildoc.subject = " Access Request"

emaildoc.Body = " Approver"

Call emaildoc.send(False, FirstApproverEmail)

Msgbox “END - First Approve”

Case “FinalApprove”

Msgbox “START - Final Approve”

emaildoc.form = “Memo”

emaildoc.from = "New "

emaildoc.subject = " Access Request"

emaildoc.Body = " Access Request is now complete (user email) "

Call emaildoc.send(False, email)

set emaildoc = db.createdocument

emaildoc.form = “Memo”

emaildoc.from = "New "

emaildoc.subject = " Access Request"

emaildoc.Body = “Team”

Call emaildoc.send(False,Team)

Msgbox “END - Final Approve”

End Select

End Sub

Subject: RE: Declaring a view

Oops, could have saved my posting, if I had had another view at the thread …

Subject: RE: Declaring a view

“i. When the Set AccessRequest = db.getview(“AccessRequest”) is placed BEFORE the Set db = session.CurrentDatabase the code within the agent does not fully execute. However, the view within the client IS updated with a new record.”

The doc will be displayed in the view, because your code didn’t have a chance to torture it. :slight_smile: But of course, db.GetView() HAS to follow the initialization of db, or your agent will never run.

"ii. When the Set AccessRequest = db.getview(“AccessRequest”) is placed AFTER the Set db = session.CurrentDatabase the code within the agent does fully execute. However, the view within the client IS NOT updated with a new record. "

After running your code, the document will e.g. have a Form item of “Memo”. Does your view selection formula recognize it? It will be there in your database, it will just not be displayed in your view.

DocumentContext is the document, that is about to be written to the database. As Dan said, you don’t need 4 or 6 variables, all pointing to the same object, you need only one.

When sending emails, do not send the DocumentContext. Create new NotesDocument objects instead, send them, but don’t save them.

Finally, do NOT call NotesSession.DocumentContext.Save() in a WebQuerySave agent. The document is going to be saved anyway. This for a start. Try to cleanup and modify your code accordingly.

Subject: Some useful error trapping code

would be good

Firstly add an explicit error handler that gives more info into the log

sub Inititalise

On error goto trap

… all your code here

outofhere:

exit sub

Trap:

MsgBox {WQS whatever error } & err { at } & erl & {:} & error$

resume outofhere

end sub

This will trap any automatically generated errors

Secondly put some defensive code like :

Dim AccessRequest As notesview

Set AccessRequest = db.getview(“AccessRequest”)

if AccessRequest is Nothing then Error 1000, “Could not get AccessRequest view”

Note: Errors 1000 - 1999 are reserved for the coder to initiate themselves

Puts as many of these as you can without affecting code efficiency

Subject: RE: Declaring a view

Is the “object variable not set” error definately occurring on the db.getview line?

Subject: RE: Declaring a view

Hi Mike,

Yup 100%

Viraj