AJF
AJF

Reputation: 1931

How do I extract the value of a UIInput in XPages

I have an Xpages application that manipulates Contracts. One procedure uses the Contract Type value in field "conService" to determine what must happen next. The code below does NOT produce any errors but third line does not seem to process any result and in fact does not even seem to process any line in the procedure after it. How do I extract the value of the conService? Thanks

UIInput uifield = (UIInput) JSFUtil.findComponent("conService");
String serviceName ="";
serviceName = uifield.getValue().toString();

Upvotes: 2

Views: 203

Answers (2)

Paul Stephen Withers
Paul Stephen Withers

Reputation: 15739

Where possible, it's worth going directly to the datasource you're storing the value in. It's more efficient and easier to manage.

If you do need the value during Process Validation phase, for a converter or validator, you can use component binding to easily access the relevant component, at which point you can use getSubmittedValue() - because the value will not have been set yet. Here's a NotesIn9 from Tim Tripcony covering it http://notesin9.com/index.php/2014/05/22/notesin9-143-component-vs-value-binding-in-xpages/.

Upvotes: 2

John Dalsgaard
John Dalsgaard

Reputation: 2807

You are almost there....

Once you have the UIInput object you can do either .getSubmittedValue() or .getValue() - depending on where in the JSF lifecycle you are. And then you just need to cast it to a String - instead of using toString().

So something like should do the trick:

UIInput uifield = (UIInput) JSFUtil.findComponent("conService");
String serviceName = (String)uifield.getValue();

To avoid having to thinkg about using getSubmittedValue or getValue I use a small utility method in my code:

ublic static Object getSubmittedValue(UIComponent c) {
    // value submitted from the browser
    Object o = null;
    if (null != c) {
        o = ((UIInput) c).getSubmittedValue();
        if (null == o) {
            // else not yet submitted
            o = ((UIInput) c).getValue();
        }
    }
    return o;
}

That just makes life a little less complicated ;-)

/John

Upvotes: 3

Related Questions