« Imitation is not always flattery | Main| XPages in Pictures - 08 - Return to Company »

XPages in Pictures - 07 - Session Scope Variables

Category XPages
This is the 7th post of the “XPages in Pictures” Series.

Now it’s time to address the fact that saving the contacts returns us to “xMain” instead of the company page that we were on when we created it.

In the last article, you saw that when you created a response document, you passed the current companies noteid field into the simple action as the Parent ID.  If you could only store the noteid somewhere, you could then use it in the Save button of the contact page to return us to the correct company.  But how do you store a variable that spans different XPages?

Scoped Variables

It turns out there’s a way to store this information in something called “Scoped Variables”.  There are different types of scoped Variables.  I’m going to talk about 2 that are of interest right now.
1- viewScope                The variable lives for the duration of the page
2- sessionScope        The variable lives for the duration of the user session.

Since you want to access a variable between pages lets focus on sessionScope.  You’ll create a variable that can be accessed from anywhere.  Here’s how:

Open the page “xCompany”.  Go to the Events tab of the XPage and click on “afterPageLoad”.

A picture named M2

This event will run after the page loads and will let you get the documents’ “universal id” from the backend document object and store that in a sessionScope variable.  Once that happens it will be available from any page.

Rather then using a Simple Action, like you’ve done before, now you’re going to have to enter JavaScript that will run on the server.  Select “Script Editor in the radio button.  Now you can start to enter JavaScript in the box, or click on the “Open Script Editor” button, to bring up the editor.

A picture named M3

Enter the following code:

//Get the document object in JavaScript
var doc:NotesDocument = companyDoc.getDocument();

//Create a variable with the Universal ID
var sID = doc.getUniversalID();

// Assign the variable sID to a Session Scope Variable called "companyCode"
sessionScope.put("companyCode",sID);

Your screen should look like this:
A picture named M4

Now that you’re starting to enter JavaScript code, I should mention debugging.  At the time of this writing, Notes DDE version 8.5, debugging tools and techniques seems to be lacking.  My suggestion is to save often and test in the web browser often.  (This is another way of saying that I haven’t figured out a great debugging approach yet.)

Testing…

You now have a variable that will be available for any XPage.  Before putting this to a practical use, make sure you’re getting a good value.  You can test this by using an Edit Box Control that’s bound not to a field on a Notes Document, but to the sessionScoped Variable itself.

From your Core Controls List, drag a Label to the page.  Then drag and Edit Box next to it.

A picture named M5


The resulting page should look like this:
A picture named M6
Now click on the Label control and change the “Label” property to be “UNID:”  Then click on the Edit box control and go to the Data tab.  This is where you control the binding.

A picture named M7

In the beginning when you created the xCompany and xContact Page, you dragged data fields to the XPage and a nice table of field names and controls was created and the binding was automatically done for you.  Here is where you can control the binding of individual fields.  Maybe you don’t want a table, or you want to replace an Edit Box Control with a Multiline Edit Box.  You can choose between Simple, Javascript, and Advanced data binding.

In this instance, you want to bind to the scoped variable “companyCode”.  This variable is created and updated with the page xCompany loads. To do this, you want to bind data using the “Advanced” option.  Choose “Scoped Variable” next to “Use:” and select “Session Scope” and enter the variable name “companyCode”.

A picture named M8
Note: This screen shot looks incorrect as I review this post. The variable name should be "companyCode".  XPages is very case sensitive so be careful.  I'll try and update the screenshot soon.



Finally, go back to the Edit box properties and bake the control “Read Only”.  Save the page and launch the application in your browser.  Click on any company.  You should now see the companies’ UNID..
000000000000000000000000000000000000000000000000
A picture named M9

Click on different companies.  Notice how the UNID on the page will change.  This variable is getting updated every time you open a company!

Pretty Cool huh?

Comments

Gravatar Image1 - View scope is also pretty interesting when you deal with a page where you only run partial refreshes. You can keep track on in-page interactions.

Gravatar Image2 - Stephan,
Thanks for the comments! These scoped variables seem really powerful!

Declan - That's an awesome tip you posted about having a custom control to display the values of the variables!!!

Gravatar Image3 - Over on my blog I have just blogged about a method to help in debugging scoped variables

{ Link }

Gravatar Image4 - From my fumblings with sessionScope variables, you seem not to be able to store objects, only strings (haven't tried numbers or dates yet). I.e. trying to store a NotesDocument object in a sessionScope variable returns a docUnid when getting it.

Anyone else had any success with anything other than strings?

Gravatar Image5 - Jerry,

From my understanding you definitely can not store a NotesDocument Object in a scoped variable. It would be too big and impossible because of the stateless nature of the web. So you want the UNID in the scoped variable and you can use that to retrieve the Notes Document Object when you need it.

Gravatar Image6 - Scope variable appear to be handy.

Gravatar Image7 - great post, now I get it

Post A Comment

:-D:-o:-p:-x:-(:-):-\:angry::cool::cry::emb::grin::huh::laugh::lips::rolleyes:;-)

Powered By:

Domino BlogSphere
Version 3.0.2