Hi!
After having dealt with the same problem and working with CMDBuild for a bit and looking at both the original Source Code and WebDevTools I’ve come up with this post to document the process.
You can use Javascript in the field interacting with the api
object you’re passed in. In the original source code you can find the FormHelper.js file that will show you the mechanics used to implement autovalue javascript execution. This sort of stuff means also that you will only be able to leverage this feature using the existing Frontend.
Sadly, albeit having written a blog post on the matter describing it in detail, I’m not able at the moment to post it.
It comes down to this:
In FormHelper.js the function:
/**
* Add auto value script to field
* @param {Object} config Ext.form.Field configuration
* @param {Object} fieldMeta Field metadata
* @param {Object} fieldMeta.validationRules Validation rules code
* @param {String} linkname
* @param {String} formmode One of `read`, `create` or `update`.
*/
addAutoValue: function (config, fieldMeta, linkname, formmode, activityLinkName) { ...
Here you will find:
var jsfn = Ext.String.format(
'function executeAutoValue(api) {{0}}',
script
);
The passed in api
object is defined like this:
var api = Ext.apply({
record: record,
activity: activity,
mode: formmode,
setValue: function (value) {
record.set(fieldMeta.name, value);
}
}, CMDBuildUI.util.api.Client.getApiForFieldAutoValue());
This means you can modify the currently opened record in the function by accessing the get
and set
functions on
the api.record
field to modify a given attributes or relations values.
So, for example adding a field NetAmount
based on amount
+ VAT
we can write it like so:
var VAT = api.record.get("VAT");
var amount = api.record.get("Amount");
if (VAT && amount) {
api.record.set("NetAmount", amount + amount * (VAT / 100));
}
And that’s it.
GIF of this mechanism working: