Use the contents of a database cell in another cell.

On my website I have a registration form for a warranty certificate.
The form has a purchase date cell.
My database has an expiration date (which is not on the registration form) for the warranty.
This date should be two years after the date of purchase.
How do I create a code that takes the contents of the cell of the purchase date and passes it to the expiring cell and adds two years to it?

1 Like

Use the before insert data hook to take the purchase date and set the warranty field.

In data.js at the backend the code should be something like:

export function collection_beforeInsert(item, context) {
 let warranty = //converted date based on item.purchase
 item.warranty = warranty;
    return item;
}

Sorry I think I was not clear.
I try to take the data from a particular date cell (purchase date) and move it to another date cell (invalid date) When it was added two years already.
This is the warranty registration page. Added just to illustrateץ


The data taken from the database should be added to this page automatically, so the dates must be added as I want.

Hi,

To calculate two years from a certain date:

let currDate = new Date(); //gets the current date, for example. Replace this with the date input
let year = currDate.getFullYear();
let month = currDate.getMonth();
let day = currDate.getDate();
let twoYearsFromNow = new Date(year + 2, month, day)


I dont know what is wrong?


Where is my mistake?
I tried to follow your instructions and this is the code I have, but when I fill in the registration form there is an error message.

Hi,
Remove the parenthesis from line 2:

let dateOfPurchase = item.dateOfPurchase;


Not working :frowning:

Hi,

I used the code below in a test site and it worked as expected.
Make sure you are using the correct field IDs and that the error is not originating from somewhere else.

export function test_beforeInsert(item, context) {
	let purchasedate = item.date;
	let year = purchasedate.getFullYear();
	let month = purchasedate.getMonth();
	let day = purchasedate.getDate();
	let twoYearsFromNow = new Date(year + 2, month, day)
	item.newField = twoYearsFromNow;
	return item;
}

There is no error with registration now but the code does nothing.

Hi,

Please paste your editor url here and we will look further.

Hi,

I can see the hook code however the hook event was not added to the ‘warranty’ collection
See how to add hooks here

really dont know why its dont work…

I added to the warranty collection

Hi,
Can you please paste again a link to your site ?
Roi

  1. My dynamic page worked beofre and its not working now Does it have anything to do with adding the code?
    it’d be great if somone can tell me what can i do to make it work.
  2. I want to change the date format from Sat Feb 14 1959 02:00:00 GMT+0200 (Eastern Europe Standard Time) to 2018/12/30 and also keep the hooks working.
    is the following code can work?

export function warrantycard_beforeInsert(item , context)
{
let oldDate = item.dateOfPurchase;
let year = oldDate.getFullYear();
let month = oldDate.getMonth()+1;
let day = oldDate.getDate();
let freshYear = year+2();
let dateFresh = year + “/” + month + “/” + day();
let twoYearsFromNow = freshYear + “/” + month + “/” + day();
item.dateOfPurchase = dateFresh();
item.invalidDate = twoYearsFromNow();
return item;

}