CMDBuild Forum

Auto Value javascript code sample

Can you provide a sample JavaScript code for setting auto value ?

Thank you

3 Likes

Yes I would like to know the answer to this question as well.

It depends on what you want as an automatic value
but an example would be:

// In addition to being called at the beginning, it is called with the attribute with the name “mr”
api.bind = [“mr”];

// saves the content of mr in a variable, it can be between 0 and 100
let mr= api.getValue(“mr”);

// checks if mr is greater than 0 and less than or equal to 9
if (mr >0 && mr <=9){
// if it is correct, it assigns itself the value 6140907
api.setValue(6140907);
}

The most important thing is api.setValue that allows you to change the value of the attribute

Something simpler, if the date attribute is empty, the current date is automatically assigned:

if (Ext.isEmpty(api.getValue(“date”))){
api.setValue(new Date());
}

PD: sorry for my english, i’m using google translate

Thank you Earcumano for your response,

I was looking for the date version of your example and it is a lot shorter than the one I’ve build so thank you for that.

I’m also looking for a way to increment the value of a field. And yes, I have discovered that there is a way. But if I understand correctly, that requires me to change the field property of the attribute in the database.

But I’m a functional administrator in large hospital and for changes like that I have to ask the technical administrator to do this, which is very inconvenient and will consume a lot of time because we have to go through a complete change process for this. It would be very useful to be able to do this myself in the interface.

So isn’t there a way to do this with javascript from the interface? I could think that if I could read the value of my ID attribute from the previous record and add a 1 to it, I could get pretty close to a solution.

Kind regards,

Frans

Could you share what you discovered? to test in my environment and see if it can be done using JavaScript

I’ve found this post: With title “How to get a sequence number as part of the code attribute as auto value”

Where I found code sniplet:

var code = api.getValue(“Code”);
if (code == null) {
var dt = new Date();
api.setValue(‘CAB-’ + Ext.Date.format(dt, ‘U’));
}

The first line make me think I can retreive the highest code value. But I cant make work.
I’ve tried:

var code = api.getValue(“Code”);
api.setValue(code);

And tried

api.bind=["Code"];
var value=api.record.get("Code")
api.setValue(value+1);

AND

api.bind=["Code"];
var value=api.record.get("Code");
var x = Number(value);
api.setValue(x++);

AND

api.bind=["Code"];
var value=api.getValue("Code");
var x = Number(value);
api.setValue(x++);

The only one that does something is :

api.bind=["Code"];
var value=api.record.get("Code");
var x = Number(value);
api.setValue(x+1)

But is not consistant.

I hope you can help

Kind regards,

Frans

Wanted to get a PO number that was unique when openg a new PO. So go to management part and get to PO Code Attributes area. Go to the attributes area. Paste the beliw in the Auto value area. Save and refresh the page and create a new PO

var code = api.getValue(“Code”);
if (code == null) {
var dt = new Date();
api.setValue(‘PO-’ + Ext.Date.format(dt, ‘Y-m-d-h-i-s-u’));
}

You will get a millisecond unique PO number in the Code area. Look something like this.
PO-2023-07-06-01-14-23-743. You shouldn’t have POs being created at the same millisecond, so it should be unique.