Computed fields documentation "lacking", let's improve!

Hi,

yesterday I tried out computed fields on XClasses and found very little documentation, except about their mere existence. Maybe we can gather the information together, so I can update the documentation.

  1. Purpose: are these fields for display only, or can these be queried in xwql/hql queries?
  2. Is the current value “cached” in the database or are they calculated on the fly on page read or page write?
  3. Script: How does the scripting work. I had some success by embedding a velocity macro. Is this the correct way? Can all the other scripting languages also be used?
  4. Programming environment: I found out by accident that there seem to be some special variables available. So $name seems to point to the page name/title, $object to the current instance. Are there more? Any velocity “tools”?
  5. Best practices: I’d like collect an extensive list of best-practices, useful examples, etc. For example: Handling edit mode, working with none-string types, etc.

Thanks in advance.

The value is computed and not saved in the database so you can’t query the value with XWQL / HQL.

Nothing is stored in the database. The value is computed on the fly. We could probably cache the value but not in the database and you shouldn’t count on this.

It supports wiki syntax so you should be able to use any script macro http://extensions.xwiki.org/xwiki/bin/view/Extension/Script%20Macro .

See xwiki-platform/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/objects/classes/ComputedFieldClass.java at master · xwiki/xwiki-platform · GitHub . There’s no custom Velocity bindings or tools.

Normally the computed value should not depend on the action (view/edit). You can use the custom displayer to control how the value is displayed in edit/view. But since the value is computed it can’t be edited directly. Its value is updated by changing the value of the other fields that are used to compute the value.

Hope this helps,
Marius

I had some weird issues in edit mode yesterday on my brand new xwiki 10.x (not sure right now, will confirm later). Showing everything from parts of the macro to exceptions. Also I had really big issues with numerical values, because they would extracted as string. This may be due to my lack of expertise with xwiki and velocity, but that point would be easily solved with better documentation.

I will provide example code later. Currently I don’t have access to my wiki.

So as promised here is my “code”:

First I created a class Sandbox.TestCalcClass with these properties:

  • name (myname: String)
  • Number (number: Number)
  • FirstLetter (FirstLetter: Computed Field)
  • timesThree (timesThree: Computed Field)

The idea was that “FirstLetter” shows the first letter of “myname” and “timesThree” shows the number in “number” times three. Seemed to be easy but was actually hard(er than it should).

My code for FirstLetter:
{{velocity}}
#set($myname=$object.get(‘myname’))
$myname.substring(0,1)
{{/velocity}}

Code for timesThree:
{{velocity}}
#set($mynumber=$object.get(‘number’))
#set($Integer=0)
#set($mynum=$Integer.parseInt($mynumber))
#set($mynum=$mynum * 3)
$mynum
{{/velocity}}

I created an instance (TestCalcInst) with:
myname=MyName
number=2

Display is okayish (note the extra line/spacing after the calculated fields):

name
MyName
Number
2
FirstLetter
M

timesThree
6

But edit fails:

name
MyName
Number
2
FirstLetter
{

timesThree
Failed to execute the [velocity] macro. Cause: [For input string: “{{html clean=“false” wiki=“false”}}{{/html}}”]. Click on this message for details.

Full stack trace:

    org.xwiki.rendering.macro.MacroExecutionException: Failed to evaluate Velocity Macro for content [#set($mynumber=$object.get('number'))
#set($Integer=0)
#set($mynum=$Integer.parseInt($mynumber))
#set($mynum=$mynum * 3)
$mynum]
	at org.xwiki.rendering.internal.macro.velocity.VelocityMacro.evaluateString(VelocityMacro.java:139)
	at org.xwiki.rendering.internal.macro.velocity.VelocityMacro.evaluateString(VelocityMacro.java:52)
	at org.xwiki.rendering.macro.script.AbstractScriptMacro.evaluateBlock(AbstractScriptMacro.java:286)
	at org.xwiki.rendering.macro.script.AbstractScriptMacro.execute(AbstractScriptMacro.java:182)
	at org.xwiki.rendering.macro.script.AbstractScriptMacro.execute(AbstractScriptMacro.java:58)
	at org.xwiki.rendering.internal.transformation.macro.MacroTransformation.transform(MacroTransformation.java:297)
	at org.xwiki.rendering.internal.transformation.DefaultRenderingContext.transformInContext(DefaultRenderingContext.java:183)
	at org.xwiki.rendering.internal.transformation.DefaultTransformationManager.performTransformations(DefaultTransformationManager.java:101)
	at org.xwiki.display.internal.DocumentContentDisplayer.display(DocumentContentDisplayer.java:263)
	at org.xwiki.display.internal.DocumentContentDisplayer.display(DocumentContentDisplayer.java:133)
	at org.xwiki.display.internal.DocumentContentDisplayer.display(DocumentContentDisplayer.java:58)
	at org.xwiki.display.internal.DefaultDocumentDisplayer.display(DefaultDocumentDisplayer.java:96)
	at org.xwiki.display.internal.DefaultDocumentDisplayer.display(DefaultDocumentDisplayer.java:39)
	at org.xwiki.sheet.internal.SheetDocumentDisplayer.display(SheetDocumentDisplayer.java:123)
	at org.xwiki.sheet.internal.SheetDocumentDisplayer.display(SheetDocumentDisplayer.java:52)
	at org.xwiki.display.internal.ConfiguredDocumentDisplayer.display(ConfiguredDocumentDisplayer.java:68)
	at org.xwiki.display.internal.ConfiguredDocumentDisplayer.display(ConfiguredDocumentDisplayer.java:42)
	at com.xpn.xwiki.doc.XWikiDocument.display(XWikiDocument.java:1144)
	at com.xpn.xwiki.doc.XWikiDocument.getRenderedContent(XWikiDocument.java:1310)
	at com.xpn.xwiki.doc.XWikiDocument.getRenderedContent(XWikiDocument.java:1264)
	at com.xpn.xwiki.doc.XWikiDocument.getRenderedContent(XWikiDocument.java:1249)
	at com.xpn.xwiki.doc.XWikiDocument.getRenderedContent(XWikiDocument.java:1205)
	at com.xpn.xwiki.objects.classes.PropertyClass.lambda$renderContentInContext$0(PropertyClass.java:343)
	at com.xpn.xwiki.internal.security.authorization.DefaultAuthorExecutor.call(DefaultAuthorExecutor.java:85)
	at com.xpn.xwiki.objects.classes.PropertyClass.renderContentInContext(PropertyClass.java:343)
	at com.xpn.xwiki.objects.classes.ComputedFieldClass.displayView(ComputedFieldClass.java:122)
	at com.xpn.xwiki.objects.classes.ComputedFieldClass.displayEdit(ComputedFieldClass.java:136)
	at com.xpn.xwiki.doc.XWikiDocument.display(XWikiDocument.java:3248)
	at com.xpn.xwiki.doc.XWikiDocument.display(XWikiDocument.java:3178)
	at com.xpn.xwiki.doc.XWikiDocument.display(XWikiDocument.java:3127)
	at com.xpn.xwiki.doc.XWikiDocument.display(XWikiDocument.java:3104)
	at com.xpn.xwiki.doc.XWikiDocument.display(XWikiDocument.java:3077)
	at com.xpn.xwiki.api.Document.display(Document.java:1551)
	at sun.reflect.GeneratedMethodAccessor687.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.apache.velocity.util.introspection.UberspectImpl$VelMethodImpl.doInvoke(UberspectImpl.java:395)
	at org.apache.velocity.util.introspection.UberspectImpl$VelMethodImpl.invoke(UberspectImpl.java:384)
	at org.apache.velocity.runtime.parser.node.ASTMethod.execute(ASTMethod.java:173)
	at org.apache.velocity.runtime.parser.node.ASTReference.execute(ASTReference.java:280)
	at org.apache.velocity.runtime.parser.node.ASTReference.render(ASTReference.java:369)
	at org.apache.velocity.runtime.parser.node.ASTBlock.render(ASTBlock.java:72)
	at org.apache.velocity.runtime.directive.Foreach.render(Foreach.java:420)
	at org.apache.velocity.runtime.parser.node.ASTDirective.render(ASTDirective.java:207)
	at org.apache.velocity.runtime.parser.node.SimpleNode.render(SimpleNode.java:342)
	at org.xwiki.velocity.internal.DefaultVelocityEngine.evaluateInternal(DefaultVelocityEngine.java:259)
	at org.xwiki.velocity.internal.DefaultVelocityEngine.evaluate(DefaultVelocityEngine.java:222)
	at com.xpn.xwiki.render.DefaultVelocityManager.evaluate(DefaultVelocityManager.java:361)
	at org.xwiki.rendering.internal.macro.velocity.VelocityMacro.evaluateString(VelocityMacro.java:131)
	at org.xwiki.rendering.internal.macro.velocity.VelocityMacro.evaluateString(VelocityMacro.java:52)
	at org.xwiki.rendering.macro.script.AbstractScriptMacro.evaluateBlock(AbstractScriptMacro.java:286)
	at org.xwiki.rendering.macro.script.AbstractScriptMacro.execute(AbstractScriptMacro.java:182)
	at org.xwiki.rendering.macro.script.AbstractScriptMacro.execute(AbstractScriptMacro.java:58)
	at org.xwiki.rendering.internal.transformation.macro.MacroTransformation.transform(MacroTransformation.java:297)
	at org.xwiki.rendering.internal.transformation.DefaultRenderingContext.transformInContext(DefaultRenderingContext.java:183)
	at org.xwiki.rendering.internal.transformation.DefaultTransformationManager.performTransformations(DefaultTransformationManager.java:101)
	at org.xwiki.display.internal.DocumentContentDisplayer.display(DocumentContentDisplayer.java:263)
	at org.xwiki.display.internal.DocumentContentDisplayer.display(DocumentContentDisplayer.java:133)
	at org.xwiki.display.internal.DocumentContentDisplayer.display(DocumentContentDisplayer.java:58)
	at org.xwiki.display.internal.DefaultDocumentDisplayer.display(DefaultDocumentDisplayer.java:96)
	at org.xwiki.display.internal.DefaultDocumentDisplayer.display(DefaultDocumentDisplayer.java:39)
	at org.xwiki.sheet.internal.SheetDocumentDisplayer.display(SheetDocumentDisplayer.java:239)
	at org.xwiki.sheet.internal.SheetDocumentDisplayer.applySheet(SheetDocumentDisplayer.java:219)
	at org.xwiki.sheet.internal.SheetDocumentDisplayer.maybeDisplayWithSheet(SheetDocumentDisplayer.java:174)
	at org.xwiki.sheet.internal.SheetDocumentDisplayer.display(SheetDocumentDisplayer.java:111)
	at org.xwiki.sheet.internal.SheetDocumentDisplayer.display(SheetDocumentDisplayer.java:52)
	at org.xwiki.display.internal.ConfiguredDocumentDisplayer.display(ConfiguredDocumentDisplayer.java:68)
	at org.xwiki.display.internal.ConfiguredDocumentDisplayer.display(ConfiguredDocumentDisplayer.java:42)
	at com.xpn.xwiki.doc.XWikiDocument.display(XWikiDocument.java:1144)
	at com.xpn.xwiki.doc.XWikiDocument.getRenderedContent(XWikiDocument.java:1185)
	at com.xpn.xwiki.doc.XWikiDocument.getRenderedContent(XWikiDocument.java:1162)
	at com.xpn.xwiki.doc.XWikiDocument.getRenderedContent(XWikiDocument.java:1193)
	at com.xpn.xwiki.api.Document.getRenderedContent(Document.java:713)
	at sun.reflect.GeneratedMethodAccessor631.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.apache.velocity.util.introspection.UberspectImpl$VelMethodImpl.doInvoke(UberspectImpl.java:395)
	at org.apache.velocity.util.introspection.UberspectImpl$VelMethodImpl.invoke(UberspectImpl.java:384)
	at org.apache.velocity.runtime.parser.node.ASTMethod.execute(ASTMethod.java:173)
	at org.apache.velocity.runtime.parser.node.ASTReference.execute(ASTReference.java:280)
	at org.apache.velocity.runtime.parser.node.ASTReference.value(ASTReference.java:567)
	at org.apache.velocity.runtime.parser.node.ASTExpression.value(ASTExpression.java:71)
	at org.apache.velocity.runtime.parser.node.ASTSetDirective.render(ASTSetDirective.java:142)
	at org.apache.velocity.runtime.parser.node.SimpleNode.render(SimpleNode.java:342)
	at org.xwiki.velocity.internal.DefaultVelocityEngine.evaluateInternal(DefaultVelocityEngine.java:259)
	at org.xwiki.velocity.internal.DefaultVelocityEngine.evaluate(DefaultVelocityEngine.java:222)
	at com.xpn.xwiki.render.DefaultVelocityManager.evaluate(DefaultVelocityManager.java:361)
	at com.xpn.xwiki.internal.template.InternalTemplateManager.evaluateContent(InternalTemplateManager.java:820)
	at com.xpn.xwiki.internal.template.InternalTemplateManager.render(InternalTemplateManager.java:696)
	at com.xpn.xwiki.internal.template.InternalTemplateManager.lambda$renderFromSkin$0(InternalTemplateManager.java:671)
	at com.xpn.xwiki.internal.security.authorization.DefaultAuthorExecutor.call(DefaultAuthorExecutor.java:85)
	at com.xpn.xwiki.internal.template.InternalTemplateManager.renderFromSkin(InternalTemplateManager.java:670)
	at com.xpn.xwiki.internal.template.InternalTemplateManager.renderFromSkin(InternalTemplateManager.java:649)
	at com.xpn.xwiki.internal.template.InternalTemplateManager.render(InternalTemplateManager.java:635)
	at com.xpn.xwiki.internal.template.DefaultTemplateManager.render(DefaultTemplateManager.java:78)
	at com.xpn.xwiki.XWiki.evaluateTemplate(XWiki.java:2155)
	at com.xpn.xwiki.XWiki.parseTemplate(XWiki.java:2133)
	at com.xpn.xwiki.api.XWiki.parseTemplate(XWiki.java:969)
	at sun.reflect.GeneratedMethodAccessor258.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.apache.velocity.util.introspection.UberspectImpl$VelMethodImpl.doInvoke(UberspectImpl.java:395)
	at org.apache.velocity.util.introspection.UberspectImpl$VelMethodImpl.invoke(UberspectImpl.java:384)
	at org.apache.velocity.runtime.parser.node.ASTMethod.execute(ASTMethod.java:173)
	at org.apache.velocity.runtime.parser.node.ASTReference.execute(ASTReference.java:280)
	at org.apache.velocity.runtime.parser.node.ASTReference.render(ASTReference.java:369)
	at org.apache.velocity.runtime.parser.node.ASTBlock.render(ASTBlock.java:72)
	at org.apache.velocity.runtime.directive.VelocimacroProxy.render(VelocimacroProxy.java:216)
	at org.apache.velocity.runtime.directive.RuntimeMacro.render(RuntimeMacro.java:311)
	at org.apache.velocity.runtime.directive.RuntimeMacro.render(RuntimeMacro.java:230)
	at org.apache.velocity.runtime.parser.node.ASTDirective.render(ASTDirective.java:207)
	at org.apache.velocity.runtime.parser.node.ASTBlock.render(ASTBlock.java:72)
	at org.apache.velocity.runtime.parser.node.ASTIfStatement.render(ASTIfStatement.java:87)
	at org.apache.velocity.runtime.parser.node.ASTBlock.render(ASTBlock.java:72)
	at org.apache.velocity.runtime.parser.node.SimpleNode.render(SimpleNode.java:342)
	at org.apache.velocity.runtime.parser.node.ASTIfStatement.render(ASTIfStatement.java:106)
	at org.apache.velocity.runtime.parser.node.SimpleNode.render(SimpleNode.java:342)
	at org.xwiki.velocity.internal.DefaultVelocityEngine.evaluateInternal(DefaultVelocityEngine.java:259)
	at org.xwiki.velocity.internal.DefaultVelocityEngine.evaluate(DefaultVelocityEngine.java:222)
	at com.xpn.xwiki.render.DefaultVelocityManager.evaluate(DefaultVelocityManager.java:361)
	at com.xpn.xwiki.internal.template.InternalTemplateManager.evaluateContent(InternalTemplateManager.java:820)
	at com.xpn.xwiki.internal.template.InternalTemplateManager.render(InternalTemplateManager.java:696)
	at com.xpn.xwiki.internal.template.InternalTemplateManager.lambda$renderFromSkin$0(InternalTemplateManager.java:671)
	at com.xpn.xwiki.internal.security.authorization.DefaultAuthorExecutor.call(DefaultAuthorExecutor.java:85)
	at com.xpn.xwiki.internal.template.InternalTemplateManager.renderFromSkin(InternalTemplateManager.java:670)
	at com.xpn.xwiki.internal.template.InternalTemplateManager.renderFromSkin(InternalTemplateManager.java:649)
	at com.xpn.xwiki.internal.template.InternalTemplateManager.render(InternalTemplateManager.java:635)
	at com.xpn.xwiki.internal.template.DefaultTemplateManager.render(DefaultTemplateManager.java:78)
	at com.xpn.xwiki.XWiki.evaluateTemplate(XWiki.java:2155)
	at com.xpn.xwiki.web.Utils.parseTemplate(Utils.java:180)
	at com.xpn.xwiki.web.XWikiAction.execute(XWikiAction.java:462)
	at com.xpn.xwiki.web.XWikiAction.execute(XWikiAction.java:209)
	at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:425)
	at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:228)
	at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
	at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:449)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at com.xpn.xwiki.web.ActionFilter.doFilter(ActionFilter.java:112)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.xwiki.wysiwyg.filter.ConversionFilter.doFilter(ConversionFilter.java:127)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.xwiki.container.servlet.filters.internal.SetHTTPHeaderFilter.doFilter(SetHTTPHeaderFilter.java:63)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.xwiki.container.servlet.filters.internal.SavedRequestRestorerFilter.doFilter(SavedRequestRestorerFilter.java:208)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.xwiki.container.servlet.filters.internal.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:111)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.xwiki.resource.servlet.RoutingFilter.doFilter(RoutingFilter.java:134)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
	at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799)
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:861)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455)
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:748)
Caused by: org.xwiki.velocity.XWikiVelocityException: Failed to evaluate content with id [xwiki:Sandbox.TestCalcInst]
	at org.xwiki.velocity.internal.DefaultVelocityEngine.evaluate(DefaultVelocityEngine.java:227)
	at com.xpn.xwiki.render.DefaultVelocityManager.evaluate(DefaultVelocityManager.java:361)
	at org.xwiki.rendering.internal.macro.velocity.VelocityMacro.evaluateString(VelocityMacro.java:131)
	... 177 more
