Search Results for 'form.getRecords'
-
Search Results
-
Topic: Previous record skip
I can’t figure out how to fix the code so the first actual record after the “Begin Year Mark” record adds the $100 to $0 of the initialization record for the year.Thanks…Date Org $ Total$01/01/24 Begin. 0 0 Begin Year mark01/01/24. KVMR 100 003/12/24 test 20 2003/27/24. test.2. 50 7004/17/24. test.3. 55 125Can’t remember how to display the code correctly – searched for it – Put that hint somewhere? “!”???!function Year_to_date() {var date_id = ‘fld-e2d20da877cb4a6c8fd72153b86f1ab1’;var donation_id = ‘fld-05c429d6a4484061b554aa584a45f8fc’;var total_id = ‘fld-7c6b9bb288ab47e0b295af6eacc9cd26’;var records = form.getRecords(); // calls function getRecords()var currentRecordIndex = records.indexOf(record);if (currentRecordIndex > 0) {var previousRecord = records[currentRecordIndex-1];} else {return 0;}// Is this the beginning of the year?var date = record.getFieldValue(date_id);if (date.getMonth() == 0 && date.getDate() == 1){return 0;}var today = record.getFieldValue(donation_id);var previous_total = previousRecord.getFieldValue(total_id);var total = today + previous_total;console.log(“Today: ” + today)console.log(“Previous: ” + previous_total)console.log(“Total: ” + total)return total;}Year_to_date();!- Hi everybody,
I have an income and expense form and I want to automate it as much as possible. To do this, and with the invaluable help of my friend ChatGPT, I have made the script that I am sending you. What I want is for the last pending payments or collections to be credited when the script is executed, if the current date is later than the date of said payments or collections. The payment records are in a table-type field called “Calculations”, which includes the fields: – “Payment or collection date”, date type field, is the expected date for payment or collection. – “Amounts and accumulated”, numerical field, is the amount to be paid or collected. – “Payments and accruals”, numerical field, is filled in with the value of the Amounts field when the Payment Date arrives. What the script intends is that when the Payment or Collection Date is before the current date, the script will be executed and the following actions will occur: In the line of the last pending payment, the Payments field will be filled with the value of the Amounts field, so that payment or collection will be skipped. At the same time, a new line will be created with the new Payment or Collection Date and the new Amount, which is always the same, and therefore will copy the one imported from the previous movement. Regarding the new date, the script will take into account the “Periodicity” field, a text field that marks the type of period that passes between one movement and the next, monthly, annual, etc., and which is also outside the Calculations table. And with this the next pending payment or collection will be fulfilled. 2 more fields will also be fulfilled, which are outside the Calculations table: – Next payment or collection date, date type field, which will copy the date of the last pending payment or collection that has been created. – Balance/next payment or collection, field calculated with a formula, which is the balance between the totals of the Amounts and the Payments. Here the new balance will be incorporated, which will be equal to the previous one. And that would be it. I want to clarify that I also have records with different imports, but I find it very difficult to measure these in a script. Each record in the form is a different payment or collection and are independent of each other. Without prejudice to the fact that the amounts are added to obtain totals and balances.
Obviously the script does not work. It has no errors in the console, but not working. - Anyway, I hope I have explained myself. Although I will clarify any doubts. And this is the script
var fechaActual = new Date();
var registros = form.getRecords();registros.forEach(function (registro) {
var calculos = registro.getFieldValue(‘fld-d8a58dbf35914f3c858c6dfb880c1c48’) || [];
var fechaProximoPagoCobro = registro.getFieldValue(‘fld-3671b67b092b4b3b949984292c023511’);
var saldoProximoPagoCobro = registro.getFieldValue(‘fld-a554770fd7834b22802b65c488be9f0d’);
var periodicidadTexto = registro.getFieldValue(‘fld-aaba9a24064a4a8893a3963f7cbe8595’);
if (calculos.length > 0 && fechaProximoPagoCobro && saldoProximoPagoCobro && periodicidadTexto) {
var fechaUltimoCalculo = new Date(calculos[calculos.length – 1].fecha_pago_o_cobro_id);// Si la última fecha de pago es anterior a la fecha actual
if (fechaUltimoCalculo < fechaActual) {
var nuevaFecha = new Date(fechaUltimoCalculo);
var meses = { ‘Mensual’: 1, ‘Bimensual’: 2, ‘Trimestral’: 3, ‘Semestral’: 6, ‘Anual’: 12 };
var dias = { ‘7 días’: 7 };// Añadir tiempo a la fecha según la periodicidad
if (meses[periodicidadTexto]) {
nuevaFecha.setMonth(nuevaFecha.getMonth() + meses[periodicidadTexto]);
} else if (dias[periodicidadTexto]) {
nuevaFecha.setDate(nuevaFecha.getDate() + dias[periodicidadTexto]);
}// Si la nueva fecha es en el futuro, añadimos una nueva entrada
if (nuevaFecha > fechaActual) {
var ultimoImporte = calculos[calculos.length – 1].importes_y_acumulado_id;
calculos.push({
fecha_pago_o_cobro_id: nuevaFecha,
importes_y_acumulado_id: ultimoImporte,
pagos_y_acumulado_id: ”
});
}// Actualizamos el campo ‘Pagos y acumulado’ del último cálculo
calculos[calculos.length – 1].pagos_y_acumulado_id = saldoProximoPagoCobro;// Guardamos los cambios en el registro
registro.setFieldValue(‘fld-d8a58dbf35914f3c858c6dfb880c1c48’, calculos);form.saveRecord(registro);
}
}
});Thank you in advance for your kind help.
- Hi everybody,