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.
Creating a Unique Number in Microsoft CRM

The SDK has an example that shows you using a Post Call Out you to create a unique identifier for each new contact record. So the GUID can be replaced with a number that is a little more user friendly. :-)

This also needed to work in a hosted environment where the client did not have the ability to register a post call out or .net assembly.

So for those of you who write code, go ahead and make fun of me now, but this is a script that should allow you to create a unique identifier for a customer record. In my case, we also appended the first letter of the contacts name to make it a little more random as well. :-)

  • For this, we used the Create On Date field, which already exists, but the data value does not get populated until the record is saved, which makes this script a little hard to use, until the first save. So, using this tip, we auto save the record as soon as the record loads. So opening the form's properties, in the OnLoad Event, have this code fire:

crmForm.Save();

  • Now, I disabled the new field (called Member Number) and placed this code in that field's event:
  • var Year = crmForm.all.createdon.DataValue.getYear();
    var Month = crmForm.all.createdon.DataValue.getMonth();
    var Minutes = crmForm.all.createdon.DataValue.getMinutes();
    var Seconds = crmForm.all.createdon.DataValue.getSeconds();
    crmForm.all.new_membernumber.DataValue = "COMP"+Year.toString()+Month.toString()+Minutes.toString()+Seconds.toString();
    crmForm.all.new_membernumber.ForceSubmit = true;

  • In the member type field, when they turn them to a member, we put this in their OnChange event:

new_membernumber_onchange0();

  • Then in the Form's OnSave, add this line:

crmForm.all.new_membernumber.ForceSubmit = true;

It will create a number that looks like this: COMP20071921. It could be done with some more complication and more error checking, but this customer here only has 300 or so new members a year, so this should not allow for any duplicates. :-)

Posted: Thursday, February 15, 2007 7:00 AM by Ben Vollmer
Filed under:

Comments

MD said:

Too bad JScript doesn't have a RAND() function...

# February 18, 2007 10:18 PM

le said:

var Year = crmForm.all.createdon.DataValue.getYear();

var Month = crmForm.all.createdon.DataValue.getMonth();

var Minutes = crmForm.all.createdon.DataValue.getMinutes();

var Seconds = crmForm.all.createdon.DataValue.getSeconds();

crmForm.all.new_membernumber.DataValue = "COMP"+Year.toString()+Month.toString()+Minutes.toString()+Seconds.toString();

crmForm.all.new_membernumber.ForceSubmit = true;

# April 4, 2007 2:44 AM

le said:

var Year = crmForm.all.createdon.DataValue.getYear();

var Month = crmForm.all.createdon.DataValue.getMonth();

var Minutes = crmForm.all.createdon.DataValue.getMinutes();

var Seconds = crmForm.all.createdon.DataValue.getSeconds();

crmForm.all.new_membernumber.DataValue = "COMP"+Year.toString()+Month.toString()+Minutes.toString()+Seconds.toString();

crmForm.all.new_membernumber.ForceSubmit = true;

# April 4, 2007 2:44 AM

le said:

var Year = crmForm.all.createdon.DataValue.getYear();

var Month = crmForm.all.createdon.DataValue.getMonth();

var Minutes = crmForm.all.createdon.DataValue.getMinutes();

var Seconds = crmForm.all.createdon.DataValue.getSeconds();

crmForm.all.new_membernumber.DataValue = "COMP"+Year.toString()+Month.toString()+Minutes.toString()+Seconds.toString();

crmForm.all.new_membernumber.ForceSubmit = true;

# April 4, 2007 2:44 AM

ADA said:

I have similar approach, I cannot use the OnLoad event of the form, because I have an workflow which fires OnSave. My code which I put OnChange event of a picklist is:

if (crmForm.FormType==1)

 {

 var datum=new Date()

 var Rok = datum.getYear();

 var Mesic = 100+datum.getMonth()+1;

 var Den = 100+datum.getDate();

 var MesicS=Mesic.toString();

 var MesicSS=MesicS.substring(1,3);

 var DenS=Den.toString();

 var DenSS=DenS.substring(1,3);

 var typakce=crmForm.all.lfc_typakce.DataValue

 switch(typakce)

   {

   case (typakce="1"):

   crmForm.all.lfc_kodakce.DataValue = "MKT"+Rok.toString()+MesicSS+DenSS;break

   case (typakce="2"):

   crmForm.all.lfc_kodakce.DataValue = "SKOL"+Rok.toString()+MesicSS+DenSS;break

   case (typakce="3"):

   crmForm.all.lfc_kodakce.DataValue = "PROD"+Rok.toString()+MesicSS+DenSS;break

   default:

   crmForm.all.lfc_kodakce.DataValue = "";break

   }

crmForm.all.lfc_kodakce.ForceSubmit=true;

}

Which:

1) checks whether the form is new

2) takes current date, formats it to YYYYMMDD e.g. 20081203

3) puts the prefix regarding the picklist.

4) saves the hidden field

pls. note the month number should be getMonth()+1

# December 3, 2008 6:21 AM

ibiz said:

We have an AutoNumber add-in that can create an AutoNumber for any customizable entity in CRM. But it does even more such as allow you to backfill existing records with the AutoNumber.

Take a look at: http://crminnovation.com/autonumber.asp

Jerry

# December 3, 2008 9:01 AM

Tim said:

MD,

Rand() couldn't guarantee a unique number.

Thanks!

# September 11, 2009 6:22 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