LOTUSSCRIPT - what am I doing wrong?

I need a fresh pair of eyes! Several, in fact!

This script is supposed to find all documents with the PC Configuration form, then spin through the response documents and update any of them that match the info in the form that just got saved. The script finds and updates one response document only. When I debug, I see that after the first response document is updated (doc_Pchange), every other instance where it should be finding a match, AP_Name SHOULD be pulling in the actual value from the response document, but it’s getting the new value (if that has changed) preventing the update. I hope that makes sense. Basically if there are 5 PC Configurations with response documents that need updating, only the first 1 that is found gets changed. Please help. Thanks,

Trish

Sub Queryclose(Source As Notesuidocument, Continue As Variant)

'Written 08/07 by Tricia Pulley, Cadence Technologies

'Revised 11/07 by Tricia Pulley



'IF THIS COMPONENT HAS BEEN UPDATED, CHANGE THE INFORMATION FOR ALL THE 

'PC CONFIGURATIONS USING THIS COMPONENT (DIALOG F_ASSPART)

Dim PT_Name As String

Dim PT_Type As String

Dim PT_Cost As String

Dim PT_Vendor As String

Dim PT_Track As String

Dim PT_Default_Today As String

Dim PT_TrackTitle As String

Dim PT_DefaultValue As String



PT_Name = Cstr(doc_Current.PT_Name(0))

PT_Type = Cstr(doc_Current.PT_Type(0))

PT_Cost = Cstr(doc_Current.PT_Cost(0))

PT_Vendor = Cstr(doc_Current.PT_Vendor(0))

PT_Default_Today = Cstr(doc_Current.PT_Default_Today(0))

PT_TrackTitle = Cstr(doc_Current.PT_TrackTitle(0))

PT_DefaultValue = Cstr(doc_Current.DefaultValue(0))





'GET ALL PC CONFIGURATIONS. SPIN THROUGH

'AND GET A COLLECTION OF RESPONSE DOCUMENTS FOR EACH PC CONFIGURATION

'IF THERE IS A MATCH, UPDATE THAT DOCUMENT



'GET THE FIRST PC CONFIG DOCUMENT

Dim LkViewPC As NotesView

Set  LkViewPC = db_Current.GetView( "V_CONFIGLOOKUPBYUNID" )

Dim doc_PConfig As New NotesDocument(db_Current)

Set doc_PConfig = LkViewPC.GetFirstDocument

Dim MyUNID As String

MyUNID = Cstr(doc_PConfig.UniversalID)



'IF THERE ARE NO PC CONFIGURATIONS, THEN EXIT SUB

If doc_PConfig Is Nothing Then

	Exit Sub

End If



'BEGIN LOOPING THROUGH ALL CONFIG DOCUMENTS

Do While Not doc_PConfig Is Nothing

	

	'GET ALL RESPONSE DOCUMENTS (F_ASSPART) AND SEARCH FOR A MATCH

	Dim LkViewP As NotesView

	Dim dc_Pcollection As NotesDocumentCollection

	Dim doc_PChange As New NotesDocument( db_Current )	

	Set LkViewP = db_Current.GetView( "V_DIALOGUPDATE" )

	Set dc_Pcollection = LkViewP.GetAllDocumentsByKey(MyUNID)

	Set doc_PChange = dc_Pcollection.GetFirstDocument

	

	'GET THE OLD VALUES FROM THE FORM TO SEE WHAT CHANGED

	Dim Old_PT_Name As String

	Dim Old_PT_Type As String

	Dim Old_PT_Cost As String

	Dim Old_PT_TrackTitle As String

	Dim Old_PT_DefaultValue As String

	Old_PT_Name = Cstr(doc_Current.PT_OldName(0))

	Old_PT_Type = Cstr(doc_Current.PT_OldType(0))

	Old_PT_Cost = Cstr(doc_Current.PT_OldCost(0))

	Old_PT_TrackTitle = Cstr(doc_Current.PT_OldInfoTitle(0))

	Old_PT_DefaultValue = Cstr(doc_Current.PT_OldDefaultValue(0))

	

	'GET THE VALUES FROM THE RESPONSE DOCS TO COMPARE WITH

	Dim AP_Name As String

	Dim AP_Type As String

	Dim AP_Cost As String

	Dim AP_TrackTitle As String

	Dim AP_DefaultValue As String

	

	If dc_Pcollection.Count > 0 Then

		'SPIN THROUGH ALL RESPONSES. IF THERE IS A MATCH, UPDATE IT.

		AP_Name = ""

		AP_Type = ""

		AP_Cost = ""

		AP_TrackTitle = ""

		AP_DefaultValue = ""

		Dim j As Integer

		Do While Not doc_Pchange Is Nothing

			

			AP_Name = Cstr(doc_Pchange.PT_Name(0))  '<---This is returning the wrong value!!!

			AP_Type = Cstr(doc_Pchange.PT_Type(0))

			AP_Cost = Cstr(doc_Pchange.PT_Cost(0))

			AP_TrackTitle = Cstr(doc_Pchange.PT_TrackTitle(0))

			AP_DefaultValue = Cstr(doc_Pchange.PT_Info(0))

			



                            'This section is just to help me see which conditions are true while debugging & will be replaced.

			Dim Match_Name As Boolean

			If Old_PT_Name = AP_Name Then

				Match_Name = True

			Else

				Match_Name = False

			End If

			

			Dim Match_Type As Boolean

			If Old_PT_Type = AP_Type Then

				Match_Type = True

			Else

				Match_Type = False

			End If

			

			Dim Match_TrackTitle As Boolean

			If Old_PT_TrackTitle = AP_TrackTitle Then

				Match_TrackTitle = True

			Else

				Match_TrackTitle = False

			End If

			

			Dim Match_Cost As Boolean

			If Old_PT_Cost = AP_Cost Then

				Match_Cost = True

			Else

				Match_Cost = False

			End If



			

			If Match_Name And Match_Type And Match_TrackTitle And Match_Cost Then	

				doc_PChange.PT_Name = PT_Name

				doc_PChange.PT_Type = PT_Type

				doc_PChange.PT_Cost = PT_Cost

				doc_PChange.PT_InfoTitle = PT_TrackTitle

				doc_PChange.PT_Info = PT_DefaultValue

				Call doc_PChange.Save( True, False)

			End If

’ If j <> dc_Pcollection.Count Then

			AP_Name = ""

			AP_Type = ""

			AP_Cost = ""

			AP_TrackTitle = ""

			AP_DefaultValue = ""

			Set doc_PChange = dc_Pcollection.GetNextDocument(doc_PChange)

’ End If

		Loop

		Set doc_PConfig = LkViewPC.GetNextDocument(doc_PConfig)

	End If

	

Loop

End Sub

Subject: LOTUSSCRIPT - what am I doing wrong?

Subject: RE: LOTUSSCRIPT - what am I doing wrong?

A couple of good hints, but let me add a few more thoughts from a junior developer (compared to you).

"I just don’t understand why people do this…

Do While Not doc_PConfig Is Nothing

Use…

Do Until doc_PConfig Is Nothing

The advantage is that you get to lose your “Exit Sub” code that you have before the loop."

Tricia has the loop construct right already, it’s a pre-test loop as well. It’s just the “If doc_PConfig Is Nothing”, that is not needed. You got trapped by one of the (many) weaker points of LS syntax. Pre-test and post-test loops can both be written using either while or until.

“Dims in the code drive me crazy! It lengthens the code so you can’t see as much of it on your screen as you would if you put all your Dims up top. I find it harder to step through code when I have to break my thought process to scroll the screen.”

I plain love to place Dims right where they belong. Putting them all on top of the code is just a left-over from the days when Basic was … well … too basic to do it the other way. No matter how careful you pick your variable names: from a certain point of complexity on, you always have to scroll back to the top to find out what kind of variable this or that was. If code becomes hard to understand, it’s not because variables are declared where they are needed, but because you didn’t break it down into easy to understand modules. I don’t go as far (as some do) to say, that whatever code module is bigger than what you see on screen at once needs to be broken up into smaller functional units. But it’s surely a guideline. No Java programmer would ever come up with the crazy idea to put all variable declarations far out of sight on top of his code.

