CRM 2016 writing plugin for autonumber
one entity is auto number
another entity is data request.
public void Execute(IServiceProvider serviceProvider)
{
System.Threading.Mutex mtx = null;
String debugText = "Inside Execute \n";
try
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
if (context.IsInTransaction)
{
lock (SyncLock)
{
Entity entity = (Entity)context.InputParameters["Target"];
debugText = debugText + "Inside if loop - targe \n";
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
string fetchXml = @"<fetch mapping='logical'>
<entity name='new_autonumber'><all-attributes/>
<filter type=""and"">
<condition attribute=""new_name"" operator=""eq"" value='" + entity.LogicalName + "'" + " /></filter></entity></fetch>";
string mutextName = string.Format("{0}{1}", entity.LogicalName, "Autonumber");
debugText = debugText + "mutextName \n" + mutextName;
mtx = new System.Threading.Mutex(false, mutextName);
mtx.WaitOne();
debugText = debugText + "entity logical name = " + entity.LogicalName + "\n";
debugText = debugText + "Before executing fetch \n";
EntityCollection result = service.RetrieveMultiple(new FetchExpression(fetchXml));
debugText = debugText + "After executing fetch \n";
string nextIncrementNumber = string.Empty;
if (result.Entities.Count == 1)
{
debugText = debugText + "inside the if loop - enitities \n";
//retrieve the counter
Entity autoNumber = result.Entities[0];
if (!autoNumber.Attributes.Contains("new_counter"))
throw new InvalidPluginExecutionException("dcas_counter must contain a value");
if (!autoNumber.Attributes.Contains("new_incrementunit"))
throw new InvalidPluginExecutionException("dcas_increamentunit must contain a value");
debugText = debugText + "get values \n";
int counter = Int32.Parse(autoNumber.Attributes["new_counter"].ToString());
int incrementUnit = Int32.Parse(autoNumber.Attributes["new_incrementunit"].ToString());
string prefix = autoNumber.Attributes.Contains("new_prefix") ? autoNumber.Attributes["new_prefix"].ToString() : string.Empty;
string prefixSeparator = autoNumber.Attributes.Contains("new_prefixseparator") ? autoNumber.Attributes["new_prefixseparator"].ToString() : string.Empty;
string suffix = autoNumber.Attributes.Contains("new_suffix") ? autoNumber.Attributes["new_suffix"].ToString() : string.Empty;
string suffixseparator = autoNumber.Attributes.Contains("new_prefixseparator") ? autoNumber.Attributes["new_prefixseparator"].ToString() : string.Empty;
string numberFormatter = autoNumber.Attributes.Contains("new_numberformat") ? autoNumber.Attributes["new_numberformat"].ToString() : string.Empty;
string fieldToUpdate;
if (autoNumber.Attributes.Contains("new_autonumberingfield"))
fieldToUpdate = autoNumber.Attributes["new_autonumberingfield"].ToString();
else
throw new InvalidPluginExecutionException("new_autonumberingfield should not be emplty");
debugText = debugText + "before calling buld number \n";
nextIncrementNumber = BuildAutoNumber(prefix, prefixSeparator, suffix, suffixseparator, counter, incrementUnit, numberFormatter);
debugText = debugText + "after calling build number \n" + nextIncrementNumber;
//increment the autonumber entity
//and update it to record the counter
debugText = debugText + "before calling update (autonumber)\n";
autoNumber.Attributes["new_counter"] = counter + incrementUnit;
service.Update(autoNumber);
debugText = debugText + "new Increament number updated\n";
debugText = debugText + "before calling update (entity) \n";
entity.Attributes[fieldToUpdate] = nextIncrementNumber;
service.Update(entity);
}
}
}
}
}
catch (Exception ex)
{
if (mtx != null)
{
mtx.ReleaseMutex();
mtx = null;
}
// throw new InvalidPluginExecutionException("An error occured in Autonumber plugin", ex);
throw new InvalidPluginExecutionException("An error occured in Autonumber plugin" + ex + "\n" + debugText);
}
finally
{
debugText = null;
if (mtx != null)
mtx.ReleaseMutex();
}
}
Thanks for your reply, issue was pre-operation in plugin registration, changed to post-operation it's working fine now.
Hello,
There is issue with below Fetch XML condition Syntax.
<filter type=""and"">
<condition attribute=""new_name"" operator=""eq"" value='" + entity.LogicalName + "'" + " /></filter>
Can you refer below thread. and change above filer conditions and check again.
community.dynamics.com/.../add-a-dynamic-variable-value-for-a-attribute-in-fetchxml
Thank you,
Amit Katariya
Daivat Vartak (v-9d...
225
Super User 2025 Season 1
Muhammad Shahzad Sh...
106
Most Valuable Professional
Eugen Podkorytov
95