Hi Below is what I tried but every time CRM is creating multiple records with duplicate numbers:
1st Way:
1. Keep counter on a separate entity record.
2. Update the lookup of counter record on the main record.
3. Increment the counter by 1.
4. Update the new number on main record.
5. Stop.
2nd Way:
1. Keep counter on a separate entity record.
2. Update the lookup of counter record on the main record.
3. Update the new number on main record.
4. Increment the counter by 1.
5. Stop.
I have also tried to do it through code and making a false update on counter record so that CRM can apply the lock until the activity is complete. But again, it creates records with duplicate numbers. Can anyone help me out here?
*This post is locked for comments
yes real time workflow will do the trick when you run in organization context
Hi,
If this is Online then you can never prepare a bullet-proof autonumbering in plugins, because you can't be sure if you are using only one application server. The best you can do, and I did not see you doing (or anybody suggesting you to do so), is to put lock in the beginning of your execute method. So you should have something like:
public object _sync = new object() public void Execute { lock(_sync) { //all your code here } }
This would make sure you have unique numbers if there is only one IIS worker for your CRM. You cannot be sure of that in Online deployment, unfortunately.
Pawel
Try the above coded make your changes as needed. register the plugin in pre operation
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Client;
using System.ServiceModel;
namespace autonumber
{
public class Autono : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService _service = serviceFactory.CreateOrganizationService(context.UserId);
string seq = "";
int lastnumber;
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity entity = context.InputParameters["Target"] as Entity;
string records = @"
<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='new_autonumber'>
<attribute name='new_lastnumber1'/>
</entity>
</fetch> ";
EntityCollection result = _service.RetrieveMultiple(new FetchExpression(records));
foreach (var c in result.Entities)
{
string count = (string)c.Attributes["new_lastnumber1"];
lastnumber = Convert.ToInt16(count);
lastnumber = lastnumber + 1;
seq = lastnumber.ToString();
c["new_lastnumber1"] = seq;
_service.Update(c);
}
Entity e = context.InputParameters["Target"] as Entity;
string zoneoptionSetValue = e.FormattedValues["new_zone"].ToString();
string zone = (zoneoptionSetValue.Substring(0, 2)).ToUpper();
string wsceoptionSetValue = e.FormattedValues["new_wsccategory"].ToString();
string wsc = (wsceoptionSetValue.Substring(0, 2)).ToUpper();
int len = seq.Length;
if (len == 1)
{
seq = zone + wsc + 0 + 0 + 0 + 0 + seq;
e["new_accountnumber"] = seq;
_service.Update(e);
}
if (len == 2)
{
seq = zone + wsc + 0 + 0 + 0 + seq;
e["new_accountnumber"] = seq;
_service.Update(e);
}
if (len == 3)
{
seq = zone + wsc + 0 + 0 + seq;
e["new_accountnumber"] = seq;
_service.Update(e);
}
if (len == 4)
{
seq = zone + wsc + 0 + seq;
e["new_accountnumber"] = seq;
_service.Update(e);
}
if (len == 5)
{
seq = zone + wsc + seq;
e["new_accountnumber"] = seq;
_service.Update(e);
}
}
}
}
}
Please try with below solution.
Hope this helps.
Following two solutions can help you:
1. Auto-numbering via workflows: prashantshuklacrm.wordpress.com/.../auto-numbering-through-real-time-workflows-no-customization-no-add-on
2. Auto-Numbering, third party: www.planetxrm.com/microsoft-dynamics-crm-2013-2011-auto-id-generator
This third party solution is tried and tested and does not give a duplicate ever
Hi, thanks for the answer, but I used 3rd party solutions in the first place but because it was generating duplicate ids so I had to shift to my own solution.
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,253 Super User 2024 Season 2
Martin Dráb 230,188 Most Valuable Professional
nmaenpaa 101,156