Same with prefixing variables of scalar type with something representing the data type more or less clearly. Simply superfluous, if the declaration is never far from where the variable is used and if the name itself makes clear, what the variable is used for.

Also, I wouldn’t be too strict about the use of conversion functions. Even if it is technically not necessary to do an explicit type casting, it might still serve as self-documenting code. If it helps you to understand what you wanted to achieve with a piece of code when you come back to it after a couple of months, it’s been worth the (mostly relatively small) performance price you have to pay for it. Given the old rule, that during the lifecycle of an application 80% of the effort is spent on maintaining and supporting existing code, I’ve learned accept, that “faster” code is not always faster.

Now, if you tell me that all this is just my personal taste: Right. As is any different approach.

And I didn’t even mention my other personal pet-peeves like always using GetItemValue instead of extended class notation, not using underscores in field and variable names, … because it is personal taste, in the end.

Subject: RE: LOTUSSCRIPT - what am I doing wrong?

Subject: RE: LOTUSSCRIPT - what am I doing wrong?

Ah, I hit sombody’s ego. Nevermind.

Don’t know what you are referencing to as being “wastefull” (and also I did not suggest anything regarding the loop, which obviously slipped your attention).

Do While Not doc_PConfig Is Nothing

and

Do Until doc_PConfig Is Nothing

are absolutely and perfectly equal. Period. You’re not counting the number of characters now to call one of them “wasteful”, are you? Let’s face it: Your statement, that only with the latter statement Tricia could get rid of the If clause was plain wrong, wasn’t it? If using the negation operator is against you personal taste or doesn’t suite your way of thinking, don’t use it. But don’t tell others, your way is the only “right” way to do it.

I did not and I will not make any assumptions on how long your average LotusScript is and so I ask you to do the same when it comes to my code. Just the idea that only lengthly code can proof good programming practice is plain absurd. Easy to maintain and understandable, because there are many lines of code and declarations can only be remembered because you have to keep them in a separate Notepad window “for reference”? Back to the 70s and lots of spaghetti code.

For sure, if you need stuff like global variables (next thing you tell me is, that you should use them as often as possible, or what?), the declaration has to go somewhere “on top”. But usually that means into the globals or directly into a script library. But that the pure technical necessity doesn’t make it a good practice for everything. Your pragma, that all variable declarations have always to be treated the same “for consistency” mainly feeds itself and not much more. Code in a library is different from code in a button or in an event by it’s very nature. Why should it all look the same? If this ain’t Java, it ain’t Fortran as well. But still, if your coding to OO principles, it doesn’t really matter if it’s Java or LotusScript or C++. Good design principles are very similar.

Properties in custom classes are often - but not always - best definded using property get and property set, e.g. to allow for lazy initialization (purely a matter of performance, while we’re discussion “waste”). So, basic practical reasons, properties might be defined using two completely different methods. There goes your consistency.

Having variables with a local meaning defined where they are used and those with a more global scope (like simple properties) all in one place can be a great help for the next developer to quickly grasp the parts of the code he needs to understand for performing changes.

All this would not be much of an issue, if you hadn’t come along and rant about somebody else’s style of coding, as if there was one and only one holy grail of LS programming. By chance, the way you do it. That’s pretty big talking, especially if you still advocate, that the results aren’t good enough for serious enterprise needs anyway.

Subject: RE: LOTUSSCRIPT - what am I doing wrong?

Subject: RE: LOTUSSCRIPT - what am I doing wrong?

“I probably should have reread and clarified my writing…”

It was a little misleading. I just wanted to take the point, that the unnecessary If clause could be removed even without applying your improvement. I agree, that your suggested syntax is probably better overall, not because it has less characters, but because its more readable. But it still has a (small) catch: In any other programming language I know, the keyword While implies a pre-test loop, while Until tells you that this is a post-test loop, without having to think about it.

Creating unnecessary double negations is bad style for sure, but depending on the task at hand a variable like isFalse might just be what is logically expected, so I wouldn’t want to rule it out categorically. And after all, Notes developers are forced to think in hide-when logic anyway …

“However, it can certainly be proof of bad programming practice, which is exactly what putting Dims in the middle of your code is.”

The overall number of lines of code is obviously unaffected by where you put Dims. You don’t consider them to be part of the code logic, I do.

“I define all my properties right below my Dims. They’re all that the top and can all be copied out for easy reference.”

I never proposed to not group properties. But properties defined using property get/set will always differ in appearance from simple read-/writable member variables. Still, there might be perfectly valid reasons for using both styles. Functionality beats equal or consistent looks.

“No, it’s not a great help. There’s no advantage to it whatsoever. It does nothing but clutter the code and shouldn’t be done.”

Yes, it is a great help to me and my fellow workers. It doesn’t clutter the code, if it follows the flow of reasoning naturally, that’s why it should be done. You have no point here, other than that you’ve always done it that way. And I bet, that’s what you were taught, when you were a little Willy. Don’t tell me, this doesn’t influence your style of coding.

So, we have two contradicting statements and I am aware, that I have nothing more to proof my point, than you have, apart from personal preference. So next comes your 15+ years of experience in Notes (necessarily less in LS), which you will draw on. It’s only 9 years on my part, so obviously I must be wrong …

“I solved Tricia’s bug and offered suggestions on coding style and made one quip about Dims in the middle of code. You’re the one that seems to have taken the entire post as some kind of rant and have taken issue with it.”

Your help is definitely much appreciated. But don’t tell me that statements like

“I just don’t understand why people do this…”

or

“Dims in the code drive me crazy!”

don’t qualify for a perfect rant. I for my part prefer to still read your posts, but that won’t keep me from expressing my opinion, if I don’t agree. And again, it’s not so much about the details, but all about your bold statements of RIGHT and WRONG, that I cannot support.

Subject: RE: LOTUSSCRIPT - what am I doing wrong?

Subject: RE: LOTUSSCRIPT - what am I doing wrong?

“I am unaware of any current or historical use of UNTIL that would lead anyone to presume it’s a post-test.”

Well, maybe it’s just me then. It’s not worth arguing about it and your right on that the position of the test is all that counts, ultimately.

Looks like our opinions on what is or is not a rant differ as well. Only unjustified claims? Must be my lack of understanding of a foreign language. Well possible. The fact alone, that you did not command Tricia to put her Dims on top (how would you have called that?) only means, that you went for a more subtle way to say it. The message is still: Fools do that!

After all, I’m pretty sure that you would get along pretty well with code that I have written, even if it doesn’t follow your path. And vice-versa. Without affecting each others well-being too much.

Subject: RE: LOTUSSCRIPT - what am I doing wrong?

“The reason your code isn’t working is because MyUNID isn’t being reset with every new doc_PConfig.”

This was the key to my problem!!!

I’m taking your suggestions as well - thank you for your detailed response! I’m posting the code on another thread.

You ROCK! I can’t tell you how grateful I am!!!

Trish

Subject: LOTUSSCRIPT code fix - thank you all!

Oh my gosh, thank you all! I am using suggestions from all 3 respondents. I already changed the getitemvalue lines (etc.) and I’m moving my dims to the top - MyUNID is the reason the value is not changing - THANK YOU ALL! I will be able to put this back together as a reasonably clean piece of code. As for using underscores in fieldnames and variables (like db_Current) I have to use the naming conventions set up by my technology company.

Yes, my code is a mess, partially from days of beating it up trying this and that to get it to work. I really appreciate the tips to make my LotusScript code better, more organized and faster.

Sincerely,

Trish

Subject: RE: LOTUSSCRIPT code fix - thank you all!

“As for using underscores in fieldnames and variables (like db_Current) I have to use the naming conventions set up by my technology company.”

Hey, if there’s one thing I wanted to point out, it is, that there is no single gold standard for good and efficient programming. It’s just my personal dislike of superfluous characters, that don’t help readability (I think). But the fact alone, that they DO have standards at least is a good thing.

Heck, I don’t even mind that you went with Willy’s (wrong) suggestion to put all Dims on top! :wink:

Most important, you found a solution to your problem. And that wasn’t due to help from my part.

