Skip to main content

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);

}

}

}

}

 

 

 

 

CONSOLE.LOG

  1. {fields: Object}
    1. fields: Object
      1.  Valor: 100
      2.  Parcela: "2/3"
      3.  Vencimento: "04/06/2023"

CONSOLE.LOG

  1. {fields: Object}
    1. fields: Object
      1.  Valor: 100
      2.  Parcela: "3/3"
      3.  Vencimento: "04/07/2023"

ERROR

Error: Field "fld3VB0dy42qjaliB" cannot accept the provided value.
    at main on line 50

 

 

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?


Date fields need to be set as ISO 8601 format, not as a locale string. Use .toISOString() instead of .toLocaleDateString('en-GB')

 

 


Reply