1

Is there a way to access in a more flexible way fields on a Java object via SSJS?

So when my Java object is stored in a obj variable I can access the created field via obj.created.

However I would make my solution a bit more flexible so the name of the field will be provided via a property definition on a custom control.

The name of the field I get via:

compositeData.fieldName

How can I bend this to:

var field = compositeData.fieldName;
obj.????

I tried

obj.getField(field);

(source: https://www.tutorialspoint.com/java/lang/class_getfield.htm)

gives me an error:

com.ibm.xsp.binding.javascript.JavaScriptValueBinding.getValue(JavaScriptValueBinding.java:132)

Is there someone with a suggestion?

Malin
  • 697
  • 5
  • 21

1 Answers1

6

You should be able to access object properties by bracket notation:

obj.fieldName

is the same as

obj[compositeData.fieldName]

You may also want to look at java reflection which may allow you to do:

obj.get(compositeData.fieldName)

Rob Mason
  • 1,385
  • 10
  • 23
  • Hello Rob, you make development look easy. It works like a charm. Thank you for helping me out! – Malin Oct 18 '17 at 08:20
  • No problem Malin. Happy to help. – Rob Mason Oct 18 '17 at 08:41
  • 3
    Just bear in mind Java reflection is prevented by the default Java policy. I needed a couple of changes for Watson Workspace Java SDK, see here https://openntf.org/main.nsf/project.xsp?r=project/Watson%20Work%20Services%20Java%20SDK/releases/54958F6AB453BEEF852581170033626B. Another option is making your Java class extend the Map interface, as shown by Tim Tripcony in his NotesIn9 https://www.youtube.com/watch?v=6xUEceG7wlM. (That code gets a lot easier with ODA, Tim had to add a lot of ODA code into his classes) – Paul Stephen Withers Oct 18 '17 at 09:18