If you are willing to spend some time on an excellent read - actually the best I know off - on LotusScript, download Julian Robichaux’s amazing “Unfinished LotusScript Book”. Completely independent from discussions about coding conventions, I promise you an eye-opener, even for experienced developers.

http://www.nsftools.com/tools/lsbook.htm

Subject: RE: LOTUSSCRIPT code fix - thank you all!

Subject: RE: LOTUSSCRIPT code fix - thank you all!

While I would argue for the use of GetItemValue precisely for the reason that you stated earlier - readability. When I write

doc.GetItemValue(“Title”)

you can be sure that I am trying to get the value in the field called Title and not a property of the document that happens to be named Title. It is usually a strong hint to me, the developer, to add the (0) array subscript when you are trying to get at the single value stored in the field too.

Also, too often, we must write code like:

For i = 1 to 100

x = doc.GetItemValue(“Choice” & Format(i, “000”))(0)

Next i

which is simply not possible using extended class syntax. So rather than use two different syntaxes, I prefer to standardize on one.

Lastly, it used to be (not sure if it still is) that when doing:

doc.MyField = SomeValue

would result in having a field named

MYFIELD

in the properties box of the document, while

doc.ReplaceItemValue “MyField”, SomeValue

would respect the case of the field name that I tried to create.

For nitpickers like me, that was bothersome :slight_smile:

Subject: RE: LOTUSSCRIPT code fix - thank you all!

Actually, I was quite happy that Willy got me wrong on the GetItemValue part (which I prefer for exactly the same reasons you mentioned), so I could avoid more arguments on that. :slight_smile:

But now, that you stepped in: Yes, I like GetItemValue() and ReplaceItemValue(), and it’s not for the (marginal at best) performance benefit in the first place.

It would have been remarkable indeed, if Willy and I had agreed on one thing at least … :stuck_out_tongue_winking_eye:

Subject: RE: LOTUSSCRIPT code fix - thank you all!

Subject: RE: LOTUSSCRIPT code fix - thank you all!

That WAS misleading on my part, at least (if not wrong choice of words altogether). Glad you read and remembered it, in the first place.

Let me take this opportunity to clearly state, that I don’t take this debate personal in any way. I still think that you are too strict in proposing your standards. Yet I enjoy discussing with someone who does care about code quality from a much broader perspective than just “works” or “doesn’t work”. I find it rather fascinating, how developers can come up with completely contradictory opinions on what should be done and what should be avoided.

In part this might be caused by the very nature of LotusScript. With its Basic roots, its VB compatibility, the bolted-on OO capabilities and the specific implementation of the DOM, it often gives programmers a lot of freedom, maybe even too much.

It is surely boosted by IBM not actively promoting clear standards. Neither explicitly nor implicitly through consistent examples of the use of the language in their templates and samples in Designer help. Leaning towards VB conventions as found on MSDN is one possible rout to go. Personally, I rather try to adopt coding standards used in languages like Java.

Wrong to the core, you might say, LS !== Java (or you might prefer LS <> Java :slight_smile: ). Could be. I found it helpful when working with developers who were rather used to J2EE or advanced JavaScript.

But I can certainly rather live with scripts coded to different standards than mine, than with scripts lacking any structure or conventions.

Subject: LOTUSSCRIPT - what am I doing wrong?

Hello, Trish

AP_Name SHOULD be pulling in the actual value from the response document, but it’s getting the new value (if that has changed) preventing the update.

AP_Name = Cstr(doc_Pchange.PT_Name(0)) '<—This is returning the wrong value!!!

did you meant that this string return wrong value, right?

can you look in debugger on string where you take next response document? actually, are you sure that next response document has another value?

it’s to so hard to help you, because your code should to work right.

btw, instead of [val = doc.fldName ] use [val = doc.getitemvalue(“fldName”)]

and [ doc.fldName = val ] use [doc.replaceitemvalu(fldName, val)]

why?

  • it slowly then doc.GetItemValue(“fldName”)(0) near 15-20%;

  • if in new version of Lotus Notes will appear new method with name “fldName” the application will work wrong