AutoNumber Plug-In for Custom Entity In CRM 4.0

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

}

Author: Arvind Singh

Microsoft Dynamics CRM Consultant

18 thoughts on “AutoNumber Plug-In for Custom Entity In CRM 4.0”

  1. My spouse and I stumbled over here from a different page and thought I might as well check
    things out. I like what I see so now i am following you.
    Look forward to looking over your web page for a second time.

  2. Thank you for any other great post. The place else may anyone get that kind of info in such an
    ideal approach of writing? I have a presentation subsequent week,
    and I am on the search for such information.

  3. Mes parents m’ont appellée Marie et j’aime beaucoup ce prénom.

    J’ai vingt-huit ans Et tant pis si ça ne se dit pas !
    Mon travail responsable de maintenance . Il est dit régulièrement que je suis drole.

  4. Howdy just wanted to give you a quick heads up.

    The text in your content seem to be running off the screen in Opera.
    I’m not sure if this is a format issue or something to do with internet browser compatibility but I figured I’d post to let you know.
    The design look great though! Hope you get the problem resolved soon.
    Kudos

  5. Heya, I am blogger as you, and wondered, since Googles Panda Update, my website gets not
    anywhere found any more 😦 However when i searched for AutoNumber
    Plug-In for Custom Entity In CRM 4.0 | Arvind’s CRM Blog your site was around the first page. Want to reveal any tips with me? I privately use only DuckDuckGo as search engine given that Google screwed virtually all up. DuckDuckGo is a lot like Google before they decided to favor only the big monster sites!

  6. Hi! Someone in my Facebook group shared this website with us so I came to check it out.
    I’m definitely enjoying the information. I’m bookmarking
    and will be tweeting this to my followers! Fantastic blog and
    superb design and style.
    \

  7. After the massive financial and critical success of Virtua Fighter 2, Sega’s
    AM2 division lightened things up with a whacky fighter using the
    company’s trademark three-button control scheme.
    And if this is the case, one will project these associations onto others
    and interpret their behaviour in such a way.
    Battles are fast-paced and strategic in nature with thousands of soldiers at war at once.

  8. I do not even know how I ended up here, but I thought this post was good.
    I do not know who you are but certainly you’re going to a famous blogger if you aren’t already 😉 Cheers!

  9. Have you ever thought about creating an ebook or guest authoring on other sites?
    I have a blog based on the same ideas you discuss and would really like to have you
    share some stories/information. I know my visitors
    would value your work. If you are even remotely interested, feel free to
    send me an e-mail.

  10. Wonderful items from you, man. I have remember your stuff previous to and you’re just extremely great.
    I actually like what you have bought here, really like what you are saying and the way by which you say it.

    You make it entertaining and you continue to take care of to stay it
    sensible. I cant wait to read far more from you.
    This is really a wonderful site.

Leave a Reply

Please log in using one of these methods to post your comment:

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