In honor of Bill Ernest (see the post below), beginning today and for the next week I'm going to post each day one of the truly cool things you can do with Notes (and/or Domino) based on things Bill figured out. So, here goes:
#1: Building dynamic JavaScript in a WebQuerySave agent.
If you build
web applications involving any sophistication, pretty soon you get to the point where simply submitting a document and diverting to a URL specified in the $$Return field is not enough. For instance, what you probably want to do is return to the view you came from when you first opened the document, just like you would if you were using the Notes client.
The problem is, how do you do that?
At first, it seems like an easy question. You know the name of the view, just put it in the $$Return and be done with it. But that doesn't really work, for a couple of reasons. First, $$Return can take you back to a view but it might not be the right view. If it is the right view (whether by application of your fine development skills or just plain old good luck), chances are it's not in the right part of the view.
The issue, in most cases, is that you usually have to navigate around in a view for a while to get to the document you care about. You may go down a few pages then expand a couple of categories to get to it. At that point, you're at a very specific URL. While you may have started at a URL like this:
http://YourServer/YourDb.nsf/AllDocsByType?OpenView
Where you ended up is a place with a URL more like this:
http://YourServer/YourDb.nsf/AllDocsByType?OpenView&Start=148&Count=50&Expand=180.1#180.1
Writing a little formula in the $$Return field to simply come back to the view will bring you back to the top of the view, not the place you worked so hard to get to. If you're doing a bit of work on several related documents located some number of pages down into your site, this can be incredibly frustrating.
So, you have to ask yourself at some point, what can I do to make that better?
Well, I'm glad you asked. Really. I am.
$$Return, while handy enough in low-use or very simple applications is a weak substitute for a real solution ("I pity the fool...."). What you need here is real code you can control in a useful way. What you need is some JavaScript in a WebQuerySave agent.
"JavaScript?," you say. "I can't use JavaScript in a WebQuerySave agent," you say. "Notes agents only let you write code in @Formulas, LotusScript and Java. You can't do JavaScript in a Notes agent," you say.
"Hahahahahaha," I say. "You didn't know Bill! He didn't always follow the rules. He found ways around them. You can write all the JavaScript you want in a WebQuerySave agent." And then, I show you:
LotusScript Print statements are the key
In a LotusScript agent that runs in the UI, you probably already know that if you use Print statements...
Print "Updating document #" + Cstr(i)
...the string you provide will be written to the status bar at the bottom of Notes client window. It's a handy way of, for instance, letting the user know his computer hasn't died a quiet death while running a long process.
That same Print statement, used in a Notes background agent, writes information to the Notes Log. This can be used both to totally piss off your Notes administrators and to give yourself or others important information about the status of an agent or related process.
You probably knew both those things. What you may not have known is that the lowly Print statement, when used in a WebQuerySave agent, writes whatever string(s) you provide directly to the browser, with no filtering, as Pass-Thru HTML.
Oooooooo.
And, if you write the code inside <script> tags, you can write JavaScript straight to the browser.
Oooooooo, too.
So, to be able to get back to exactly the place you came from in a view, how would you do it? The first problem is knowing where you were. One approach, which we've used in ProcessIt! is to pass the view location through into the document in the Query_String, parse it out, manipulate as necessary, and use it as we see fit. That works quite well, but it's a bit fussy to get right and requires all the views to play along. Not for the timid.
A simpler approach is to store the data in a separate frame. If you're using framesets, this is easy. If you're not, you could consider adding a hidden frame to your page for this purpose. The idea is this: create a JavaScript variable, let's call it currentViewLocation, on the page in the extra frame. It might be the page you have a navigator on, for instance.
Then, on your $$ViewTemplate form (oh, please tell me you're not just showing people views the way Notes serves them up!), add a quick JavaScript function to the onLoad event to write the page's current location to the frame's JavaScript variable (you can get it from the CGI variable "Path_Info_Decoded" by placing a field of that name on your form):
if (window.parent.parent.left){
window.parent.parent.left.currentViewLocation = document.forms[0].Path_Info_Decoded.value;
}
Every time you change the view URL by moving to the next page or expanding or collapsing the twisties, the $$ViewTemplate will reload, run the onLoad event, and update the currentViewLocation value in the outside frame. Now you just need to use it.
In your WebQuerySave agent, do whatever else you need to do, but at the end, add this:
Print |<script type="text/javascript">|
Print | window.location = window.parent.parent.left.currentViewLocation;|
Print |</script>|
That's all there is to it! Of course, once you've figured out you can write a little JavaScript to the browser like this, pretty soon you start writing a LOT of it and doing much more sophisticated things. But, I'll leave that to you to figure out.
See you tomorrow.
1. Richard Schwartz05/04/2005 11:58:11 AM
Homepage: http://smokey.rhs.com/web/blog/PowerOfTheSchwartz.nsf
Wonderful idea, and great tip, too, Scott. I think this is a fitting tribute to a great guy who has helped so many people over the years. Over in my blog I'm also suggesting http://smokey.rhs.com/web/blog/PowerOfTheSchwartz.nsf/d6plinks/RSCZ-6C3LBG that other people add to this tribute by posting their own tips this week in honor of Bill.
-rich
2. Ashish Sidapara05/04/2005 09:41:02 PM
Homepage: http://lono.blogspirit.com
Appreciate your idea Scott !
The Notes community owes him alot and this is one of the best ways to give it back.
3. Carolyn Kraut05/16/2005 10:17:51 AM
Homepage: http://panther.sharedknowledgesystems.com:81/sksblog.nsf
This is an interesting approach that I will have to try. Up until now I have been using a cookie/$$Return formula combination. I've been lucky in that my customers have been okay about allowing cookies for my applications but I'm sure there will come a time when objections will be raised.
4. Hannes11/19/2007 07:12:47 AM
Hi guys, this is great but I have a question and maybe you help me out! I have been stuck on this for 2 weeks now and I just can't think of any way to do this, I have an application that draws forms based on the users requirements. This is a HTML page with fields, dropdowns, radio buttons etc.
Basically a generic questionare whith the whole page build up using Lotus Script Print statements. This all works great but I would like to generate a document from this form with field values same as the values captured on the form.
I can create the whole form with input fields and submit buttons with Javascript field validations, but I cant create a document with the values as inserted on the form by another user. The problem is that the print statements doesn't generate a new document.
Any Idaes I am really stuck.
Much appreciated!
5. Newbs11/19/2007 09:39:07 AM
Homepage: http://www.henrynewberry.com
@Hannes:
What you need to do is submit the form to another agent that has the smarts to extract the data and create the document. In the <Form> tag you add a parameter called action which you then point at your agent. Include in the agent url parameters that tells you what form you are using so you can easily validate the items that are being sent.
The agent will need to parse out the items from the CGI Variable Query_String_Decoded and REQUEST_CONTENT.
There are examples in this session from Advisor Live 2005. http://www.henrynewberry.com/newbs/website2.nsf/DocsByKey/HNEY6EGSHA?opendocument
Let em know if this helps.
Newbs
6. Scott Good11/19/2007 09:46:25 AM
Homepage: http://www.scottgood.com
@Hannes,
In case you haven't figured it out already, Henry Newberry is the new Bill Ernest (at least as far as we're concerned).
Scott

























