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 :
Customer experience | Sales, Customer Insights,...
Unanswered

new_datarequest With Id = f099246d-d2f1-eb11-80d4-0022480d79a3 Does Not Exist

(0) ShareShare
ReportReport
Posted on by 10

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();
}
}

I have the same question (0)
  • Amit Katariya007 Profile Picture
    10,409 Super User 2025 Season 2 on at

    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

  • shahzebkn Profile Picture
    10 on at

    Thanks for your reply, issue was pre-operation in plugin registration, changed to post-operation it's working fine now.

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 > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 83 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 49 Most Valuable Professional

#3
#ManoVerse Profile Picture

#ManoVerse 40

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans