May 04, 2023 11:48 AM
I made the code below to read a row in my Teste3 table, and from that row, generate new rows according to the code. Row generation is correct, but the script is unable to record new rows in the table. Can you help me?
let table = base.getTable('Teste3');
let queryResult = await table.selectRecordsAsync({
fields: ['Valor', 'Vencimento', 'periodo_real', 'parcela_real'],
});
let records = queryResult.records;
for (let record of records) {
let valor = record.getCellValue('Valor');
let vencimento = record.getCellValue('Vencimento');
let periodo = record.getCellValue('periodo_real');
let parcela = record.getCellValue('parcela_real');
if (valor && vencimento && periodo && parcela) {
let [currentParcel, totalParcels] = parcela.split('/').map(Number);
if (currentParcel < totalParcels) {
let newParcels = [];
for (let i = currentParcel + 1; i <= totalParcels; i++) {
let newParcel = {
'fields': {
...record.fields,
'Valor': valor,
'Parcela': `${i}/${totalParcels}`,
}
};
delete newParcel.fields['Vencimento'];
if (periodo === 'Semanal') {
let currentDate = new Date(vencimento);
currentDate.setDate(currentDate.getDate() + 7 * (i - currentParcel));
newParcel.fields['Vencimento'] = currentDate.toLocaleDateString('en-GB');
} else if (periodo === 'Mensal') {
let currentDate = new Date(vencimento);
currentDate.setMonth(currentDate.getMonth() + (i - currentParcel));
newParcel.fields['Vencimento'] = currentDate.toLocaleDateString('en-GB');
} else if (periodo === 'Quinzenal') {
let currentDate = new Date(vencimento);
currentDate.setDate(currentDate.getDate() + 15 * (i - currentParcel));
newParcel.fields['Vencimento'] = currentDate.toLocaleDateString('en-GB');
}
newParcels.push(newParcel);
console.log(newParcel)
}
for (let newParcel of newParcels) {
await table.createRecordAsync(newParcel.fields);
}
}
}
}
at main on line 50
Solved! Go to Solution.
May 04, 2023 09:53 PM
This usually means the format of the data you're inputting into the field is incompatible. Could you double check that you're inputting the correct data format for the type of field that you're updating?
May 05, 2023 08:21 PM
Date fields need to be set as ISO 8601 format, not as a locale string. Use .toISOString() instead of .toLocaleDateString('en-GB')
May 04, 2023 09:53 PM
This usually means the format of the data you're inputting into the field is incompatible. Could you double check that you're inputting the correct data format for the type of field that you're updating?
May 05, 2023 08:21 PM
Date fields need to be set as ISO 8601 format, not as a locale string. Use .toISOString() instead of .toLocaleDateString('en-GB')