Variable not visible to script library sub

Hello, all,

This has got to be something simple I am missing. I have a variable, wholename, that’s dimmed in the Declarations section of an agent script, as is another variable, user:

Dim user As String

Dim wholename As String

(The Options section of the agent uses the “AutoEmail” script library, which contains the sub called in the agent:

Option Public

Use “AutoEmail”)

I define wholename in the agent:

' Get name

	firstname = doc.GetItemValue ("FirstName") (0)  

	lastname = doc.GetItemValue ("LastName") (0)

	wholename = Cstr(firstname) + " " + Cstr(lastname)    '  * * * * * THIS IS NOT CARRYING OVER TO THE SUB * * * * * 

User is defined in the agent as:

user = sess.CommonUserName ' (needed for SendMilitaryLtrs sub)

The agent calls a sub, which uses both variables. The variable “user” works fine:

emdoc.SendTo = addressees

emdoc.CopyTo = copypersons

emdoc.From = user

The variable “wholename” evaluates to an empty string:

Dim rtintervmsg As NotesRichTextItem

Set rtintervmsg = New NotesRichTextItem (emdoc, "Body")

Call rtintervmsg.AppendText ("Dear " + wholename + ":")

(Wholename evaluates correctly in the agent before it hits the call to the sub.)

What’s the difference between these two variables that would make one work fine in the sub and the other not??

TIA,

Mark

Subject: Variable not visible to script library sub

Subject: SOLVED re: Variable not visible to script library sub

Willy,

Thank you so much - as soon as I declared the variables as public in the script library, everything worked! The name popped into the greeting, and the email was sent correctly. (For some reason the line breaks are all screwy in my Yahoo email, when they all look nice in my Outlook, but oh well… )

I understood your explanation of the agent being able to access variables in the library but not vice versa… I had trouble with the scope issue because it’s counter-intuitive to me, if one thinks of the sub being called by the agent as analogous to a subform in a form; you’d think the sub would use whatever is in the agent since the agent is the “parent,” rather than v.v.

I also understood what you were saying about “user,” but I don’t know the answer, because none of the three possibilities you listed applies; “user” is only defined in the agent code.

At any rate, other than the formatting screwing up the line breaks, it works now, so thanks again!

Mark

Subject: RE: SOLVED re: Variable not visible to script library sub

NOW it is only running when I have the debugger on!! If I have it off, it says there is no one to send the email to! How can it successfully send the emails when the debugger is on?

Subject: RE: SOLVED re: Variable not visible to script library sub

I created a new agent, pasted in the code from the original agent, and used the new agent instead. Works fine now w/o the debugger running.

Subject: The “user” variable appears to work because…

…having seen your code, I am assuming that you are the one who tested the agent.

If the From field is blank (and if you ran this script through the debugger you would see that the user variable is a blank variant) Notes uses other means of determining who the sender is. When the agent runs, you are the one who is running it, so the Mail.Send command will assign you as the Sender. So the script isn’t providing the data, the .send command is.

Willy’s suggestion works, but I am more of the mindset to pass your variables from your agent to your sub in the form of parameters:

Sub SendLetters(sender as String, wholeName as String)

Then when you call your sub, you pass the variables to the sub:

Call SendLetters(user,wholeName)

I would also recommend that you declare Option Declare in any script libraries or agents you write. If you had called Option Declare, your script would never have compiled, because user would have not have been declared in your dims. It makes it so that you can organize your code and be sure that every variable has been declared and has a datatype.

just my 2 cents. glad you could solve your issue.

brandt

Subject: RE: The “user” variable appears to work because…

Brandt,

Thanks so much for this info. Thanks for explaining the mystery of “user” - I think I figured out how to see a variable’s value using the debugger. I am going to have to sit and go over the technique of passing the variables from the agent to the sub, as I don’t understand it fully… I get the Option Declare stuff and will start doing that in future scripts.

About posting code: I didn’t want to get flamed about (or worse, ignored because of) posting too much code, as in an apparently angry post I read earlier today about being expected to pore through lines and lines of code… I have read Andre’s CRISPY question guidelines, but sometimes it’s not obvious what code is relevant to the problem. (If I realized what parts were relevant, I might be skilled enough to solve the problem!)

Thanks again for your time and explanations.

Mark

Subject: RE: The “user” variable appears to work because…

Mark,

My suggestion would be to review the Designer documentation about how to build custom classes. It’s a difficult concept to get your head around at first, but once you get it, you can really expand your horizons when it comes to coding these things.

And personally, I prefer more code than less. My experience is that many Notes developers did not start their existences off as programmers and are trying to get competence in how to use LotusScript. Often times, they don’t know where the error is occurring…otherwise, they would be able to answer it themselves!

brandt

Subject: Variable not visible to script library sub

Mark,

Can you post the whole code? Without all of it, this will be hard to diagnose.

brandt

Subject: RE: Variable not visible to script library sub

Of course, sorry, I was trying to only post the relevant lines but I know the context is important…

AGENT CODE:

Sub Initialize

' 12/10/2007: Send interview letters to military applicants



Set sess = New NotesSession

Set db = sess.CurrentDatabase

user = sess.CommonUserName ' (needed for SendMilitaryLtrs sub)

Dim coll As NotesDocumentCollection

Set coll = db.UnprocessedDocuments

Dim doc As NotesDocument, nextdoc As NotesDocument

Set doc = coll.GetFirstDocument



' Set up an output file for problems:

Dim filename As String

Dim filenum As Integer

filename = "H:\My Documents\MilNoEM.txt"

filenum = Freefile()

Open filename For Output As filenum

Dim todaydate As String

todaydate = Today

Print #filenum, "Military Candidates folder auto email interview letters"

Print #filenum, todaydate

Print #filenum,

