Hello together,
I wrote a cwa which creates an autonumber for an entity x. the CWA runs in a syncron workflow when an account is created or on demand for initial filling of the field.
i bulk updated around 300 accounts and it worked perfectly. the other day I updated 4 accounts and suddenly the number didnt increase and all got the same number.
I dont think the code is the problem. I belief the instances of the cwa where running simultaniously and accessing the same variable before it was increased. But I dont know how I can avoid it. Here is my Code anyway:
...
protected override void Execute(CodeActivityContext executionContext) {
IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
IOrganizationServiceFactory serviceFactory =
executionContext.GetExtension<IOrganizationServiceFactory>();
IOrganizationService service =
serviceFactory.CreateOrganizationService(context.UserId);
QueryExpression query = new QueryExpression("vci_autonumbering");
query.ColumnSet = new ColumnSet(true);
query.Criteria.Conditions.Add(new ConditionExpression("vci_entity", ConditionOperator.Equal, context.PrimaryEntityName));
EntityCollection results = service.RetrieveMultiple(query);
foreach (Entity e in results.Entities)
{
//Update autonumber
var autonumber_guid = e.Id;
var prefix = e.Attributes["vci_prefix"];
var fieldname = e.Attributes["vci_fieldname"].ToString();
String number = e.Attributes["vci_number"].ToString();
Int64 number1 = Convert.ToInt64(number);
number1 = number1 + 1;
var update = new Entity(e.LogicalName, e.Id);
update.Attributes.Add("vci_number", number1.ToString());
service.Update(update);
//update entity
var updateentity = new Entity(context.PrimaryEntityName, context.PrimaryEntityId);
updateentity.Attributes.Add(fieldname, prefix+number1.ToString());
service.Update(updateentity);
}
}
Any Idea why the workflow pulled the same number and any Idea how I can avoid it?
Greetings and thx
McDauly