Notes Curiosities: Script in an action button in a form

Hi all,

found a strange behaviour in one of our forms. The following fragment of script code belongs to an action button:

…

x$=Doc.GetItemValue(“Doc_subject”)(0)

x$=Doc.Doc_Subject(0)

Call UiDoc.Refresh(True)

x$=Doc.GetItemValue(“Doc_subject”)(0)

x$=Doc.Doc_Subject(0)

…

The first 4 lines are executed without any problem, but the 5th line raises an error:

“Variant does not contain a container”

Any idea what this feature is good for ?

Greetings, Joe

Subject: Notes Curiosities: Script in an action button in a form …

x$ = doc.GetItemValue(“Doc_Subject”)(0)…

Lose the x$ and (0), as GetItemValue doesn’t take array values, and returns a variant not a string.

You need to throw the ‘doc_subject’ values into a variant, then get the (0) value, ie

Dim subj As Variant

subj = doc.GetItemValue( “Subject” )

x$ = subj(0)

Why not just do:

x$ = doc.doc_subject(0)

if you are sure its a string with only one value? Seems a long way round a simple thing.

Dave

Subject: RE: Notes Curiosities: Script in an action button in a form …

Hi David,

thanks for your answer, but the problem was the command

x$=Doc.Doc_Subject(0)

and not the

x$ = doc.GetItemValue(“Doc_Subject”)(0)

The second is ok, because GetItemvalue returns an array of string and I just take the value of the first field (0). So no problems wit hthis command.

The problem is that Notes delivered a value in the line

x$=Doc.Doc_Subject(0)

and raised an error after the Refesh command was executed though the command was absolutly equal.

Joe

Subject: RE: Notes Curiosities: Script in an action button in a form …

x$ = doc.GetItemValue(“Doc_Subject”)(0)…

Lose the x$ and (0), as GetItemValue doesn’t take array values, and returns a variant not a string.

right for the wrong reasons though (bracket order is important)

doc.GetItemValue(“Doc_Subject”) will return a variant

doc.GetItemValue(“Doc_Subject”)(0) wil return the zero’th element from that variant

x$ forces a conversion from variant to string (not 100% sure about forced, it may barf on a non string)

Subject: Notes Curiosities: Script in an action button in a form …

You are wasting memory by using all those implicitly defined variants. Use this syntax instead:

dim item as notesitem

dim x as string

set item = doc.getfirstitem(“Doc_subject”)

x = item.text

OR

x = item.values(0)

Also, if it happens that no Doc_Subject item exists, you will get the more helpful “Object variable not set” message.

GetItemValue is more convenient if the item in question might be a Rich Text item.

Subject: RE: Notes Curiosities: Script in an action button in a form …

the short syntax is not that reliable, which is why I avoid it.

What if you have a fieldname on a document that ends up being used as NotesDocument class property/method in a new release? (I’ve seen this in the past).

just my 2cts, Cheers

IRC: Woonjas on EFNet’s notes

Subject: RE: Notes Curiosities: Script in an action button in a form …

Sorry, but I’m not wasting any memory at all cause I naturally have defined all variables as in my client Option Declare is automatically set and I can’t use variables which were not defined before ! I just don’t want to write unnecessary information and waste others space and attention in this forum, so that I don’t repeat it and it wouldn’t change the result of what I have found out. Thats why I have choosen the more convientient and obvious way.

Subject: RE: Notes Curiosities: Script in an action button in a form …

You are wasting memory by using all those implicitly defined variants. Use this syntax instead:

do you realise that notes will allocate memory for all variables, declared and undeclared, used in a script sub/function, so it’s impossible to ‘waste’ memory via the method you do or don’t declare variables - check the variables page on the debugger, every variable youre about to use is listed there, whether you dim’d it or not.

btw. the better choice is to use NotesDocument.HasItem(“fieldname”) before accessing the contents of any field, but that requires more code. it all really depends upon exactly how sure you are that a field will exist on your document at the time the script runs.

sort of like some C programmers and malloc(), you can add extra code to check if the memory allocation worked but if you’re out of memory it’s not like you can do anything about it anyway so why bother.

Subject: Notes Curiosities: Script in an action button in a form …

how are you setting Doc? and does Doc change after the uidoc.Refresh? can we see more of the code?

x$=Doc.GetItemValue(“Doc_subject”)(0)

x$=Doc.Doc_Subject(0)

why are you effectively doing the same thing twice? you only need one of them. or are you trying to figure out why one works and the other doesn’t?

try;

If Doc.HasItem(“Doc_Subject”) then

x$=Doc.GetItemValue("Doc_subject")(0)

End If

or build a function to do it for you if you have a lot of them

Subject: RE: Notes Curiosities: Script in an action button in a form …

how are you setting Doc?

Set Doc=notesUIWorkspace.CurrentDocument.Document

and does Doc change after the uidoc.Refresh?

No, obvious visible change

can we see more of the code?

Sure but there is nothing else of importance concerning this behaviour

why are you effectively doing the same thing twice? you only

need one of them. or are you trying to figure out why one

works and the other doesn’t?

Yes, the second assumption is right. I found this strange behaviour by chance and wanted to isolate the circumstances.

Subject: Notes Curiosities: Script in an action button in a form …

I would use the debugger to step through the code to see if the Doc-object still exists correctly. If it does - I would check the spelling on the field-name; I noticed that You in the last line used a capital ‘S’ in Subject instead of a small ‘s’ as You did in the previous lines.

Last; I wouldn’t recommend using underscore in fieldnames since I have had many bad experiences with that - for no appearent reason.

hth

Subject: RE: Notes Curiosities: Script in an action button in a form …

I’ve stepped through it with the debugger and the Doc document seemed to exist correctly after the Refresh-command. The case of the first char had also no influence and the _ character is a valid char for fieldnames in Notes.

This example shows that the short command

Set Var=Doc.{fieldname}

and the long command version

Set Var=Doc.GetItemValue(“{fieldname}”)

are inside Notes not(!) the same routines and may deliver, like in this case, different results !

Joe