Print #filenum, "The following applicants do not have email addresses entered so were not emailed interview notices today:"

Print #filenum,

Print #filenum, "LAST NAME"; Tab (24); "FIRST NAME"

Print #filenum,



' Loop thru selected docs:

Do While Not doc Is Nothing

	Set nextdoc = coll.GetNextDocument (doc)

	

' Get name

	firstname = doc.GetItemValue ("FirstName") (0)

	lastname = doc.GetItemValue ("LastName") (0)

	wholename = Cstr(firstname) + " " + Cstr(lastname)    '  * * * * * THIS IS NOT CARRYING OVER TO THE SUB * * * * * 

	

' Make sure there is an email address before proceeding

	emailaddr = doc.GetItemValue ("Email") (0)

	If emailaddr = "" Then			

		Msgbox "No email address for: " + wholename '  * * * ERROR CHECKING * * * 

		

		' LOG THE NAME IN A FILE HERE:

		Print #filenum, Cstr(lastname); Tab (24); Cstr(firstname)

		Goto keepgoing

	Else

		emailstr$ = Cstr (emailaddr)

		Msgbox "Email for " + wholename + ": " + emailstr$    '  * * * ERROR CHECKING * * * 

	End If

	

	'  Call the email sub here

	SendMilitaryLtrs

keepgoing:

	Delete doc

	Set doc = nextdoc

Loop



Print #filenum,

Print #filenum, "All others currently in the folder but not listed above were sent emails today."

Close filenum



Msgbox "Done sending emails!  See MilNoEM.txt under My Documents on your H drive for applicants with no emails.", 0 + 64 + 0 + 0, "Completed"

End Sub

SUB CODE:

Sub SendMilitaryLtrs

'12/11/2007: To auto e-mail interview letters to military applicants



Set sess = New NotesSession

Set db = sess.CurrentDatabase

Set emdoc = New NotesDocument (db)



emdoc.Form = "Memo"



' Set value of email message:

Dim messagetext As NotesRichTextItem

Set messagetext = New NotesRichTextItem (emdoc, "mainmessage")

Dim msgstyle As NotesRichTextStyle

Set msgstyle = sess.CreateRichTextStyle



Call messagetext.AddTab (1)

Call messagetext.AppendText ("We will be conducting interviews at the locations and dates listed on the attached sheet. This will be the only opportunity for interviews.")

Call messagetext.AddNewline (2)

Call messagetext.AddTab (1)

Call messagetext.AppendText ("In order to schedule an interview at one of the designated locations, call during the week of ")

msgstyle.Underline = True

Call messagetext.AppendStyle (msgstyle)

Call messagetext.Update

Call messagetext.AppendText ("December 10-14, 2007, between 8:30 am - 5:00 pm, Eastern Standard Time.  ")

msgstyle.Underline = False

Call messagetext.AppendStyle (msgstyle)

Call messagetext.AppendText ("Locations and times are limited and will be allocated on a first-come, first-served basis.")

Call messagetext.AddNewline (2)

Call messagetext.AddTab (1)

Call messagetext.AppendText ("The interviews will be approximately 1 - 2 hours long and you should plan to arrive at least 15 minutes prior to your scheduled interview.  Please bring ")

msgstyle.Bold = True

Call messagetext.AppendStyle (msgstyle)

Call messagetext.AppendText ("two (2) forms of personal identification; one (1) must be with a photo.")

msgstyle.Bold = False

Call messagetext.AppendStyle (msgstyle)

Call messagetext.AddNewline (2)

Call messagetext.AddTab (1)

Call messagetext.AppendText ("Failure to contact us and schedule an interview in the specified time frame will result in your name being removed from further consideration.")

Call messagetext.AddNewline (2)

Call messagetext.AddTab (1)

Call messagetext.AppendText ("Thank you for your interest.")

Call messagetext.AddNewline (2)

Call messagetext.AddTab (6)

Call messagetext.AppendText ("Sincerely,")

Call messagetext.AddNewline (3)

Call messagetext.AddTab (6)

Call messagetext.AppendText ("Office of Recruitment")



Dim rtintervmsg As NotesRichTextItem

Set rtintervmsg = New NotesRichTextItem (emdoc, "Body")

Call rtintervmsg.AppendText ("Dear " + wholename + ":")    '  THIS IS NOT CARRYING OVER FROM THE AGENT * * * * 

Call rtintervmsg.AddNewline (1)

Call rtintervmsg.AddNewline (1)

Call rtintervmsg.AppendRTItem (messagetext)



' Send auto email notification

addressees(0) = emailstr$	' ADD BACK IN AFTER TESTING	   '  THIS IS NOT CARRYING OVER FROM THE AGENT, EITHER * * * * 

'copypersons(0) = "person@us.com"    ' ADD BACK IN AFTER TESTING

emailsubj = "Interviews"

emdoc.SendTo = addressees

emdoc.CopyTo = copypersons

emdoc.From = user

emdoc.MemoDt = Today

emdoc.Subject = emailsubj



Call emdoc.Send (False)

End Sub

user carries over into the sub just fine. wholename is apparently not available in the sub. Also, I switched the addressee from my own hard-coded email address (for testing) to the emailstr$ variable, and that is not carrying over, either, so I guess that is the same problem. The users are willing to forego the personalized email greeting using wholename if necessary, but obviously if emailstr$ is not available, it won’t work at all!

It must be a scope problem, yes?? I was next going to try copying the agent code into a button instead of having it in the Actions menu… Failing that, I was going to just copy all the sub code into the agent.

TIA,

Mark

P.S. (The formatting doesn’t work, either, but I’ve researched that one, nothing I’ve tried has changed, and at this point the users don’t care about bolding and underlining, as long as we can solve the email address issue.)