web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

C# auto number

(0) ShareShare
ReportReport
Posted on by 1,942

Hi all,

I have to create a autonumber plugin for opportunity entity, initially thought of reusing the account number code from CRM SDK samples. Is there any way where we can restrict the random number to a fixed digit; lets say 8 digit. 

Below is my code.

using System;

Using Microsoft.Xrm.Sdk;

namespace Microsoft.Crm.Sdk.Samples
{
public class AccountNumberPlugin: IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
// Obtain the execution context from the service provider.
Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)
serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));

if (context.InputParameters.Contains("Target") &&
context.InputParameters["Target"] is Entity)
{
Entity entity = (Entity)context.InputParameters["Target"];
if (entity.LogicalName == "opportunity") 

{
if (entity.Attributes.Contains("new_opportunityid") == false)
{
Random rndgen = new Random();
entity.Attributes.Add("new_opportunityid", rndgen.Next().ToString());
}
else
{
throw new InvalidPluginExecutionException("The account number can only be set by the system.");
}
}
}
}
}
}

*This post is locked for comments

I have the same question (0)
  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    Try to use rndgen.Next(1, 99999999).

    Also I believe your plugin will fail because of following line:

    entity.Attributes.Add("opportunityid", rndgen.Next().ToString());

    Replace opportunityid with new_opportunityid

  • EmployeeOcta Profile Picture
    6 on at

    using System;

    public class Example

    {

      public static void Main()

      {

         Random rnd = new Random();

         for (int ctr = 1; ctr <= 50; ctr++) {

            Console.Write("{0,3}    ", rnd.Next(1000, 10000));

            if(ctr % 10 == 0) Console.WriteLine();

         }  

      }

    }

    // The example displays output like the following:

    //    9570    8979    5770    1606    3818    4735    8495    7196    7070    2313

    //    5279    6577    5104    5734    4227    3373    7376    6007    8193    5540

    //    7558    3934    3819    7392    1113    7191    6947    4963    9179    7907

    //    3391    6667    7269    1838    7317    1981    5154    7377    3297    5320

    //    9869    8694    2684    4949    2999    3019    2357    5211    9604    2593

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans