Posts Tagged ‘AutoNumber’


using System;

using System.Collections.Generic;

using System.Text;

using Microsoft.Crm.Sdk;

using Microsoft.Crm.SdkTypeProxy;

using Microsoft.Crm.Sdk.Query;

using System.Web.Services.Protocols;

namespace CRM.Customization.AutoNumber

{

public class AutoNumberPlugIns:IPlugin

{

DynamicEntity entity = null;

int startNumber;

string prefix = string.Empty;

string seperator = string.Empty;

int nextMaxNumber;

string uniqueId = string.Empty;

#region IPlugin Members

public void Execute(IPluginExecutionContext context)

{

if (context.InputParameters.Properties.Contains(“Target”) &&

context.InputParameters.Properties[“Target”] is DynamicEntity)

{

entity = (DynamicEntity)context.InputParameters.Properties[“target”];

ICrmService service = context.CreateCrmService(true);

if (entity.Name ==“new_testentity”)

{

// Find maximum number

int maxNumber = FindMaxNumber(service);

nextMaxNumber=maxNumber + 1;

if (nextMaxNumber > 0)

{

bool isRetrivePrefixSuccess = RetrievePrefixSufixAndStartNumber(service);

if (isRetrivePrefixSuccess)

{

if (nextMaxNumber >= startNumber)

{

string buildAutoNumber = prefix + seperator + nextMaxNumber.ToString();

uniqueId = buildAutoNumber;

StringProperty uId = new StringProperty(“new_uniqueid”, uniqueId);

entity.Properties.Add(uId);

CrmNumber nextNumber = new CrmNumber();

nextNumber.Value = nextMaxNumber;

CrmNumberProperty uniqueNo = new CrmNumberProperty(“new_uniqueno”, nextNumber);

entity.Properties.Add(uniqueNo);

}

}

}

}

}

}

private int FindMaxNumber(ICrmService service)

{

int maxNo=0;

QueryExpression query = new QueryExpression();

query.EntityName = “new_testentity”;

ColumnSet cols = new ColumnSet();

cols.AddColumn(“new_uniqueno”);

query.ColumnSet = cols;

query.AddOrder(“new_uniqueno”, OrderType.Descending);

PagingInfo pageInfo = new PagingInfo();

pageInfo.Count = 1;

pageInfo.PageNumber = 1;

query.PageInfo = pageInfo;

RetrieveMultipleRequest request = new RetrieveMultipleRequest();

request.Query = query;

request.ReturnDynamicEntities = true;

RetrieveMultipleResponse response = (RetrieveMultipleResponse)service.Execute(request);

DynamicEntity testEntity = (DynamicEntity)response.BusinessEntityCollection.BusinessEntities[0];

for (int i = 0; i < testEntity.Properties.Count; i++)

{

if (testEntity.Properties.Contains(“new_uniqueno”))

{

CrmNumber uniqueNumber = (CrmNumber)testEntity.Properties[“new_uniqueno”];

maxNo = uniqueNumber.Value;

}

}

return maxNo;

}

private bool RetrievePrefixSufixAndStartNumber(ICrmService service)

{

try

{

QueryByAttribute query = new QueryByAttribute();

query.EntityName = “new_autonumber”;

ColumnSet cols = new ColumnSet();

cols.AddColumn(“new_startnumber”);

cols.AddColumn(“new_prefix”);

cols.AddColumn(“new_currentnumber”);

cols.AddColumn(“new_separator”);

query.ColumnSet = cols;

query.Attributes = new string[] { “new_entityname” };

query.Values = new object[] { “new_testentity” };

RetrieveMultipleRequest request = new RetrieveMultipleRequest();

request.Query = query;

request.ReturnDynamicEntities = true;

RetrieveMultipleResponse response = (RetrieveMultipleResponse)service.Execute(request);

DynamicEntity dynamicsEntity = (DynamicEntity)response.BusinessEntityCollection.BusinessEntities[0];

if (dynamicsEntity.Properties.Count > 0)

{

// DynamicEntity autoNumberEntity = (DynamicEntity)dynamicsEntity.BusinessEntities[0];

// Extract the fullname from the dynamic entity string fullname;

for (int i = 0; i < dynamicsEntity.Properties.Count; i++)

{

if (dynamicsEntity.Properties.Contains(“new_startnumber”))

{

CrmNumber startNo = (CrmNumber)dynamicsEntity.Properties[“new_startnumber”];

startNumber = startNo.Value;

}

if (dynamicsEntity.Properties.Contains(“new_prefix”))

{

String prefixId = (String)dynamicsEntity.Properties[“new_prefix”];

prefix = prefixId.ToString();

}

if (dynamicsEntity.Properties.Contains(“new_separator”))

{

String seperaterId = (String)dynamicsEntity.Properties[“new_separator”];

seperator = seperaterId.ToString();

}

}

}

return true;

}

catch (SoapException ex)

{

throw ex;

}

}

}

#endregion

}

