Community
Showing results for 
Search instead for 
Do you mean 
Reply

Code for Calculating Age From Birthday

Copper Contributor
Posts: 62
Country: USA

Code for Calculating Age From Birthday

[ Edited ]

This code creates a field in the database with the alias of Age and calculates the Age off of the Birthday field. You still must go into the Layout Designer and add the Age field to the contacts layout manually.

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

using Act.Framework;

using Act.Framework.ComponentModel;

using Act.UI;

using Act.UI.Core;

using Act.Framework.MutableEntities;

using Act.Framework.Database;

 

namespace Bday

{

    public class Class1 : IPlugin

         {

        private ActApplication actApp;

 

        void IPlugin.OnLoad(ActApplication actApp)

                  {

            this.actApp = actApp;

                            actApp.AfterLogon += new EventHandler(Application_AfterLogon);

                            actApp.BeforeLogoff += new EventHandler(Application_BeforeLogoff);

                  }

 

        void IPlugin.OnUnLoad()

                  {

            //Unload

                  }

          

        private void Application_AfterLogon(object sender, EventArgs e)

                 {

                            actApp.CurrentContactChanged += new EventHandler(actApp_CurrentContactChanged);

                            CreateField("Age");

                 }

       

       void actApp_CurrentContactChanged(object sender, EventArgs e)

                 {

                             actApp.ApplicationState.CurrentContact.Fields.FieldChanged += new Act.Framework.MutableEntities.MutableEntity.FieldCollection.FieldChangedHandler(Fields_FieldChanged);

                             PullContactInfo();

                 }

 

       private void Application_BeforeLogoff(object sender, EventArgs e)

                {

            if (actApp != null)

                            {

                                      actApp.AfterLogon -= new EventHandler(Application_AfterLogon);

                                      actApp.BeforeLogoff -= new EventHandler(Application_BeforeLogoff);

                                      actApp.CurrentContactChanged -= new EventHandler(actApp_CurrentContactChanged);

                            }

                }

 

       void Fields_FieldChanged(string fieldName)

                {

                            PullContactInfo();

                }

 

       private void CreateField(string fieldName)

                {

            FieldDescriptorCollection allContactFields = actApp.ActFramework.Fields.GetFields(Act.Framework.RecordType.Contact);

 

            bool hasAgeField = false;

                            foreach (FieldDescriptor f in allContactFields)

                            {

                 if (f.Alias == "Age")

                                        {

                                                   hasAgeField = true;

                                        }

                            }

            if (hasAgeField == false)

                            {

                 FieldDescriptor ageField = new FieldDescriptor(fieldName, Act.Framework.RecordType.Contact, FieldDataType.Character);

                                        actApp.ActFramework.Database.LockDatabase(Act.Framework. DatabaseLockReason.SchemaChanges);

                                        actApp.ActFramework.Fields.Save(ageField);

                                        actApp.ActFramework.Database.UnlockDatabase();

                            }

                }

 

       public void PullContactInfo()

                {

            try

                            {

                 string CalcAge;

                 string fieldname2 = "TBL_CONTACT.BIRTHDATE";

                                        DBFieldDescriptor birthday = actApp.ActFramework.Contacts.GetContactFieldDescriptor(fieldname2);

                 DBFieldDescriptor age = actApp.ActFramework.Contacts.GetContactFieldDescriptor("AGE", FieldNameType.Alias);

                                        string value = Convert.ToString(birthday.GetValue(actApp.ApplicationState.CurrentContact));

                                        DateTime DPicker = Convert.ToDateTime(value);

                                        CalcAge = Convert.ToString(CalculateAge(DPicker));

                                        age.SetValue(actApp.ApplicationState.CurrentContact, CalcAge);

                            }

            catch

                            {

                            }

                }

 

       public static int CalculateAge(DateTime birthDate)

                {

            DateTime now = DateTime.Today;

                            int years = now.Year - birthDate.Year;

                            if(now.Month < birthDate.Month || (now.Month == birthDate.Month && now.Day < birthDate.Day))

--years;

            return years;

                }

        }

}

Bronze Super Contributor
Posts: 1,231
Country: USA

Re: Code for Calculating Age From Birthday

Mark,

Thanks for the code. Here at Durkin Computing we get this request a lot. Not just for Birthday but also for any date field. The problem which always stops us is the AGE field is only updated on the contact changed event in this type of solution.

 

If you go to the contact list view the AGE value is either not populated ( never been walked on) or is out of date ( Walk on two years ago). You can overcome this by doing a batch but then it would require user interaction to start the batch process.

 

I suggested a feature request a few months ago where the ANY field would be virtual allowing the a user or plugin to edit/modify the select portion of the field descriptor. That would allow the SQL server to do the heavy lifting without any cumbersome plug-in. Here is a link to the feature request: 

 

http://community.act.com/t5/Sage-ACT-Developer-s-Forum/Feature-Request/m-p/155788/highlight/true#M46...

 

Maybe you can slide the feature request back onto Allen's desk.


Thanks

-- Jim Durkin

DurkinComputing.com