Avatar

Auto Numbering

Muhaymin Rakim

Hi Guys,

Can you help me with my problem in auto numbering, I have a record "WES 20-00024" but once the year change it will go back to 00001 can you help me with this problem thank you.

(function() {
'use strict';

function autoNum(event) {
var record = event.record;


var dt = record['Date_for_auto_num'].value;
var dtyy = dt.substring(0, 4);
var dtmin = dtyy + '-01-01';
var dtmax = (parseInt(dtyy, 10) + 1) + '-01-01';


var query = {
'app': kintone.app.getId(),
'query': 'Date_for_auto_num > "' + dtmin + '" and Date_for_auto_num < "' + dtmax + '" order by Auto_num desc limit 1'
};

return kintone.api(kintone.api.url('/k/v1/records', true), 'GET', query).then(function(resp) {
var records = resp.records;

if (records.length > 0) {

var rec = records[0];
var autono = rec['Auto_num'].value;
autono = autono.substring(4,12);
autono = parseInt(autono.substring(3), 10) + 1;
autono = '00000' + autono;
autono = 'WES '+ dt.substring(2, 4) + '-' + autono.substring(autono.length - 5);
event.record['Auto_num'].value = autono;


}

else {



event.record['Auto_num'].value = 'WES ' + dt.substring(2, 4) + '-00001';
}
return event;
}).catch(function(e) {
alert('Error occured getting record - error: ' + e.message);
return false;
});
}



kintone.events.on("app.record.create.show", "app.record.create.change.Date_for_auto_num","app.record.edit.change.Date_for_auto_num", autoNum);
kintone.events.on("app.record.create.submit", autoNum);


kintone.events.on('app.record.create.show', function(event) {
var record = event.record;
record['Auto_num'].disabled = true
record['Date_for_auto_num'].disabled = true;
return event;
});

kintone.events.on('app.record.edit.show', 'app.renord.index.edit.show', function(event) {
var record = event.record;

record['Auto_num'].disabled = true;
record['Date_for_auto_num'].disabled = true;
return event;
});


}());
Follow 3 followers
0

2 comments

Avatar
Sean Tachibana 1 vote

Hello Muhaymin,

By the following processing, it generates a criteria
specified query to see if the record with the year
appointed to the "Date_for_auto_num" field.

var dt = record['Date_for_auto_num'].value;
var dtyy = dt.substring(0, 4);
var dtmin = dtyy + '-01-01';
var dtmax = (parseInt(dtyy, 10) + 1) + '-01-01';

var query = {
'app': kintone.app.getId(),
'query': 'Date_for_auto_num > "' + dtmin + '" and Date_for_auto_num < "' + dtmax + '" order by Auto_num desc limit 1'
};


Also, when the record does not exist, it will go back to 00001.
Please appoint the query itself like the one below, otherwise no matter what the year is,
it will count up.

var query = {
'app': kintone.app.getId()
};

 

Hopefully, this helps.

Sean

1
Comment actions Permalink