2
votes

First of all, I am very new in dynamics crm.

I have situation where I have to calculate the age of person where case occurred and involved person type is "Complainant"

For that we have 3 Custome Entity

new_Case 
    Occurrence Date
new_involvePerson
    Involved Person Type(following are types)
        - Complainant
        - Supervisor
        - Investigator
and New_person
        - DOB

Now, user update the Occurrence Date in Incident, I have to calculate age of complainant Involved Person

Is it possible to do in Javascript OnSave when Incident Case save? Or do i have to create a workflow or Plug-in? Is it possible to get the sample code

Thanks in advance

1

1 Answers

1
votes

Yes it's possible. You just have to write a function and trigger it on the on save of the incident form.

Check if Occurence Date is not empty then Retrieve the new_involvePerson's type and if that equals Complainant and then it's easy you just Retrieve the involvePerson's date of birth and calculate the age.

calculateAge = function (date) {

    var birthDate = new Date(date);
    var todayDate = new Date();
    var todayYear = todayDate.getFullYear();
    var todayMonth = todayDate.getMonth();
    var todayDay = todayDate.getDate();
    var birthYear = birthDate.getFullYear();
    var birthMonth = birthDate.getMonth();
    var birthDay = birthDate.getDate();

    var age = todayYear - birthYear;

    if (todayMonth < birthMonth) {
        age--;
    }

    if (birthMonth == todayMonth && todayDay < birthDay) {
        age--;
    }
    if (age != undefined && age != null) {
        Xrm.Page.getAttribute("yourfieldhere").setValue(age);
    }
    else {
        Xrm.Page.getAttribute("yourfieldhere").setValue(null);
    }
}