0

I would like to calculate my Contacts age based on his birthday. With some search I came up with this code:

(add code to Edit.js)

/*** Function to calculate Age based on birthday field change*/ 

jQuery('[name="birthday"]',form).change(function() {
var dateBirth = Vtiger_Helper_Js.getDateInstance(jQuery('[name="birthday"]',form).val(),jQuery('[name="birthday"]',form).attr("data-date-format"));

jQuery('[name="cf_755"]',form).val( Math.floor((Date.now()-dateBirth) / (31557600000)));

});

It doesn't work as it is and apart from that, it would change the age of the contact only on updating the birthday field. How to make this code work when any field has been updated?

Even better would be a real-time calculation of the age of the contact when the contact's page has been opened i.e. in detail view.

Any help is much appreciated!

danh
  • 62,181
  • 10
  • 95
  • 136
RoberJr
  • 1
  • 2

1 Answers1

0

You need something like the following code:

$('form[id="EditView"] input, form[id="EditView"] select').change(function() {
    var dateBirth = Vtiger_Helper_Js.getDateInstance(jQuery('input[name="birthday"]').val(),jQuery('input[name="birthday"]').attr("data-date-format"));
    jQuery('input[name="cf_755"]').val( Math.floor((Date.now()-dateBirth) / (31557600000)));
});

also there is a simlest way to set value into cf_755 input. Use Vtiger Expressions to compute the values via workflows. you just need to create a workflow and calculate age via time_diffdays method. click here to check vtiger expressions

Hamid
  • 378
  • 3
  • 8
  • thank you for your effort! I have setup the workflow successfully, but I don't like that it's showing the updated age in the history. My intention is to hide the updates from the history in Vtiger 7. My guess would be to write the code in a function like it was solved here: https://stackoverflow.com/questions/56628996/calculation-on-vtiger-field-before-save-handler-on-javascript-side-code-inside please check my submitted jsfiddle for the Edit.js – RoberJr Jul 08 '19 at 18:01