Resetting Workflows and Jumping between Workflows

2 minute read time.
This is a technique for moving records from one workflow to another.

In fact there are 2 ideas here.

1) The first is the concept of resetting a workflow. Basically this is when you have started to progress along a workflow and then realise that you should have followed a different path. A "reset" workflow rule would allow to then start the workflow process over again. An example of this maybe when you have two valid workflows associated with the case entity. One is for Returns Authorisation and the other is for Customer Service Requests. It might be that you start a process of logging a return but then realise that the customer was only had a simple problem that could be solved easily.

You can then start your second workflow using transition rules that are attached to the entry state.

2) The second example is when you have started one path and need to switch to directly into another workflow and join the new workflow at a particular stage. You might have started the "Customer Service Request" workflow and then realise that the product needs to be returned so you want to bring the case record directly into the "Return Authorised" stage of the "Returns" workflow.

Both types of jumping workflow will preserve any of the old tracking information held in the progress tables and that are displayed on the tracking screen. The history of what has happened to the reocrd is therefore preserved.

Both types of jump are manged using an ASP page. You can call such an ASP page with a Global or Transitional rule.

To Reset a Workflow

Using the Example of a Case Workflow the code in the ASP page will blank out or nullify the records workflowid field.

E.g.

var intRecordId = CRM.GetContextInfo("case","case_caseid");
var myRecord = CRM.FindRecord("case","case_caseid="+intRecordId);
myRecord.case_workflowid = null;
myRecord.SaveChanges();
Response.Redirect(CRM.URL(281));

To Jump Workflows

Jumping workflows is more complex. It requires you to create a new workflowinstance record and then link that record to the workflowstate information and the record being managed.
var intRecordId = CRM.GetContextInfo("case","case_caseid");
var myRecord = CRM.FindRecord("case","case_caseid="+intRecordId);

var wkinRecord = CRM.CreateRecord("workflowinstance");
wkinRecord.wkin_workflowid = 7; //this is the workflow id of the alternate case workflow;
wkinRecord.wkin_currententityid = 3; //this is the id of the case table found in the custom_table meta data table
wkinRecord.wkin_currentrecordid = intRecordId; //the ID of the Case record being workflowed;
wkinRecord.wkin_currentstateid = 27; //this is the id value of the state found in the workflowstate table.
wkinRecord.SaveChanges();

myRecord.case_workflowid = wkinRecord.wkin_instanceid; //this sets the Case as belonging to the new workflow.
myRecord.SaveChanges();
Response.Redirect(CRM.URL(281));


Note:

There are of course some caveats. This will not undo any changes that you have made to the actual data. It will not undo tasks that have been done, it can't go back in time and make telephone calls not happen, mail merges not be sent, retrieve sent emails from the internet or otherwise change the timelines. You need a tardis for that.
  • Ok i found the solution in this thread "Update Oppo_WorkflowId with WkIn_InstanceId in Table Level Script" community.sagecrm.com/.../17362.aspx

    It has to be:

    Values("oppo_workflowid")=wkInRecord.wkin_instanceid;

    then it is working like i expect it :-)!

  • I got another question, when i have this line: myRecord.oppo_workflowid = wkinRecord.wkin_instanceid;

    (i changed case to opportunity) in 'function InsertRecord()' tablescript, i get the error that the object does not provide the method or property?! On myRecord.oppo_workflowid = 9999 it brings the same error. What could be the problem?

  • Ok thats how i thought it is working, so i need a new entry in workflowinstance for my own opportunity workflow when i convert a lead to an opportunity. The primary rule is ignored by the convert function.

  • The workflowinstance record is the join between the Opportunity/Case/Company being worked and the definition of the workflow. The workflowinstance record is created when the entity is attached to the workflow. Typically this could be as a result of a primary rule action as for Opportunity/Case/Solutuions/Leads but it can also be through a transitional rule - see the default Oppo workflow and consider how the Leads that have been converted to an Oppo then need to be attached to a workflow.

    As workflow progresses the workflowinstance table is then updated with the new state information.

  • Hi,

    the sagecrm logic doesn't insert new records in workflowinstance when the workflowfunction is called by the user in the entity. It inserts records only history entries and updates the wkinstance entry (version 7.1.d.1).

    Is this behavior correct? So i have to update the instance entry by default and i have to insert a new record into the workflowhistory table to have the correct default behavior.

    Greetings