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