Dynamics CRM 4.0

Auto Numbering for Dynamics CRM 4.0

One of the most common custom functionality that I have to implement in MSCRM 4.0 is auto numbering. Auto Numbering is often used when an entity needs to have a separate human-friendly id. Although there are many formats that client might want, I am just going to discuss the simplest way and an auto-increment number is as simple as it can be.

After doing the same thing many times, I started to think there must be a better way of doing this. This post will discuss how to make Auto Numbering reusable.

Here goes the tutorial:

First things first – Create AutoNumber entity

This entity will hold the next number. I typically name this entity AutoNumber (new_autonumber).



And don’t forget to tick Settings so that it displays on Settings menu.

And here are the attributes that you should create:

  1. Primary attribute: Entity Name (new_entityname, Business Required, nvarchar(100))
  2. Attribute Name (new_attributename, Business Required, nvarchar(100))
  3. Next Auto Number (next_autonumber, Business Required, int)

You can change the prefixes to anything you like; however, that means changing the code.

Here is the customization file: customization.zip

Second Step – Plugin

This plugin is responsible to retrieve the correct next number for the specified entity and attribute name on create event of the specified entity, and also increment the auto number record.

public void Execute(IPluginExecutionContext context)
{
	if (context == null) throw new InvalidPluginExecutionException("Context is null.", new ArgumentNullException("context"));

	// Verify that there is an entity in the target
	if (context.InputParameters.Properties.Contains(ParameterName.Target)
		&& context.InputParameters.Properties[ParameterName.Target] is DynamicEntity)
	{
		// Obtain the target business entity from the input parmameters.
		DynamicEntity entity = (DynamicEntity)context.InputParameters[ParameterName.Target];

		autoNumberedEntityName = entity.Name;

		string messageUpperCase = context.MessageName.ToUpperInvariant();
		if (messageUpperCase == "CREATE")
		{
			int nextAutoNumber;
			CrmServiceWrapper service = new CrmServiceWrapper(context);

			nextAutoNumber = GetNextAutoNumber(service);

			entity.Properties[autoNumberedAttributeName] = new CrmNumber(nextAutoNumber);
		}
	}
}

Here is the plugins dll: AutoNumberPlugin.dll

And the solution link @ codeplex: click here

Third Step – Register the Plugin

Register the plugin using Plugin Registration Tool and create step on CREATE message for the entity requiring auto-numbering.

The plugin will retrieve from the step configurations the name of the auto-numbered attribute. The format of the configuration will be:

<Settings>
<setting name="autoNumberAttributeName">
<value>(replace this with the auto-numbered attribute name)</value>
</setting>
</Settings>

In this example, “new_campaignnumber” will be the auto-numbered attribute for the step the plugin is registered to. Make sure the entity the plugin step is registered to actually have this attribute.

Fourth Step – It’s all about details

At this point, you can call it a day, and everything will work as it’s supposed to be. However, attention to details will make or break the user experience. The entity form is what I’m talking about. The auto-numbered field might be on the form and user will enter data which will be overwritten by the plugin.

The answer? Set the auto-numbered attribute as read-only on the form and write javascript to set inital data of the attribute (required only if the attribute is required field).

// CRM Form Types
var CRM_FORM_TYPE_CREATE = 1;
var CRM_FORM_TYPE_UPDATE = 2;
var CRM_FORM_TYPE_READONLY = 3;
var CRM_FORM_TYPE_DEACTIVATED = 4;
switch (crmForm.FormType)
{
case CRM_FORM_TYPE_CREATE :
crmForm.all.new_campaignnumber.DataValue = 0;
break;
}

That’s it – You’re Done!

And at this point you might be wondering why I didn’t mention about creating the actual auto-number record for the entity – attribute combination. Well, you don’t need to, the plugin will create a new auto-number record if it doesn’t exist, with initial number of 1.

Cheers – Sy

Advertisements

6 thoughts on “Auto Numbering for Dynamics CRM 4.0

  1. Hi bro,
    I have cretaed as what you mentioned above and everything is work fine. But the generated autonumber is appearing in the entity that i want this autonumber to be appeared.

  2. hmmmm

    I get an HTTP error 400: bad request when saving my entity now… not sure what went wrong… all seems ot be ok according to your guide…

    1. Hi Philippe,

      Please turn on tracing on CRM and try the action again, the log should tell you more information about why it fails.

      Cheers – Sy

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s