Question
· Jan 8, 2021

Zen Controls: How to read value of a control in a zen method

I have a text control : <text id="clinic" label="Type Clinic Code ">. I want to retrieve the value of this control in my zen method. But I keep getting errors.

ClassMethod readValues(bData As %ZEN.proxyObject, act As %String) As %Boolean [ ZenMethod ]

{

  zenPage.getComponentbyId('clinic').getValue() // this doesnt work

%clinic.%GetValue(); // Tried this as well but getting errors

}

Can I get some advice on this? Apologies if this is a simple question. Just learning and developing using zen pages

Discussion (3)1
Log in or sign up to continue

in your ClassMethod you operate on Server side.

the equivalent to zenPage on Client side is  %page on Server side.
And methods on Server side all start with %.. and strings use double quotes.
It is ISOS/COS not JS !

try:

set ref=%page.%GetComponentById("clinic")

https://docs.intersystems.com/latest/csp/documatic/%25CSP.Documatic.cls?&LIBRARY=%25SYS&CLASSNAME=%25ZEN.Component.abstractPage#%25GetComponentById

A ZenMethod is written in ObjectScript, not JavaScript, so that's why getValue() doesn't work. You can only access page components in a ClassMethod if you pass them in as arguments; otherwise, you need to make the method an instance method (that is, just Method readValues). Then you can use:

Set clinic = ..%GetComponentById('clinic').value

If you do need to do things in JavaScript in a ZenMethod, you can do them in an &js block - e.g.:

&js<alert(#(..QuoteJS(clinic))#);>

Which, in combination with the line above, would safely quote the string "clinic" for use in JavaScript (e.g., escaping quotes within the string), and this JavaScript will run on the clientafter the method returns. (That is, it isn't immediate; if you had a "hang 5" command after the &js block you wouldn't see an alert on the client until after the method ends.)

Instance methods in Zen are expensive because the whole page needs to be serialized and sent to the server (so that you can access and potentially modify all the components on the page). However, your method signature suggests that it's expecting a Zen proxyObject. You could build a Zen proxyObject with all of the form field values you care about in a JavaScript method on the client, and send that to the server by passing it to your ClassMethod, and that would be more efficient (if all you want to do is retrieve data from the form).

Also, @ED Coder , it's perhaps worth stepping back and asking the context of your work. If you're looking to build a brand new application of any real complexity, Zen probably isn't the best approach these days. I really enjoy Zen and have spent many years working with it, but starting on a new project I'd probably look toward a popular client-side framework like Angular or React and using REST APIs to interact with the database (probably using https://openexchange.intersystems.com/package/apps-rest to match the rapid development features of Zen in a REST API context).

If you're jumping into a large, existing Zen application and trying to make sense of the technology, that's a lot more understandable. Just curious. smiley