Advertisements

Create two field as we created..

1) New_uniqueid ( Int)

2) New_id( nvarchar).

If you want AutoNumber as Int , no need to create two fields ,but we wanted AutoNumber as in specific format(OP/DEC08/0000000001) so we reqired to two field.

Steps for AutoNumber:

1) Retrieved the maximum number for entity based on field new_uniqueid.

2) Update both field new_uniqueid and new_id using web service on PostCreate event.

Source Code

using System;

using System.Collections.Generic;

using System.Text;

using System.Xml;

using System.Xml.Serialization;

using System.IO;

using System.Collections;

using Microsoft.Crm.Sdk;

using System.Data;

using System.Data.SqlClient;

using System.Web.Services.Protocols;

namespace CRM.PlugIn.UniqueIdGeneration

{

public class PlugInForUniqueId : IPlugin

{

string serverName = string.Empty;

string domain = string.Empty;

string authUser = string.Empty;

int leftPad;

string uniqueNoField=string.Empty;

string uniqueIdField=string.Empty;

static string logPath = string.Empty;

// get configuration string

public PlugInForUniqueId(string unsecure, string secureConfiguration)

{

XmlDataDocument doc = new XmlDataDocument();

doc.LoadXml(secureConfiguration);

// get database server name

XmlNode database = doc.SelectSingleNode(“//Database”);

serverName= database.InnerText;

// get domain name

XmlNode domainName = doc.SelectSingleNode(“//Domain”);

domain = domainName.InnerText;

// CRM User as Authentication User

XmlNode authUserName = doc.SelectSingleNode(“//AuthUser”);

authUser = authUserName.InnerText;

// get total no of digit for padding

XmlNode padding = doc.SelectSingleNode(“//Padding”);

leftPad=Convert.ToInt32( padding.InnerText);

// get FieldName and type of datatype should be Integer

XmlNode uniqueNo = doc.SelectSingleNode(“//UniqueNumber”);

uniqueNoField= uniqueNo.InnerText;

// get FieldName and type of datatype should be nvarchar

XmlNode Id = doc.SelectSingleNode(“//Id”);

uniqueIdField= Id.InnerText;

// get path of log file.

XmlNode Log = doc.SelectSingleNode(“//LogPath”);

logPath = Log.InnerText;

}

public void Execute(IPluginExecutionContext context)

{

// Create instance of Crm WenService

ICrmService Service = context.CreateCrmService(true);

string entityName = context.PrimaryEntityName;

string orgName = context.OrganizationName;

// Avialable max. number in database.

int maxUniqueNo = ReturnMaxNumber(serverName, entityName, orgName);

CrmNumber num = new CrmNumber();

num.Value = maxUniqueNo;

// Formatted Unique Id With entityname,month,year and unique No.

string formattedId = FormattedUniqueNumber(num.Value, context.PrimaryEntityName);

try

{

// DynamicEntity dyEntity = ReturnDynamicEntityToUpate(context, num);

if (context.InputParameters.Properties.Contains(“Target”))

{

DynamicEntity entity = (DynamicEntity)context.InputParameters.Properties[“Target”];

if (entity != null)

{

string entityId = context.PrimaryEntityName + “id”;

Guid id = (Guid)context.OutputParameters[ParameterName.Id];

DynamicEntity updateUniqueId = new DynamicEntity(context.PrimaryEntityName);

Key key = new Key(id);

KeyProperty keyProp = new KeyProperty(entityId, key);

updateUniqueId.Properties.Add(keyProp);

CrmNumberProperty propUniqueId = new CrmNumberProperty(uniqueNoField, num);

updateUniqueId.Properties.Add(propUniqueId);

StringProperty propFormattedId = new StringProperty(uniqueIdField, formattedId);

updateUniqueId.Properties.Add(propFormattedId);

Service.Update(updateUniqueId);

}

}

}

catch (SoapException SoapEx)

{

WriteLog(entityName, “On Create”,“ERROR”,SoapEx);

}

catch (Exception ex)

{

WriteLog(entityName, “On Create”, “ERROR”, ex.Message);

}

}

private int ReturnMaxNumber(string server, string entity, string orgName)

{

int maxNum = 0;

string dataSource = server;

string tableView = “Filtered” + entity;

string organizatioName = orgName ;

string databaseName = organizatioName + “_MSCRM”;

string sqlQuery = @” SETUSER ‘”+domain +“\\”+ authUser + “‘ select max(new_uniqueid) from “ + tableView ;

WriteLog(entity , “On Create”, “INFO”, “DatabaseServer :” + dataSource);

WriteLog(entity, “On Create”, “INFO”, “Sql Query :” + sqlQuery);

try

{

SqlConnection connection = new SqlConnection(“Data Source=” + dataSource + “;Initial Catalog=” + databaseName + “;Integrated Security=SSPI; Pooling=false”);

SqlCommand cmd = new SqlCommand(sqlQuery, connection);

connection.Open();

if (cmd.ExecuteScalar().ToString() != “”)

{

maxNum = Convert.ToInt32(cmd.ExecuteScalar());

}

connection.Close();

connection.Dispose();

if (maxNum > 0)

{

maxNum++;

}

else

{

maxNum = 1;

}

}

catch (SqlException sqlex)

{

WriteLog(entity, “On Create”, “ERROR”, sqlex);

maxNum =0;

}

return maxNum;

}

private string FormattedUniqueNumber(int num, string entity)

{

string finalUniqueNo=string.Empty;

try

{

DateTime currentDate = DateTime.Now;

string getMonth = currentDate.ToString(“MMM”).ToUpper();

string getYear = currentDate.Year.ToString().Substring(2, 2);

string numWithPad = num.ToString().PadLeft(leftPad, ‘0’);

string entiyName = entity.Substring(0, 2).ToUpper();

finalUniqueNo = entiyName + “/” + getMonth + getYear + “/” + numWithPad;

WriteLog(entity, “On Create”, “INFO”, “Unique ID=” + finalUniqueNo);

return finalUniqueNo;

}

catch (Exception ex)

{

WriteLog(entity, “On Create”, “ERROR”, ex.Message);

return finalUniqueNo;

}

}

private static void WriteLog(string Entity, string EventName, string MessageType, SoapException ex)

{

try

{

string lstrMessage;

TextWriter log = TextWriter.Synchronized(File.AppendText(logPath));

lstrMessage = Entity + ” | “ + EventName + ” | “ + MessageType + ” | “ + System.DateTime.Now.ToString () + ” | “ + ex.Detail.InnerXml;

log.WriteLine(lstrMessage);

log.Close();

}

catch (Exception ex1)

{

throw ex1;

}

}

private static void WriteLog(string Entity, string EventName, string MessageType, string Message)

{

try

{

string lstrMessage;

TextWriter log = TextWriter.Synchronized(File.AppendText(logPath));

lstrMessage = Entity + ” | “ + EventName + ” | “ + MessageType + ” | “ + System.DateTime.Now.ToString () + ” | “ + Message ;

log.WriteLine(lstrMessage );

log.Close();

}

catch (Exception ex)

{

throw ex;

}

}

private static void WriteLog(string Entity, string EventName, string MessageType, SqlException Message)

{

try

{

string lstrMessage;

TextWriter log = TextWriter.Synchronized(File.AppendText(logPath));

lstrMessage = Entity + ” | “ + EventName + ” | “ + MessageType + ” | “ + System.DateTime.Now.ToString() + ” | “ + Message ;

log.WriteLine(lstrMessage);

log.Close();

}

catch (Exception ex)

{

throw ex;

}

}

}

}

Now Register the Plug-In for any entity on PostCreate event.It will give the AutoNuber in this format(OP/DEC08/0000000001) I.e (first two character of Entity name/three character of current month+ two digit of current year/number with padding(10 digit but customizable))

Don’t forget to pass configuration string when registering Plug-In in Secure Configuration Field in this format

<Config><Database>Machine Name of Database</Database><Domain>Domain Name</Domain><AuthUser>Authenticated User of CRM on behalf of that query will be executed</AuthUser><Padding>number of character for padding </Padding><UniqueNumber>new_uniqueid</UniqueNumber><Id>new_id</Id><LogPath> Log path as D:\CRM\Logs\CrmLog.txt</LogPath></Config>