XPages Tip: Getting the Value of the Current Field with SSJS (@ThisValue equivalent)

Sometimes, I miss the convenience of several @functions. If you’re writing SSJS code that needs to access the value of the current field from an event handler on that field, but you don’t want to hard-code the component name (so you can easily reuse it), it would be handy if there was an @ThisValue equivalent. Alas, there isn’t, but, fortunately, you can still easily access the value.

this

The special this keyword is where we need to start. It provides a handle to the current object.

When running code from an event handler, the object type is: com.ibm.xsp.component.xp.XspEventHandler

I find this by using the typeof operator in JavaScript

print (typeof this);

getParent()

The event handler doesn’t have a value — we need to get to the parent component that contains it. That’s where the getParent() method comes in handy. As you would expect, it goes up the chain and gives us a handle to the parent object, which for my sample input field is: com.ibm.xsp.component.xp.XspInputText

This object has a getValue() method to return the value of the component.

Getting the Current Field Value

Putting this together, I can easily get the value with this line:

var thisValue = this.getParent().getValue();

FYI — if you want the ID of the parent control from an event handler, you can use this line:

var thisID = this.getParent().getId();

Advertisements

3 responses to “XPages Tip: Getting the Value of the Current Field with SSJS (@ThisValue equivalent)”

  1. Sachin says :

    Nice one 🙂 !

  2. Eugene says :

    getId() returns value like “_id700”. But it’s not a FieldID. How to get a fieldID written in xpage source?

    • Brad Balassaitis says :

      Is this server side (possibly in a repeat control) or client side?

      If it’s server side, what type of component is it? (maybe some have more layers)

      If it’s client side (and your code is within an xp control on the page) you can use pass-through syntax like this: “{id:componentName}”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: