Quantcast
Channel: Microsoft Dynamics CRM Forum - Recent Threads
Viewing all articles
Browse latest Browse all 46379

Auto Increment with bulk created records - duplicate numbers

$
0
0

I have a record called Dream Invoice that needs an auto-incremented number.

These records are created in batches and I'm finding that the number is being duplicated 2 or 3 times. I have written the following plugin that is detecting the greatest existing number and increment it by one to place in the relevant field.

int max = 9999;

string referenceQuery = @"<fetch distinct='false' mapping='logical' aggregate='true'>
    <entity name='cloud20_dreaminvoice'>
        <attribute name='cloud20_proformanumber' alias='refmax' aggregate='max' />
            <filter type='and'>
                <condition attribute='cloud20_proformanumber' operator='not-null' />
            </filter>
        </entity>
    </fetch>";
tracer.Trace(referenceQuery);
EntityCollection mems = service.RetrieveMultiple(new FetchExpression(referenceQuery));
tracer.Trace("Entities Received");

if (mems.Entities.Count > 0)
    foreach (var c in mems.Entities)
    {
        if (c.Contains("refmax") && ((AliasedValue)c["refmax"]).Value != null)
        {
            tracer.Trace("assign value");
            max = (int)((AliasedValue)c["refmax"]).Value;
        }
    }

max = max + 1;
tracer.Trace("max: " + max.ToString());
//if (context.MessageName == "Create") {

/*                    
Entity inv = new Entity("cloud20_dreaminvoice");
inv.Id = targetEntity.Id;
inv["cloud20_proformanumber"] = max;
inv["cloud20_proformanumberstring"] = "PF" + max.ToString();
service.Update(inv);*/

targetEntity["cloud20_proformanumber"] = max;
targetEntity["cloud20_proformanumberstring"] = "PF" + max.ToString();


This is currently running in synchronous pre stage. I had run it in sync post and async post using the code commented out but get the same result.

This is replicated in both CRM online and on premise 8.1

Thanks


Viewing all articles
Browse latest Browse all 46379

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>