Caused by: org.apache.velocity.exception.MethodInvocationException: Invocation of method 'parseInt' in  class java.lang.Integer threw exception java.lang.NumberFormatException: For input string: "{{html clean="false" wiki="false"}}<input size='30' id='Sandbox.TestCalcClass_0_number' value='2' name='Sandbox.TestCalcClass_0_number' type='text'/>{{/html}}" at 36:xwiki:Sandbox.TestCalcInst[line 3, column 22]
	at org.apache.velocity.runtime.parser.node.ASTMethod.handleInvocationException(ASTMethod.java:243)
	at org.apache.velocity.runtime.parser.node.ASTMethod.execute(ASTMethod.java:187)
	at org.apache.velocity.runtime.parser.node.ASTReference.execute(ASTReference.java:280)
	at org.apache.velocity.runtime.parser.node.ASTReference.value(ASTReference.java:567)
	at org.apache.velocity.runtime.parser.node.ASTExpression.value(ASTExpression.java:71)
	at org.apache.velocity.runtime.parser.node.ASTSetDirective.render(ASTSetDirective.java:142)
	at org.apache.velocity.runtime.parser.node.SimpleNode.render(SimpleNode.java:342)
	at org.xwiki.velocity.internal.DefaultVelocityEngine.evaluateInternal(DefaultVelocityEngine.java:259)
	at org.xwiki.velocity.internal.DefaultVelocityEngine.evaluate(DefaultVelocityEngine.java:222)
	... 179 more
