Auto update records once you opened the App

Hi Everyone,

Cany anyone help with this, what I’m trying to do is to auto update all the records once you open the application. please see below code for reference.

Thank you.

(function() {
“use strict”;

kintone.events.on(‘app.record.index.show’, function (event) {

var records = event.records;

for (var i = 0; i < records.length; i++) {

var record = records[i];
var RECORDNO = ‘recordNo’;
var tempID = record.[RECORDNO].value; //To get ID in a field, just use a record number field and it is autofilled

var body = {

app: kintone.app.getID(),

id: tempID,

record: {“age”: { “value”: 19 }
};

kintone.api(’/k/v1/record’, ‘PUT’, body);

}
}
});

}());

Additional information:

Image below has a automatic calculation of age for the new records, and it will input on the Age column (highlighted in yellow), what I want is it will automatically update once the App is viewed and shows the record list of the App. Lets say my age today is 20 and then once I viewed it next year it will automatically become 21. I already have the Automatic Age calculation so its not a problem.

 

 

Please help me thank you.

Hello Muhaymin,

 

The record update process “PUT” is executed many times by a for statement.

There is a bulk update process “PUT” to update records.

If you want to update information in the event object,

it may be better to perform a bulk update after generating a request to update multiple records.

 

Also, after performing the record update process, you will need to describe the screen redrawing process.

If you describe the redrawing process in the record list screen, please be careful because

the process may fall into an infinite loop.

 

I’m a little unclear as to how much information you wish to have, but,

If you want to update the records displayed on the record list screen,

you can use the following process below.

 

(function() {

"use strict";

kintone.events.on('app.record.index.show', function (event) {

var records = event.records;

var wRecord = {};

var wRecords = [];

for (var i = 0; i < records.length; i++) {

var record = records[i];




wRecord = {

"id":records[i].$id.value,

"record":{

"age":{

"value":"19"

}

}

};

wRecords.push(wRecord);

}

var wParm = {

"app":kintone.app.getId(),

"records":wRecords

};




kintone.api(

'/k/v1/records',

'PUT',

wParm,

function(res){

console.log(res);

},

function(err){

console.log(err);

}

);

});

}());

 

It is not ideal to use the for statement to register records one by one.

It is desirable and better to do a bulk update.

 

Also, please be careful when redrawing the record list screen, to avoid an infinite loop

you will need a flag to determine that you have updated.

 

Hopefully, this helps.

Hi Sean,

Thank you so much for the help.

Hi Sir Sean,

I have a question, I have a code that will calculate the age but what I want is to put it on the bulk update that you gave to me. I tried it already but its not working. Here is my code. Your help is much appreciated. Thank you.

(function() {
“use strict”;
var OPTION = “date”; //field code of the table
var CALCULATE = “age”;

kintone.events.on(‘app.record.index.show’, function (event) {

var records = event.records;
var wRecord = {};
var wRecords = [];

// Auto calculate of Age
var options = eventobj.record[OPTION].value;
options = options.toString();

var dt = new Date();
var year = dt.getFullYear();
var today = new Date(options);
var bdd = today.getDate();
var bmm = today.getMonth()+1;
var byy = today.getYear();
var tdd = dt.getDate();
var tmm = dt.getMonth()+1;
var tyy = dt.getYear();
var birthDate = bmm +"-"+ bdd;
var ytd = tmm +"-"+ tdd;

if (ytd === birthDate) {

var Ages = parseInt(tyy) - parseInt(byy)+1;
}
else {

var Ages = parseInt(tyy) - parseInt(byy);

}

return event;

for (var i = 0; i < records.length; i++) {
var record = records[i];

wRecord = {

“id”:records[i].$id.value,
“record”:{
“age”:{
“value”: Ages
}
}
};
wRecords.push(wRecord);
}

var wParm = {
“app”:kintone.app.getId(),
“records”:wRecords
};

 

kintone.api(’/k/v1/records’,‘PUT’,wParm, function(res){
console.log(res);
},

function(err){
console.log(err);
}
);

});
}());

Hello Muhaymin,

After checking the code you have provided, we have found the following situations:

 

・ var options = eventobj.record[OPTION].value;

 

→ No definition for eventobj

 

・ return event;

 

→ Since the record is written before the record update process “PUT” is executed, the event is completed by the process.

 

The operation itself was possible with the following modifications.

 

・ var options = eventobj.record[OPTION].value;

 

→ Change the process to var options = records[0][OPTION].value;

 

・ return event;

 

→ Delete

 

However, since all of the “age” fields will have the same value, it may be necessary to change

the process of getting the value of the “date” field in the for statement.

 

You should also check to see if it is necessary to implement the process

of getting the value of the “date” field within the for statement.

 

Hopefully, this helps.