Here I am going to create auto numbering plugin for lead entity
Step 1:
First create one custom entity for Auto Numbering. Which is
used for configuring auto numbering operation.
Step 2:
Create following fields
1.
Entity Name
2.
Prefix or Postfix
3.
Starting Number
Step 3:
Create a field to display auto number in the required entity
Step 4:
Using the code below create a plugin for auto number generation.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Discovery;
using Microsoft.Crm.Sdk.Messages;
using System.ServiceModel;
using System.Runtime.Serialization;
namespace AutoNumberPlugin
{
public class Class1:IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.
GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.
GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service =
serviceFactory.CreateOrganizationService(context.UserId);
if (context.InputParameters.Contains("Target")
&& context.InputParameters["Target"] is Entity)
{
Entity entity = (Entity)context.InputParameters["Target"];
if (entity.LogicalName == "lead")
{
try
{
var fetchXml ="<fetch version='1.0' output-format='xml-platform'
mapping='logical' distinct='false'>";
fetchXml +="<entity name='new_auto'>";
fetchXml +="<attribute name='new_autoid' />";
fetchXml +="<attribute name='new_name' />";
fetchXml +="<attribute name='new_prefix' />";
fetchXml +="<attribute name='new_startingnumber' />";
fetchXml +="<attribute name='createdon' />";
fetchXml +="<order attribute='new_name' descending='false'
/>";
fetchXml +="<filter type='and'>";
fetchXml +="<condition attribute='new_name' operator='eq'
value='"+entity.LogicalName+"' />";
fetchXml +="</filter>";
fetchXml +="</entity>";
fetchXml +="</fetch>";
var result =
service.RetrieveMultiple(new FetchExpression(fetchXml));
if (result.Entities.Count >
0)
{
Guid AutoNumberId = ((Guid)result.Entities[0].Attributes["new_autoid"]);
String Prefix = ((String)result.Entities[0].Attributes["new_prefix"]);
String Number = ((String)result.Entities[0].Attributes["new_startingnumber"]);
Int32 No = Convert.ToInt32(Number);
No = No + 1;
String ACCcode = Prefix +
(Convert.ToString(No)).PadLeft(4,
'0');
ColumnSet attributes1 = new ColumnSet(new string[] { "leadid", "new_lead_id" });
Entity acc =
service.Retrieve("lead", entity.Id, attributes1);
if (!(acc.Contains("new_lead_id")))
{
acc.Attributes["new_lead_id"] = ACCcode;
service.Update(acc);
ColumnSet attributes2 = new ColumnSet(new string[] { "new_startingnumber" });
Entity ano =
service.Retrieve("new_auto", AutoNumberId, attributes2);
ano.Attributes["new_startingnumber"] = Convert.ToString(No);
service.Update(ano);
}
}
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException("An error occured in
plugin. " + ex, ex);
}
}
}
}
}
}