hi,
I want to auto generate a number in Account entity. On pre-operation plugin, following error occurs. Please help me.
Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=8.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: Unexpected exception from plug-in (Execute): AutoNumber.Class1: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.Detail:
<OrganizationServiceFault xmlns:i="www.w3.org/.../XMLSchema-instance" xmlns="schemas.microsoft.com/.../Contracts">
<ErrorCode>-2147220956</ErrorCode>
<ErrorDetails xmlns:d2p1="schemas.datacontract.org/.../System.Collections.Generic" />
<Message>Unexpected exception from plug-in (Execute): AutoNumber.Class1: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.</Message>
<Timestamp>2016-11-21T07:14:41.9076981Z</Timestamp>
<ExceptionSource i:nil="true" />
<InnerFault i:nil="true" />
<OriginalException i:nil="true" />
<TraceText>
[AutoNumber: AutoNumber.Class1]
[7d00b1bc-80ad-e611-812c-c4346bddc181: AutoNumber.Class1: Create of account]
</TraceText>
</OrganizationServiceFault>
CODE:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System.IO;
namespace AutoNumber
{
public class Class1 : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
ITracingService tracingservice = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = (IOrganizationService)factory.CreateOrganizationService(context.InitiatingUserId);
Entity entity = null;
if(context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
entity = (Entity)context.InputParameters["Target"];
}
else
{
return;
}
QueryExpression query = new QueryExpression("new_autonumber")
{
ColumnSet = new ColumnSet(true),
Criteria =new FilterExpression()
};
query.Criteria.AddCondition(new ConditionExpression("new_entityname", ConditionOperator.Equal, entity.LogicalName));
EntityCollection results = service.RetrieveMultiple(query);
if(results.Entities.Any())
{
foreach (Entity e in results.Entities)
{
string prefix = null;
string suffix = null;
int counter = 0;
int padding = 0;
string counterPadded = null;
string fullReference = string.Empty;
if(e.Attributes.ContainsKey("new_Prefix"))
{
prefix = (string)e["new_Prefix"];
}
if (e.Attributes.ContainsKey("new_Suffix"))
{
suffix = (string)e["new_Suffix"];
}
if (e.Attributes.ContainsKey("new_Counter"))
{
counter = (int)e["new_Counter"];
}
if (e.Attributes.ContainsKey("new_PaddingLength"))
{
padding = (int)e["new_PaddingLength"];
}
if (e.Attributes.ContainsKey("new_PaddingLength"))
{
padding = (int)e["new_PaddingLength"];
}
if(!string.IsNullOrEmpty(prefix))
{
fullReference = fullReference + prefix;
}
if(padding>0 && padding >counter.ToString().Length)
{
counterPadded = counter.ToString("D" + padding);
}
else
{
counterPadded = counter.ToString();
}
fullReference += counterPadded;
if(!string.IsNullOrEmpty(suffix))
{
fullReference += suffix;
}
entity[((string)e["new_FieldName"])] = fullReference;
e["new_Counter"] = ++counter;
service.Update(e);
}
}
else
{
return;
}
}
}
}









