Answered by:
CRM 2011: custom workflow creates an infinite loop

Question
-
Hi all,
i don't know if my question is exactly related to development section. apologies if it's not,
The scenario is something like this, i am creating a custom workflow which fires when a custom entity's statusreason is changed. then it checks for somevalue (validity of some fields) within the entity. if those values are not/are valid it changes the status reason to a different status reason and exits. i am using xrm service context class and these are the two lines where i am trying to save the entity.
crm.UpdateObject(objEntity); crm.SaveChanges();
now my problem is since the workflow is triggered after the status reason gets changed, the workflow gets triggered again and does the same checks over and over again in a infinite loop. what is the workaround so that the workflow can only gets kicked once.
thanks in advance
im
Thursday, July 21, 2011 7:49 PM
Answers
-
In CRM workflow, you can check the workflow excution context object's Depth property.
If I understand you correctly, the following code snippet should help you prevent from workflow infinite loop:
IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>(); if (context.Depth > 1) return;
Hope this helps.
Cheers,
Daniel Cai | http://danielcai.blogspot.com- Proposed as answer by HIMBAPModerator Friday, July 22, 2011 10:30 AM
- Marked as answer by Ayaz.AhmadModerator Tuesday, July 26, 2011 2:20 AM
Thursday, July 21, 2011 8:58 PM
All replies
-
In CRM workflow, you can check the workflow excution context object's Depth property.
If I understand you correctly, the following code snippet should help you prevent from workflow infinite loop:
IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>(); if (context.Depth > 1) return;
Hope this helps.
Cheers,
Daniel Cai | http://danielcai.blogspot.com- Proposed as answer by HIMBAPModerator Friday, July 22, 2011 10:30 AM
- Marked as answer by Ayaz.AhmadModerator Tuesday, July 26, 2011 2:20 AM
Thursday, July 21, 2011 8:58 PM -
thanks man...much appreciated....Friday, July 22, 2011 9:43 AM
-
Thanks mate, You saved my day.Tuesday, July 26, 2011 12:08 AM