Reputation: 2170
What could possibly cause a plug-in to execute 4 times from a single event?
The plug-in has been registered on-retrieve of Case and fires 4 times for a single Retrieve.
protected void ExecutePostCaseRetrieve(LocalPluginContext localContext)
{
if (localContext == null)
{
throw new ArgumentNullException("localContext");
}
// TODO: Implement your custom Plug-in business logic.
IOrganizationService service = localContext.OrganizationService;
QueryByAttribute query = new QueryByAttribute("resource");
query.Attributes.AddRange("name");
query.Values.AddRange("Employee");
EntityCollection result = service.RetrieveMultiple(query);
Guid employee = result.Entities[0].Id;
QueryScheduleRequest scheduleRequest = new QueryScheduleRequest
{
ResourceId = employee,
Start = DateTime.Now.ToLocalTime(),
End = DateTime.Now.ToLocalTime().AddMonths(1),
TimeCodes = new TimeCode[] { TimeCode.Available }
};
QueryScheduleResponse schedule = (QueryScheduleResponse)service.Execute(scheduleRequest);
DateTime today = DateTime.Now.ToLocalTime();
if (today.Date.Equals(schedule.TimeInfos[0].Start.Value.Date)) // today is a working day
{
service.Create(new Lead { FirstName = today.Hour.ToString() });
}
else // just escalate from beginning of next working day
service.Create(new Lead { FirstName = schedule.TimeInfos[0].Start.Value.ToLocalTime().ToString(), LastName=schedule.TimeInfos[0].End.Value.ToLocalTime().ToString() });
I have tried synchronous and asynchronous, the same thing happens.
Upvotes: 1
Views: 284
Reputation: 279
I know this post is a year old, but I ran into the same issue with another plugin triggering my first plugin. The crm service actually has a depth property you can use to figure out how many actions have triggered before this one.
putting something like this before the code to be executed can solve this problem.
if (context.depth > 1 ){
return;
}
Upvotes: 0
Reputation: 39118
Except from a bug being to blame (which I suspect is not the case), the only reasonable way to quad-fire a plug in is that you have a call to it from itself. The problem with this answer is that such a setup usually results in an eternal recursion and a poof on the screen.
Speculating regarding the cause, I realize that there are four re-calls in your issue and there are four basic operations of CRUD. May that be a dependency?
EDIT:
As my MVP always says. In such situations, find out where it happens before you try to explain why it happens. In some cases, he's right.
So, this is what I'd do in this case.
The term *magic*will in the following list mean this set of operations: remove all instances of Case and Lead, create a single one of Case and see how many instance of Lead there are.
Service.Create(new Entity("case"));
). Do the magic.You can try that for a totally new installation, too, to guarantee that there's no other software that collides. There's a free 30-day trial. I've got about 10 different trials at any given time. :)
Upvotes: 1