Controlling workflow state in ASP

1 minute read time.
This is some code from an asp page designed to be used in a cross entity workflow. The original page forms part of an Opportunity Workflow. But the actual code here was in an ASP page is called from a rule associated with the communication entity.

The button would appear on the communication screen and when pressed it would close the communication, then change the oppo_stage and update the workflow state for the instance.

If we are creating a new record in code then we can set the initial value of the workflow by using:

var myRecord = CRM.CreateRecord("TableName");
myRecord.SetWorkflowInfo('vWorkflowName', 'vWorkflowState')

But this is not available for existing records so we must control the progress through the workflow ourself.

Below is the extract showing how the workflow id and the workflow instance as used to allow the setting of the workflow state.

var strKeyID= "key6";
var Id = new String(Request.Querystring(strKeyID));
var intRecordId = 0;
if (Id.indexOf(",") > 0)
{
var Idarr = Id.split(",");
intRecordId = Idarr[0];
}
else if (Id != "")
{
intRecordId = Id;
}

//Retrieve Record
var myRecord = CRM.FindRecord("communication","comm_communicationid="+intRecordId);
myRecord.comm_status = 'Complete';
myRecord.SaveChanges();
////////////////////////////////////////

var myOppoRecord = CRM.FindRecord("opportunity","oppo_opportunityid="+CRM.GetContextInfo("opportunity","oppo_opportunityid"));
myOppoRecord.oppo_stage = 'Qualified';
myOppoRecord.SaveChanges();

//////////////////////////////////////////
//Please note that the wkin_currentstateid has to be checked with
//workflow definitions

var workflowinstanceRecord = CRM.FindRecord
("workflowinstance","wkin_instanceid="+
myOppoRecord.oppo_workflowid);
workflowinstanceRecord.wkin_currentstateid = 42;
workflowinstanceRecord.SaveChanges();

////////////////////////////////////////

Response.Redirect(CRM.URL(260));