Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

AutoNumbering Plugin

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

guys here is my code trying to make plugins for account numbering

but account number generated randomly not according to the method

code is here : 

protected override void ExecuteCrmPlugin(LocalPluginContext localContext)
{

var trace = localContext.TracingService;


if (localContext != null)
{ //The plugin context
var context = localContext.PluginExecutionContext;
var orgService = new OrganizationServiceContext(localContext.Organizati1 onService);

trace.Trace("Context", context.InputParameters);

if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{

var entityFrom = (Entity)context.InputParameters["Target"];

//Get New Connection
var newconnection = orgService.
CreateQuery<new_newconnectionapplication>().
Where(a => a.new_newconnectionapplicationId == entityFrom.Id).
FirstOrDefault();

// if (!newconnection.new_customerpaid.Value)


//{ return; }
//else

//Get Accounts Under the New Connection
List<Account> listOfAccounts = orgService
.CreateQuery<Account>()
.Where(a => a.new_new_newconnectionapplication_account_NewConnectionApplication.new_newconnectionapplicationId == entityFrom.Id)
.ToList();


//var digitLength = accountNumbering.new_NumberOfDisgits;

// throw new Exception(listOfAccount.Count.ToString());
foreach (var account in listOfAccounts)
{

//Get Number configuration for account
new_numberingconfiguration accountNumbering = orgService
.CreateQuery<new_numberingconfiguration>()
.Where(a => a.new_EntityName == "account")
.FirstOrDefault();

//if (string.IsNullOrWhiteSpace(account.AccountNumber))
// {
var lastRecordNumber = accountNumbering.new_LastRecordNumber;

var accountNumber = lastRecordNumber+1;
account.AccountNumber = accountNumber.ToString();
accountNumbering.new_LastRecordNumber = accountNumber;

orgService.UpdateObject(account);
orgService.UpdateObject(accountNumbering);

orgService.SaveChanges();


//var incValue = lastRecordIncrement(lastnumber.Value);
// item.AccountNumber = incValue + lastnumber.Value.ToString();

// orgService.UpdateObject(item);
// orgService.SaveChanges();
// }
}

}


}
else
throw new Exception("Context is null");


}


// TODO: Implement your custom Plug-in business logic.


//public static int lastRecordIncrement(int record)
//{
// int constantValue = 1;
// constantValue += record;
// return constantValue;
//}

// TODO: Implement your custom Plug-in business logic.
}
}

need ur support guys pleaseee

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: AutoNumbering Plugin

    Hi ,

    please follow the below steps to create a Auto number Generation

    Step 1: Download SDK Latest Version

    Step 2 : Select Sample Code Folder

    Step 3: Select CS Folder  

    Step 4 : Select Plug ins Folder (in this folder example code is given for Auto generation)

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: AutoNumbering Plugin

    refer arunpotti.wordpress.com/.../comment-page-1

  • Suggested answer
    Nithya Gopinath Profile Picture
    Nithya Gopinath 17,076 on at
    RE: AutoNumbering Plugin

    Hi,

    You could configure auto number generation without any code. Refer the link below.

    dynamicscrmcoe.com/auto-number-generation-dynamics-crm-2016

    Hope this helps.

  • Suggested answer
    Guido Preite Profile Picture
    Guido Preite 54,073 Moderator on at
    RE: AutoNumbering Plugin

    use an existing autonumber solution like this one (free and opensource)

    celedonpartners.com/.../celedon-autonumber-solution-for-dynamics-crm

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,494 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,307 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans