Microsoft Dynamics CRM – Advanced Workflows


Today’s guest blogger is CRM MVP Ayaz Ahmad who is a CRM Solution Architect with more than seven years in software development and managing large projects and technical teams. Ayaz blogs at

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 created a custom workflow activity to stop 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 such as in 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 stop 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:


    Wait for Next Due Date == Today

         Reschedule Activity Accordingly

         Recursive Call: Auto Reschedule Activity


What i am looking for is:


     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


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

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.

Now is the time to kill the outdated workflow. Use code like this for setting that workflow to complete/cancelled.

foreach (BusinessEntity be in bec.BusinessEntities)


asyncoperation async = (asyncoperation)be;

async.statecode = new AsyncOperationStateInfo();

async.statecode.Value = AsyncOperationState.Completed;



Note that you can improve this workflow utility to add lot more functionality to the workflow designer. Workflow is also described in the Microsoft Dynamics CRM SDK.


Ayaz Ahmad

Comments (10)

  1. The url for workflow customization tutorial at Microsoft website is actually pointing to the old v3.

    Do you know the tutorial for v4?

  2. 365blog says:

    Okay the link at "workflow" is fixed.

  3. Ercan TOP says:

    Thanks mate it is very helpfull solution…

  4. Ercan TOP says:

    Thanks mate it is very helpfull solution…

  5. ian says:


    wonder if i could get your assistance/ point me in the right direction.

    For the data retrieval i’ve added the following code:


    FilterExpression filter = new FilterExpression();




    query.Criteria = filter;

    RetrieveMultipleRequest retrieve = new RetrieveMultipleRequest();

    retrieve.Query = query;

    retrieve.ReturnDynamicEntities = true;

    RetrieveMultipleResponse retrieved = (RetrieveMultipleResponse)service.Execute(retrieve);


    When the code hits the line:

    RetrieveMultipleResponse retrieved = (RetrieveMultipleResponse)service.Execute(retrieve);

    An error is generated:

    exception: "Server was unable to process request"

    InnerText: "n  0x80040216n  An unexpected error occurred.n  Platformn"

  6. ayazahmad says:

    show me your complete code. there must be some issue with conditions.

  7. Nilesh says:


    How can i create recurrance system Job,

    i wants to create recurance system job (Asyncjob)

    how can i create

    it showing an error org name privilage


  8. Tribek88 says:


    Nice post.  Can you also share your code for your recursive call to the Auto-Reschedule Activity?

    Many thanks and more power.

  9. Sarbashish Bhattacharjee says:

    It is possible to create reccuring appointments in MS CRM 4.0 with form customization as well.

    I have done one implementation for creating reccuring service activity. I have added two new fields in the Service Activity entity.

    Please read the detailed implementation below:

    Entity Name: serviceappointment

    Add two new Fields

    New Field 1


    Field Label: Recurring Frequency

    Field Name: new_recurringactivity

    Requirement Level: No Constraint

    Field TYpe: picklist

    Field Values:

    1 – Daily

    2 – Weekly

    3 – Fortnightly

    4 – Monthly

    5 – Bi-Monthly

    6 – Quarterly

    7 – Half Yearly

    8 – Yearly

    New Field 2


    Field Label: Recurring End Date

    Field Name: new_recurringenddate

    Field Type: datetime

    Field Format: Date Only

    Requirement Level: No Constraint

    Purpose of the new fields


    Field Name: new_recurringactivity

    Purpose: This field will accept recurring frequency from the user.

    Field Name: new_recurringenddate

    Purpose: This field will accept the date from the user. It will not create any activity beyond the date. Otherwise it will go in a endless loop.



    // Add this code snippet to the OnLoad event of the Service Activity Form

    // Lock the recurring Service Activiy fields once activities have been created

    if (crmForm.all.new_recurringactivity.DataValue)


    crmForm.all.new_recurringactivity.disabled = true;

    crmForm.all.new_recurringenddate.disabled = true;





    // Add this code snippet to the OnSave event of the Service Activity

    // Function to format a date to the UTC format required by web services

    function DateToUTCFormat(inputDate)


    var date = inputDate.getDate();

    var month = inputDate.getMonth()+1;

    var year = inputDate.getYear();

    var hours = inputDate.getHours();

    var minutes = inputDate.getMinutes();

    var ampm = " AM";

    if (hours > 11)


       ampm = " PM";

       hours = hours – 12;


    if (hours == 0)

       {hours = 12;}

    if (minutes < 10)

       {var time = hours.toString() + ":0" + minutes.toString() + ":00" + ampm;}


       {var time = hours.toString() + ":" + minutes.toString() + ":00" + ampm;}

    var UTCDate = month.toString() + "/" + date.toString() + "/" + year.toString() + " " + time;

    return UTCDate;


    if (crmForm.all.new_recurringactivity.disabled == false && crmForm.all.new_recurringactivity.DataValue && crmForm.all.new_recurringactivity.DataValue)


    var interval = 0;

    switch (parseInt(crmForm.all.new_recurringactivity.DataValue))


    case 1:   // Daily (Please check the value you are getting from the CRM Form)

    interval = 1;


    case 2: // weekly

    interval = 7;


    case 3: // Fortnightly

    interval = 14;


    case 4: // Monthly

    interval = 30;


    case 5: // Bi-Monthly

    interval = 60;


    case 6: // Quarterly

    interval = 90;


    case 7: // Half Yearly

    interval = 180;


    case 8: // Yearly

    interval = 365;



    var recurringEnd = crmForm.all.new_recurringenddate.DataValue;


    var activityStart = crmForm.all.scheduledstart.DataValue;

    var activityEnd = crmForm.all.scheduledend.DataValue;

    // Set the first reccuring appointment as per the recurring frequency opted by the user



    // Prepare values for the new Service Activity

    var subject = crmForm.all.subject.DataValue;

    var regardingId = crmForm.all.regardingobjectid.DataValue[0].id;

    var customerId = crmForm.all.customers.DataValue[0].id;

    var serviceid = crmForm.all.serviceid.DataValue[0].id;

    var resourceId = crmForm.all.resources.DataValue[0].id;

    var ownerId = crmForm.all.ownerid.DataValue[0].id;

    var new_recurringactivity = crmForm.all.new_recurringactivity.DataValue;

    // Loop for the number of recurring Service Activities

    while (activityStart < recurringEnd)


       // Prepare the SOAP message.

       var startUTC = DateToUTCFormat(activityStart);

       var endUTC = DateToUTCFormat(activityEnd);

      // alert("startUTC: "+startUTC);

       var recurringEndUTC = DateToUTCFormat(recurringEnd);

       var authenticationHeader = GenerateAuthenticationHeader();

       var xml = "<?xml version='1.0' encoding='utf-8'?>" +

       "<soap:Envelope xmlns:soap='…/&

       " xmlns:xsi='…/XMLSchema-instance&amp;

       " xmlns:xsd='…/XMLSchema&amp;



       "<Create xmlns='…/WebServices&

       "<entity xsi:type='serviceappointment'>"+


























       // Prepare the xmlHttpObject and send the request.

       var xHReq = new ActiveXObject("Msxml2.XMLHTTP");

       xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);


       xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8");

       xHReq.setRequestHeader("Content-Length", xml.length);


       // Capture the result

       var resultXml = xHReq.responseXML;

       // Check for errors.

       var errorCount = resultXml.selectNodes('//error').length;

       if (errorCount != 0)


            var msg = resultXml.selectSingleNode('//description').nodeTypedValue;



       // Notify user of Service Activity creation



          // alert("Service Activity created on " + activityStart);


       // Increment the next Service Actvity to be created by as per the recurring frequency opted by the user




    } // Code block Ends

    Some More Explanation


    Please note in the above code snippet I am taking resources as user. So for that I have used in the code snippet








    If you are considering equipments as your resources then use


    Also note that if you want to accept multiple resources then you need to change the value in the "participationtypemask" node.

    Since I am expecting 1 resource so I have kept the value as 1.

    Let me know if this helps.


    Sarbashish Bhattacharjee

Skip to main content