Categorize Contacts by Age Group

Categorize Contacts by Age Group

  • Comments 1

Many times in health related organizations, it is necessary to keep track of the age categories for patients.  While one might think that this would be an easy out-of-the-box configuration, it's really not (...yet).

Just for fun and a bit of edification to users getting started with JScripting, I put the following scenario together with the sample script below.  Enjoy!

 Scenario: 

  1. Create bit value fields to store the age categories as defined by the US Census and CDC
  2. Add the fields onto the Contact form and make them appear as a checkbox field on the form
  3. Add the script to the OnLoad event for the form

Inside the script it does the following things:

  1. Hide all age group category fields
  2. calculate the age of the user based on the current system date and the record's Birthday field value  **exceptions are not accounted for here since this is just an illustratory script.
  3. Clear all age group field values
  4. Check which age group field to check based on the calculated age
  5. Unhide the field with the checked value 

(The script is verbose to allow for easier reading.)

//BEGIN HIDE ALL AGEGROUP FIELDScrmForm.all.new_agegroupchild_c.style.display = "none";crmForm.all.new_agegroupchild_d.style.display = "none";crmForm.all.new_agegroup20to24_c.style.display = "none";crmForm.all.new_agegroup20to24_d.style.display = "none";crmForm.all.new_agegroup25to29_c.style.display = "none";crmForm.all.new_agegroup25to29_d.style.display = "none";crmForm.all.new_agegroup30to34_c.style.display = "none";crmForm.all.new_agegroup30to34_d.style.display = "none";crmForm.all.new_agegroup35to39_c.style.display = "none";crmForm.all.new_agegroup35to39_d.style.display = "none";crmForm.all.new_agegroup40to44_c.style.display = "none";crmForm.all.new_agegroup40to44_d.style.display = "none";crmForm.all.new_agegroup45to49_c.style.display = "none";crmForm.all.new_agegroup45to49_d.style.display = "none";crmForm.all.new_agegroup50to54_c.style.display = "none";crmForm.all.new_agegroup50to54_d.style.display = "none";crmForm.all.new_agegroup55to59_c.style.display = "none";crmForm.all.new_agegroup55to59_d.style.display = "none";crmForm.all.new_agegroup60to64_c.style.display = "none";crmForm.all.new_agegroup60to64_d.style.display = "none";crmForm.all.new_agegroup65to69_c.style.display = "none";crmForm.all.new_agegroup65to69_d.style.display = "none";crmForm.all.new_agegroup70to74_c.style.display = "none";crmForm.all.new_agegroup70to74_d.style.display = "none";crmForm.all.new_agegroup75to79_c.style.display = "none";crmForm.all.new_agegroup75to79_d.style.display = "none";crmForm.all.new_agegroup80to84_c.style.display = "none";crmForm.all.new_agegroup80to84_d.style.display = "none";crmForm.all.new_agegroup85plus_c.style.display = "none";crmForm.all.new_agegroup85plus_d.style.display = "none";//END HIDE ALL AGEGROUP FIELDS //BEGIN CALCULATING AGEvar byr = crmForm.all.birthdate.DataValue.getFullYear();var today = new Date();today.setDate(today.getDate());var cyr;cyr = today.getFullYear();var bmth = crmForm.all.birthdate.DataValue.getMonth() + 1;var cmth = today.getMonth() + 1;var ageinmos = (cyr - byr) * 12;var modiff = cmth - bmth;var realageinyrs = (ageinmos + modiff) / 12;//END CALCULATING AGE //BEGIN DEFINE FUNCTION TO SHOW USED FIELDvar fld2use;function showused (){eval ("crmForm.all." + fld2use + "_c.style.display = '';");eval ("crmForm.all." + fld2use + "_d.style.display = '';");};//END DEFINE FUNCTION TO SHOW USED FIELD //BEGIN DEFINE FUNCTION TO CLEAR ALL AGEGROUP FIELDSfunction clearagegroups() {    crmForm.all.new_agegroupchild.DataValue = null;    crmForm.all.new_agegroup20to24.DataValue = null;    crmForm.all.new_agegroup25to29.DataValue = null;    crmForm.all.new_agegroup30to34.DataValue = null;    crmForm.all.new_agegroup35to39.DataValue = null;    crmForm.all.new_agegroup40to44.DataValue = null;    crmForm.all.new_agegroup45to49.DataValue = null;    crmForm.all.new_agegroup50to54.DataValue = null;    crmForm.all.new_agegroup55to59.DataValue = null;    crmForm.all.new_agegroup60to64.DataValue = null;    crmForm.all.new_agegroup65to69.DataValue = null;    crmForm.all.new_agegroup70to74.DataValue = null;    crmForm.all.new_agegroup75to79.DataValue = null;    crmForm.all.new_agegroup80to84.DataValue = null;    crmForm.all.new_agegroup85plus.DataValue = null;};//END DEFINE FUNCTION TO CLEAR ALL AGEGROUP FIELDS //BEGIN FLAG APPROPRIATE AGEGROUP AND HIDE ALL OTHERSif (realageinyrs < 20) {    clearagegroups();    crmForm.all.new_agegroupchild.DataValue = true;     fld2use = "new_agegroupchild";    showused();    };if (realageinyrs > 20 & realageinyrs < 24) {    clearagegroups();    crmForm.all.new_agegroup20to24.DataValue = true;    fld2use = "new_agegroup20to24";    showused();    };if (realageinyrs > 25 & realageinyrs < 29) {    clearagegroups()    crmForm.all.new_agegroup25to29.DataValue = true;    fld2use = "new_agegroup25to29";    showused()    };if (realageinyrs > 30 & realageinyrs < 34) {    clearagegroups()    crmForm.all.new_agegroup30to34.DataValue = true;    fld2use = "new_agegroup30to34";    showused()    };if (realageinyrs > 35 & realageinyrs < 39) {    clearagegroups()    crmForm.all.new_agegroup35to39.DataValue = true;    fld2use = "new_agegroup35to39";    showused()    };if (realageinyrs > 40 & realageinyrs < 44) {    clearagegroups()    crmForm.all.new_agegroup40to44.DataValue = true;    fld2use = "new_agegroup40to44";    showused()    };if (realageinyrs > 45 & realageinyrs < 49) {    clearagegroups()    crmForm.all.new_agegroup45to49.DataValue = true;    fld2use = "new_agegroup45to49";    showused()    };if (realageinyrs > 50 & realageinyrs < 54) {    clearagegroups()    crmForm.all.new_agegroup50to54.DataValue = true;    fld2use = "new_agegroup50to54";    showused()    };if (realageinyrs > 55 & realageinyrs < 59) {    clearagegroups()    crmForm.all.new_agegroup55to59.DataValue = true;    fld2use = "new_agegroup55to59";    showused()    };if (realageinyrs > 60 & realageinyrs < 64) {    clearagegroups()    crmForm.all.new_agegroup60to64.DataValue = true;    fld2use = "new_agegroup60to64";    showused()    };if (realageinyrs > 65 & realageinyrs < 69) {    clearagegroups()    crmForm.all.new_agegroup65to69.DataValue = true;    fld2use = "new_agegroup65to69";    showused()    };if (realageinyrs > 70 & realageinyrs < 74) {    clearagegroups()    crmForm.all.new_agegroup70to74.DataValue = true;    fld2use = "new_agegroup70to74";    showused()    };if (realageinyrs > 75 & realageinyrs < 79) {    clearagegroups()    crmForm.all.new_agegroup75to79.DataValue = true;    fld2use = "new_agegroup75to79";    showused()    };if (realageinyrs > 80 & realageinyrs < 84) {    clearagegroups()    crmForm.all.new_agegroup80to84.DataValue = true;    fld2use = "new_agegroup80to84";    showused()    };if (realageinyrs > 85) {    clearagegroups()    crmForm.all.new_agegroup85plus.DataValue = true;    fld2use = "new_agegroup85plus";    showused()    };

//END FLAG APPROPRIATE AGEGROUP AND HIDE ALL OTHERS

 

  • P.S.  Keep in mind that while this example is a functional example, an optimized model might be to incorporate a typed array with the field names then call them in a for...in loop.