Freigeben über


Tracking the Date Fields by Date, Month or Year

-- Update -- Before you use this code, you may want to look at this Blog Post. It contains a number of fixes and is alot cleaner than my code. :-) (This article goes up 8/11/07)

So one of questions posed on one of our internal distribution groups was a question from a partner on how to do something I had never thought of before… And it turns out it is a limitation of the advanced find feature of Microsoft CRM 3.0.

In this case the partner wanted to be able to run a query to select all of the contacts with a birth date in a specific month. (i.e., give me all contacts born in May.) Guess what the advanced find doesn’t have that logic built into it. L

The good news is that fixing it is WAY easy. (It was with some help from Erika in Developer Support.) So in my case I used the anniversary field.

1. Create a Field with the type of Picklist in my case I called it AnnMonth

2. Create values in the Picklist of January through December. (In order… So January should be 1 and December should be 12 in the list of values.)

3. When you place the field on the form, click the disable button so as not to allow for end user input.

4. Place the following code on the OnChange of the anniversary field, which is on the contact form. (Or if not, place it there. J)

// here we are declaring the variable of AnnDate, which is the

// whole anniversary date. We are also making sure it is in date

// format, which it should be, so this step is probably

// redundant.

var AnnDate = new Date (crmForm.all. anniversary.DataValue);

//here we are using the Jscript command to get the date from the

// Date field and get just the monthvar AnnMonth = AnnDate.getMonth();

//Due to the way that Jscript handles the get month, 0=January,

// so we need to add 1 to make it match the picklist we created

// earlier
var AnnMonthInsert = AnnMonth + 1;

//We are now taking that value and inserting it into the form.
crmForm.all.new_AnnMonth.DataValue = AnnMonthInsert.toString();

5. Publish the results and you are all set. You can now do search of people with dates in months, etc… J

If you are interested in going beyond the months, these commands will get you on the right path:

getFullYear () – Pulls the Full Year (i.e., 1999)

getDate() – Date of the Month (i.e., the 20th)

getDay() – Pulls the Day of the week (0=Sunday, so you would probably want to do a picklist and do the plus one trick above. J)

This is what it should look like when done

 

Jason Hunt from Invoke Systems showed me a way to do it without all of the variables, so please don’t make fun of my lousy code writing skills. J If I can find his code, it will be appended here below, so you can see what it should look like. J

Enjoy!

Comments

  • Anonymous
    August 08, 2006
    Hi Ben,

    This is a good solution for a common problem. The only disadvantage of this approach is that there is a field on the form which the user doesn't need. Personally I'd like to have only the fields on the form which are required by the customer. You can get to the same functionality by adding the attribute, but not add it to the form. You would then need a call-out which fills the picklist with the correct value as soon as the contact gets created or updated.

    Kind regards,

    Ronald
    CRM MVP
  • Anonymous
    August 09, 2006
    Ron,
    I agree that the call out is a better solution. But, if you are lazy like I am, this way gets the job done... Based upon the feedback from other folks that solution will be posted as well as some additional code samples. :-)

    Boy am I glad there is more than one way to skin this cat. :-)

    Thanks!
    Ben
  • Anonymous
    August 10, 2006
    Ben/Ron,

    How about doing this in the client side itself like Ben's and hide the attribute in the form using

    crmForm.all.new_AnnMonth.style.display = 'none';

    I would prefer not to use callout if I just have this kind of functionality which can be dealt in the client side itself

    Cheers,
    Karthik
  • Anonymous
    August 10, 2006
    Karthik,
    Kevin makes some good arguments for why a callout would be a good idea. :-) Check back on Friday for a new post that has the reasons why and a new piece of code you may like better. :-)

    HTH
    Ben
  • Anonymous
    July 21, 2008
    Ultram addiction. Ultram side effects.