Home > CRM Deployement, CRM Development, CRM Integrations, MSCRM General > Auto Complete Recursive/Waiting Workflows – Microsoft CRM 4

Auto Complete Recursive/Waiting Workflows – Microsoft CRM 4


Microsoft Dynamics CRM provides very powerful workflow designer tools to create workflows for various business scenarios. Waiting workflows are one of them. Very often, it is required to create waiting workflow on dates. So if a date is changed the waiting workflow remains in waiting state and date change executes another waiting workflow. So this aggregation of workflows affects CRM Server performance and application functionality.

Recently I have created a custom workflow activity to kill these waiting workflows in a recursive workflow. So I would like to share my experience with all of you.

I am in a condition to create a waiting workflow to reschedule my activity periodically i.e. recurring appointments, Auto Reorder (Sales Order) etc. My workflow is fired on Create of activity/Order and on change of Next Due date/Next Order Date. So if the user has changed Next Due Date or Next Order Date, the workflows in waiting remains in waiting and also a new Waiting workflow is fired. My aim is to kill the existing workflow as it is invalid now and start a new waiting workflow with updated information.

My workflow name is Auto Reschedule Activity. Here is the description of my workflow:

Start:
                Wait for Next Due Date == Today
                                Reschedule Activity Accordingly
                                Recursive Call: Auto Reschedule Activity
End

What i am looking for is:

Start:
                Check if there is an existing workflow with same Workflow ID, Entity ID and in Waiting State.
                                Then Kill that workflow.
                Wait for Next Due Date == Today
                                Reschedule Activity Accordingly
                                Recursive Call: Auto Reschedule Activity
End

So I started with a custom Activity to fetch workflows in waiting state with same name and regardingobjectid:

I created a query object in my custom workflow activity:

QueryExpression query = new QueryExpression();
query.EntityName = “asyncoperation”;
 query.ColumnSet = cols;

And created 3 conditions as below:

ConditionExpression c1 = new ConditionExpression();
c1.AttributeName = “name”;
c1.Operator = ConditionOperator.Equal;
c1.Values = new Object[] {‘My workflow Name’}; //Workflow
ConditionExpression c2 = new ConditionExpression();
C2.AttributeName = “statecode”;
C2.Operator = ConditionOperator.Equal;
C2.Values = new Object[] {1}; //Waiting
ConditionExpression c3 = new ConditionExpression();
C3.AttributeName = “regadingobjectid”;
C3.Operator = ConditionOperator.Equal;
C3.Values = new Object[] {context. PrimaryEntityId}; //Waiting

So now I have all the waiting workflows regarding my activity. Ideally there will be only one workflow in waiting state all the time. This workflow will be responsible for rescheduling.

So now is the time to kill the outdated workflow. So here is the code for setting that workflow complete/cancelled.

foreach (BusinessEntity be in bec.BusinessEntities)
{
                    asyncoperation async = (asyncoperation)be;
                    async.statecode = new AsyncOperationStateInfo();
                    async.statecode.Value = AsyncOperationState.Completed;
                    service.Update(async);
}

You can improve this workflow utility to add lot more functionality to workflow designer.

About these ads
  1. January 5, 2010 at 8:21 pm | #1

    What a wonderful blog! Please continue this great work I will be sure to check back regularly…

  2. January 7, 2010 at 5:43 pm | #2

    I simply wanted to add a comment here to say thanks for you very nice ideas. Blogs are troublesome to run and time consuming therefore I appreciate when I see well written material. Your time isn’t going to waste with your posts. Thanks so much and carry on You’ll defintely reach your goals! have a great day!

  3. January 9, 2010 at 6:50 am | #3

    Thank you. Very educational posting.

  4. January 13, 2010 at 5:27 am | #4

    Many thanks for the information. I liked it. You have a very nice site.

  5. January 16, 2010 at 10:01 pm | #5

    Nice post!

  6. January 17, 2010 at 10:42 pm | #6

    Hi, nice post. I just found this blog, but I will definitely bookmark it. Keep us updated.

  7. rajwindar
    January 19, 2010 at 9:56 am | #7

    good work. Realy helpful

  8. January 23, 2010 at 1:49 am | #8

    Please don’t take this the wrong way. I think your overall ideas are fine but you might want to put a little more thought into your next posts. I say this becuase it seems like your writing style has gone downhill a bit as opposed to your previous posts. – C.

  9. January 25, 2010 at 4:39 am | #9

    Outstanding posting. I thank you for posting it. Keep up the fine work.

  10. ElieFadous
    January 25, 2010 at 11:53 am | #10

    hello mr. ayaz i’m really interrested in this post
    but it’s giving me errors
    i wish if i can get the fully typed .cs file so i can test this custom workflow

    Thank you
    Elie,

  11. ElieFadous
    January 25, 2010 at 11:54 am | #11

    hello mr. ayaz i’m really interrested in this post
    but it’s giving me errors
    i wish if i can get the fully typed .cs file so i can test this custom workflow

    Thank you
    Elie,

  12. January 26, 2010 at 3:53 am | #12

    Hi, just discovered this blog but I have to admit that it seems nice. I totally agree with you. Have a nice day, keep up the nice work and I’ll definitely keep checking your blog.

  13. January 27, 2010 at 4:30 am | #13

    What exactly is the error?

    It pretty simple Custom Workflow Activity.

  14. eliefadous
    January 27, 2010 at 6:17 am | #14

    Hey,
    for exemple, about this line:

    query.ColumnSet = cols;

    what do we really mean about ‘cols’ ?
    any way
    that’s my e-mail elie_fadous@bemea.com
    i’ll be very thankful if you can provide me with a sample .cs file

    Thank you,
    Elie

  15. January 27, 2010 at 1:07 pm | #15

    Declare a variable:

    ColumnSet cols = new AllColumns();

    this will fetch all columns associated with entity in query expression.

    I cannt share the code due to contract restrictions.

    Hope this helps,

  16. eliefadous
    January 27, 2010 at 1:23 pm | #16

    thank you very much

  17. January 30, 2010 at 5:53 pm | #17

    You have a very nice site! Very informative and fun to read. I will be returning soon to check out what’s new.

  18. February 7, 2010 at 6:01 pm | #18

    Ive been reading a few posts and really and enjoy your writing. Im just starting up my own blog and only hope that I can write as well and give the reader so much insight.

  19. bharath
    August 27, 2010 at 12:10 pm | #19

    i’m using timeout step in workflow. but this workflow keeps hitting the server thus causing perfomance issues. is there a way to limit the number of times this workflow hits the server.can someone help please

  20. Amar
    April 29, 2011 at 2:15 pm | #20

    Hi,
    This is a very nice code but for me it gets stuck at one place and that is retrieving of the asyncoperations. On the retrievemultiple it gives me the error that “Server was unable to process request.”. I am getting the error on the following line:

    BusinessEntityCollection retrieved1 = crmService.RetrieveMultiple(query);

    Can you please tell me what may be the reason? Following is the part of the code where I am retrieving the asyncoperation:

    ConditionExpression c1 = new ConditionExpression();
    c1.AttributeName = “name”;
    c1.Operator = ConditionOperator.Equal;
    c1.Values = new string[] {“DRC ACE”}; //Workflow
    ConditionExpression c2 = new ConditionExpression();
    c2.AttributeName = “statecode”;
    c2.Operator = ConditionOperator.Equal;
    c2.Values = new Object[] {1}; //Waiting
    ConditionExpression c3 = new ConditionExpression();
    c3.AttributeName = “regadingobjectid”;
    c3.Operator = ConditionOperator.Equal;
    c3.Values = new Object[] {ctx.PrimaryEntityId}; //Waiting
    FilterExpression filter = new FilterExpression();
    filter.FilterOperator = LogicalOperator.And;
    filter.AddCondition(c1);
    filter.AddCondition(c2);
    filter.AddCondition(c3);
    QueryExpression query = new QueryExpression();
    query.EntityName = EntityName.asyncoperation.ToString();
    query.ColumnSet = new AllColumns();
    query.Criteria = filter;
    // Execute the request.
    BusinessEntityCollection retrieved1 = crmService.RetrieveMultiple(query);
    //Finally kill the workflows.
    foreach (BusinessEntity be in retrieved1.BusinessEntities)
    {
    asyncoperation async = (asyncoperation)be;
    async.statecode = new AsyncOperationStateInfo();
    async.statecode.Value = AsyncOperationState.Completed;
    crmService.Update(async);

    }

  21. April 29, 2011 at 3:50 pm | #21

    Hi Amar,
    This error is generic. Can you write down the detail message in SOAPException Or use trace to capture the detail error.

  22. Richard
    January 4, 2012 at 7:15 am | #22

    Hi Ayaz, its a very nice article,, but im using crm 2011, can you provice more information how to kill outstanding WF in crm 2011, since it doesnt have “BusinessEntityCollection”.

    i really appreciate your helps.
    Thanks

  23. crmbeginning
    October 3, 2013 at 8:21 pm | #23

    Hi,
    1. I am facing the some issue unable to send mail via email router,
    2. Workflows are not running in system job system evnt is showing as workflow expansion task and status waiting. And the error code which it shows is for AsyncNetwork Error. Please help me as my boss is really pissed of now its been 2 weeks now i am facing this issue. It started after the admin pwd was changed.

    I have started the Asyn Service many times and iisreset also. Server and Host name is also same in ‘AsyncSdkRootdomain’, ‘ADSdkRootdomain’, ‘ADWebApplicationRootdomain’. Guide me guys what should i do now.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 58 other followers

%d bloggers like this: