Welcome to MSDN Blogs Sign in | Join | Help

East Region Microsoft CRM Blog

This Blog is focused on Microsoft CRM. It specalizes in news for CRM partners and customers, customization tools for demos and local events in the Microsoft East Region.

News

  • Welcome to the Mid-Atlantic CRM Blog, formerly owned and operated by Ben Vollmer, a CRM legend ;). My name is Chris Kahl and Ben has asked me to take over the care and feeding of this most valuable resource. Like Ben, my job is to help partners and customers with Microsoft CRM in the Eastern US. This blog is a great and searchable information source. Feel free to subscribe to the RSS feed to stay current with Dynamics CRM. Any tools discussed here are for DEMO only. You should NOT use these in a production environment unless you test them. Microsoft Support DOES NOT support these tools. Everything here is provided as-is with no warranty.
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!

 

Posted: Tuesday, August 08, 2006 10:45 PM by Ben Vollmer

Comments

Ronald Lemmen said:

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
# August 9, 2006 2:20 AM

Ben Vollmer said:

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
# August 9, 2006 8:11 AM

TrackBack said:

# August 9, 2006 9:25 PM

mkarthik said:

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
# August 10, 2006 1:17 PM

Ben Vollmer said:

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
# August 10, 2006 4:40 PM

Ultram addiction. said:

Ultram addiction. Ultram side effects.

# July 21, 2008 2:05 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

  
Enter Code Here: Required

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker