I have the following situation:A computed field at one point on an XPage gets several field values from another document using @DbLookup. Elsewhere on the XPage, I want to display the same values without repeating the @DbLookup calls.
Is there a server JS version of getElementById, so I can get the value directly from the first computed field?
Otherwise, what would be a good way to do this with minimal code?
Subject: Use a temporary variable
The best solution would use a temporary variable. For example, store the computed value in the requestScope, which remains valid for the time of the request, and reuse this value in different part of the page.The equivalent of getElementById() is getComponent(), but it won’t help in your case as calling getValue() on a computed field will recompute the formula all the time
Subject: getValue() recomputing?
“… calling getValue() on a computed field will recompute the formula all the time”
Wow - I did not realize that a component gets recomputed every time you try and access the value? I will have to test this out as I have been using that quite a bit. It sure would change my usage of the getComponent functionality.
Subject: True - with caveats
Yes - the computed field recomputes everytime you call getValue() on it. But there are at least 2 caveats to this.
-
Only if the field’s formula/value is selected as “Compute dynamically”. If you set the value to Compute on page load - it does not recompute.
-
If you have called setValue(), the original formula will not recompute. My assumption is the setValue() overwrites the formula - and any future references will access the new formula - which is just the value you just set it to.
Here is a simple test if someone wants to verify how it all works. It will print to the log when when the formulas are being run.
<xp:button value=“Run Test” id=“button1”>
<xp:eventHandler event="onclick" submit="true"
refreshMode="complete">
<xp:this.action><![CDATA[#{javascript:print("Dynamic: " + getComponent("dynamicField").getValue())
print("Dynamic: " + getComponent(“dynamicField”).getValue())
getComponent(“dynamicField”).setValue(1)
print("Dynamic: " + getComponent(“dynamicField”).getValue())
print("Dynamic: " + getComponent(“dynamicField”).getValue())
print("Load: " + getComponent(“loadField”).getValue())
print("Load: " + getComponent(“loadField”).getValue())
getComponent(“loadField”).setValue(1)
print("Load: " + getComponent(“loadField”).getValue())
print(“Load” + getComponent(“loadField”).getValue())
}]]></xp:this.action>
</xp:eventHandler>
</xp:button>
<xp:br></xp:br>
<xp:text escape="true" id="dynamicField">
<xp:this.value><![CDATA[#{javascript:print ("Dynamic Formula Run")
Math.random()}]]></xp:this.value>
</xp:text>
<xp:br></xp:br>
<xp:text escape="true" id="loadField">
<xp:this.value><![CDATA[#{javascript:print ("On Page Load Formula Run")
Math.random()}]]></xp:this.value>
</xp:text>