AutoNumber Plug-In for Custom Entity In CRM 4.0

Posted: July 28, 2010 in Microsoft .NET, Microsoft Dynamics CRM
Tags: , ,

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
Comments
  1. Ann says:

    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. fitness says:

    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. Wow! What an idea ! What a concept ! Beautiful
    . Awesome. The info has been so very much appreciated!

  4. Venetta says:

    It’s very simple to find out any topic on web as compared to books, as I found this paragraph at this website.

  5. Je souffle mes quarante-huit bougies dans un mois !
    Je me nomme Mirabelle.
    je fais un stage de conductrice de travaux . Je suis plutôt d’un caractère réservé.

  6. Voyage says:

    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.

  7. Generally I don’t post on blogs, but I’d prefer to say that this location really
    forced me to do so! pleasant post.

  8. Overall a user friendly website, some good points!
    It is a well written article, I’ll try to read others. With Respect. Regards!

  9. samuel sandeep says:

    Nice Article,thanks for sharing this information.Looking forward for more posts this.

    Dynamics CRM Developers

  10. 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

  11. 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!

  12. 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.
    \

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