Caused by: java.lang.NumberFormatException: For input string: "{{html clean="false" wiki="false"}}<input size='30' id='Sandbox.TestCalcClass_0_number' value='2' name='Sandbox.TestCalcClass_0_number' type='text'/>{{/html}}"
	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
	at java.lang.Integer.parseInt(Integer.java:580)
	at java.lang.Integer.parseInt(Integer.java:615)
	at sun.reflect.GeneratedMethodAccessor823.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.apache.velocity.util.introspection.UberspectImpl$VelMethodImpl.doInvoke(UberspectImpl.java:395)
	at org.apache.velocity.util.introspection.UberspectImpl$VelMethodImpl.invoke(UberspectImpl.java:384)
	at org.apache.velocity.runtime.parser.node.ASTMethod.execute(ASTMethod.java:173)
	... 186 more

This displays the field depending on the current action. For edit mode it returns the HMTL needed to edit the field. You should use $object.getValue('number') instead (and same for myname).

Thanks @mflorea! This works now in edit and view mode. What I noticed is that calculated values are rendered in <p> tags while the other values are returned verbatim. This causes the odd spacing of the form. Any reason for this?

Is velocity “the right tool” for handling non-string datatypes or should I use groovy then? For example if you have a date property on an object how would you compute the diff in days to today?

