CMDBuild Forum

Calculate total number of X

Looking at the building and floor card you’ll see totals for floors and rooms. I was wondering how this was done to recreate something similar in a custom class I created. Looking at the attributes in these classes give me no information as to how this was achieved.

Can anyone help?

what do you mean with “totals for floors”? It might be inheritance what you are thinking about, but I would love if you provided screenshots or more in-depth description

Backend Attribute
image
Front end Card
image
I created some custom classes and wanted to include totals like this for those with relationships.

I am not sure you can do that in the product. If autovalue does not work for you (in attribute settings, there is autovalue option, which might take some sql), you might need to do that manually or implement a small process to update it.
Or there is another option that @tecnoteca only knows :smiley:

Really wish the manuals had more information. The only thing I could find in the various manuals is that Autovalue is a string. (Woo Hoo.) and uoi put JavaScript code in there.

Do you have any idea how that works?

HI is there someone that knows how AutoValue works maybe with some little example?

Thx in advance

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:
cmdbuild-add-card-attributes-with-autovalue

1 Like