Welcome to MSDN Blogs Sign in | Join | Help

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. :-)

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

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

Comments

# re: Creating a Unique Number in Microsoft CRM

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

Sunday, February 18, 2007 10:18 PM by MD

# re: Creating a Unique Number in Microsoft CRM

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;

Wednesday, April 04, 2007 2:44 AM by le

# re: Creating a Unique Number in Microsoft CRM

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;

Wednesday, April 04, 2007 2:44 AM by le

# re: Creating a Unique Number in Microsoft CRM

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;

Wednesday, April 04, 2007 2:44 AM by le

# re: Creating a Unique Number in Microsoft CRM

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

Wednesday, December 03, 2008 6:21 AM by ADA

# re: Creating a Unique Number in Microsoft CRM

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

Wednesday, December 03, 2008 9:01 AM by ibiz

Leave a Comment

(required) 
required 
(required) 
 
Page view tracker