As I said I would like to write some documentation about all this. My idea was to add a link from the Calculated Pseudofield on this page to a new page, if you don’t mind.

Probably because the content of the script meta property is rendered as HTML… which is not nice.

Depends on the complexity of the operation. For dates we have http://extensions.xwiki.org/xwiki/bin/view/Extension/Joda%20Time%20Plugin and even $datetool http://extensions.xwiki.org/xwiki/bin/view/Extension/Velocity+Module#HVelocityTools .

Yes, DevGuide.DataModel seems the right page.

So I added a first version of the documentation trying to sum up this thread and my findings.
I’d like to have an example working with datetime in velocity, so if you have one handy, please share.

Hope this helps.

Hello,

This one? https://github.com/xwiki/xwiki-platform/blob/master/xwiki-platform-core/xwiki-platform-web/src/main/webapp/templates/displayer_date.vm#L1#L2

By the way, to check if velocity variable is Null (or empty): CheckingForNull - VELOCITY - Apache Software Foundation

Usualy to working with string on velocity, I use this doc:

Pascal B

Looks good. Thanks for this contribution!

I’d like to revive this topic.

I have created a computed field for an AWM which calculates the average $avg of a number of entries. The following is the velocity script I used:

{{velocity}}
#set($qry1 = "from doc.object(AWMApp.Code.AWMAppClass) as pc where doc.fullName <> 'AWMApp.Code.AWMAppTemplate' and doc.object(AWMApp.Code.AWMAppClass).shortText7 like '$doc.databaseList3'")
#set($count=0)
#set($sum = 0)
#set($results = $services.query.xwql($qry1).execute())
#foreach ($item in $results)
 #set($pc = $xwiki.getDocument($item))
 #set($sum = $sum + $pc.getValue("number12"))
 #set($count = $count+1)
#end
#set($avg = $sum/$count)
$avg
{{/velocity}}

The calculation runs correctly however, they are enclosed by

$avg

.

I have tried:

${$avg.replace("<p>", "").replace("</p>", "")}

to no avail.

I’d be grateful for any advice on this matter.