Skip to main content

Notifications

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

  • shahzebkn Profile Picture
    10 on at
    RE: new_datarequest With Id = f099246d-d2f1-eb11-80d4-0022480d79a3 Does Not Exist

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

  • Amit Katariya007 Profile Picture
    10,407 Super User 2025 Season 1 on at
    RE: new_datarequest With Id = f099246d-d2f1-eb11-80d4-0022480d79a3 Does Not Exist

    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

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

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Daivat Vartak (v-9davar) Profile Picture

Daivat Vartak (v-9d... 225 Super User 2025 Season 1

#2
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 106 Most Valuable Professional

#3
Eugen Podkorytov Profile Picture

Eugen Podkorytov 95

Overall leaderboard

Product updates

Dynamics 365 release plans