//Automatic ID function with table NumericalSeries
{
// get data where [Agenda == Contracts and Active == true]
let resultNumericalSeries = await doo.table.getData('numericalSeries',
{filter: [
{field: "NUMS_agenda", operator: "in", value: "Contracts"},
{field: "NUMS_active", operator: "eq", value: true}
], filterOperator: "and"}
);
// if some record exists
if (resultNumericalSeries.data.length > 0) {
// save value of field Prefix to variable
var nsPrefix = resultNumericalSeries.data[0].fields.NUMS_prefix;
// save value of field Number to variable
var nsNumber = resultNumericalSeries.data[0].fields.NUMS_number;
// save Number length (summ of characters) to variable
var Length = nsNumber.length;
var Prefix = nsPrefix
}
// else default values
else {
var Length = 2;
var Prefix= "undefined";
}
// get the one last record of table Contracts where ContractNo contains Prefix value
const resultContracts = await doo.table.getData('Contracts',
{filter: [{field: "CON_contractNo", operator: "contains", value: Prefix}], limit: 1}
);
// if some record exists
if (resultContracts.data.length > 0) {
// save value of field ContractNo to variable
var lastID = resultContracts.data[0].fields.CON_contractNo;
// save value of Prefix to new variable
var lastPrefix = lastID.substr(0, lastID.length -Length);
// save value of Number to new variable
var lastNumber =parseInt(lastID.substr(lastID.length -Length));
}
// if records doesn't exist but there is new active record in table Numerical series
else if (Prefix !="undefined") {
var lastPrefix = Prefix;
var lastNumber = 0;
}
// else start from 0
else {
var lastNumber = 0;
}
// adding 1 to last number
var nextNumber = lastNumber+1;
// convert Number to String
var paddingNumber = nextNumber + '';
// if length of number is less than Length value then add "0"
while (paddingNumber.length < Length) {
paddingNumber = "0" + paddingNumber;
}
// save new ID to variable
var newID = lastPrefix + paddingNumber;
// set value this value into field
doo.model.<[Contract No (CON_contractNo)]>.setValue(newID);
}
Hi, I want to do something similar. I tried to write something by quoting your code, but I couldn’t get results because I’m a newbie in this business.
The topic is here: https://faq.tabidoo.cloud/t/pulling-data-from-another-table-based-on-condition/303
I would be glad if you help.
Or if you share the template of your own application with me, I can use it by making changes on it.
Thanks.