XPage development question - Trying to set sessionScope with value from page

I wonder if someone could tell me how to set a sessionScope parameter using client-side javascript (or perhaps propose an easier way to accomplish what I am trying to achieve)?

I have a sessionScope parameter called “customerName” (sessionScope.customerName).

I have calculated the appearance and value of a number of design elements based on this parameter.

I also have a Combo Box control on the page that pulls the valid customer names from the database and allows me to select from the list.

What I would like to do in this case is add logic behind the onChange event of the Combo Box to set the sessionScope.customerName value to the name selected in the Combo Box (nothing to do with setting field values on an underlying document).

I followed John Mackey’s advice (http://www.jmackey.net/groupwareinc/johnblog/johnblog.nsf/d6plinks/GROC-7GLFZG) and tried to execute the server-side script from the client-side, with limited results.

I am having two issues in particular that I am hoping someone can help me with…

I am using script such as:

var newName = document.getElementById(“selectCustomer”)

However, although I have given the Combo Box an ID of “selectCustomer”, I can never initialize a handle to that element.

To test, I instead used the code:

var newName = “Test Customer”;

var test = “#{javascript:sessionScope.customerName='”+newName+“'}”;

That presents my second issue.

Instead of the sessionScope parameter assuming the new value (“New Customer”), it instead assumes the value: “+newName+” (inc the quotes).

I know I am royally screwing up something here, and have probably taken the wrong route to accomplish what should be an easy task.

Can anyone give me some advice to help me along?

Thanks in advance for any such assistance!

T.

Subject: Re: XPage development question - Trying to set sessionScope with value from page…

I can answer the first part of your question. If you look at the source, the id you’ve allocated to the comboBox gets prefixed with additional code to indicate which custom control it’s part of (which is why you can have a repeat control including an element with a specific id). If you use “#{id:selectCustomer}”, that will get the full id of the combobox. I think you need to assign this to a variable rather than just include it in the getElementByID.

I don’t know if you can set a scoped variable in client-side script. If you look at the source HTML of the page, you’ll see the server-sde code gets calculated before it gets to the browser, so the “#{id:selectCustomer}” will be converted to the full id on the source. I think that’s why your sessionScope variable is set to “+newname”, because it’s getting set before the client-side script is being triggered. Is there a reason you’re not setting the sessionScope variable in server-side code on the onClick event, with a partial refresh to ensure the whole page doesn’t reload? Bear in mind though, if you’ve got a debug panel to show your scoped variables, if you’re not refreshing that panel it will look like the scoped variable hasn’t been updated, but it will update the scoped variable. For anyone reading this who is still on 8.5, I was not able to set scoped variables via SSJS with partial refresh, though that works fine on 8.5.1.

Subject: I hadn’t tried using SSJS…

… because I didn’t think I would be able to extract the value of the Combo Box. I suppose my head is still stuck in the old R6/7 days, where CSJS was where most of my logic was processed.

Thanks for your tip on #1 - I had seen that before and totally forgotten that method of retrieving the complete ID.

You are certainly correct - I would certainly like to use SSJS to set the session scoped variables. I’m obviously a novice who doesn’t know how to obtain the value of the selected Combo Box option though.

Typically, I would use something like…

var element = document.getElementById( “selectCustomer” );

var newName = element.options[element.selectedIndex].text;

I appreciate your assistance sofar Paul, but could you enlighten me as to how I inject this logic into SSJS code?

Thanks again for your responses - You’re really helping me out! I just wish there were more hours in the day to learn this!!

Subject: Re: I hadn’t tried using SSJS…

getComponent() will get a handle on a control in the current custom control (or XPage if you’re working directly on the XPage). It takes the id parameter. getValue() can be used to access the value.

So for your comboBox, you can use getComponent(“selectCustomer”).getValue().

Because your combobox is bound to an XSPDocument data source, you can also get the value from there, e.g. document1.getItemValueString(“fieldName”) - document1 would be the variable name allocated to your data source.

It’s been mentioned a few times here, but Declan Lynch’s series on XPages is excellent, and there are other resources available (see XPages blog) - just google them. Also I should include a shameless plug for some XPages stuff on my own blog - http://hermes.intec.co.uk/intec/blog.nsf

Subject: Thanks again Paul!

Thanks Paul - I knew there was a much more simple method of addressing the challenge!

Yes, I am almost up to “Part 40” of Declan’s series, and am finding it extremely helpful.

Like most things, however, the “follow the bouncing ball” stuff doesn’t sink into my brain until I am trying to use it in a practical application of my own.

In this case I am developing an internal application, where I can hopefully put what I have been learning into practice (thanks to your assistance, of course!).

It’s when attacking a project like this, I find myself in a predicament where I know exactly what I need to achieve, and I know that it can probably be accomplished fairly easily, but I can’t remember which blog/tutorial/wiki contained an example of how to do it. I then find myself trying several “imaginative” ways to do it, and then becoming frustrated enough to post a question here. After going through that once though, you can rest assured that I won’t forget (and I’ll always have my own code examples to refer back to)!!

I’ll be sure to bookmark your blog, and use that as a source of development tips in the future. Thanks very much again!

T.