Quantcast
Channel: Microsoft Dynamics CRM Forum - Recent Threads
Viewing all articles
Browse latest Browse all 46379

C# auto number

$
0
0

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.");
}
}
}
}
}
}


Viewing all articles
Browse latest Browse all 46379

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>