K2 Very Critical Fundamental issue : Datafields are not getting updated from code

  • 13 August 2014
  • 1 reply
  • 0 views

Badge +1

Hi,

I am using 4.6.7/8

I have assigned ProcessInstance Datafields from ASP.NET code LIKE :

ProcessInstance.DataFields["DtFldRoleID"].Value = strK2RoleID; (This is only datafield I am writing here but there are few more)

Before this line I am creating process instance.

The process Instance is getting created properly but when I am looking for the value of this datafield (in a email event) it is just showing me the initial value of the datafield that is 0.

 

This is very surprising actually I have seen the values getting assigned from code previously but how this is not working now.

 

There are some datafields on which the whole business logic of K2 Process depends. If the datafields are not getting assignined then it will be very big issue.

 

Following is the code : I debugged and no any error.

 

public Int32 InvokeK2Process(RequestFormDTO requestFormDTO)
        {
            ProcessInstance piK2ProcessInstance = null;
            int procInstID = 0;
            //Declare and Open Connection to K2 Server
            Connection conn = new Connection();

  try
            {
                strK2ConnectionStringValue = K2ConnectionString();
                conn.Open(WebConfigurationManager.AppSettings.Get(GlobalConstant.K2_WORKFLOW_SERVER_NAME), strK2ConnectionStringValue);
                               string piK2ProcessInstance = "@StandardRequestWorkflow";
                piK2ProcessInstance = conn.CreateProcessInstance(strK2ProcessName);

 conn.StartProcessInstance(piK2ProcessInstance );             
              
                procInstID=piK2ProcessInstance.ID;
                piK2ProcessInstance .DataFields["DtFldFormID"].Value = requestFormDTO.ServiceMasterId.ToString();
                piK2ProcessInstance .DataFields["DtFldUserID"].Value = requestFormDTO.LoggedInUserId.ToString();
                piK2ProcessInstance .DataFields["DtFldRoleID"].Value = requestFormDTO.RoleId.ToString();
                piK2ProcessInstance .DataFields["DtFldRequestID"].Value = requestFormDTO.ServiceRequestId.ToString();
                piK2ProcessInstance .Update();
               
               
            }

 

Please, help on this, Is the code correct.


1 reply

Badge +8

Why are you starting your process instance then updating your data fields?  Move this line:

 

conn.StartProcessInstance(piK2ProcessInstance );

 to the end of your method and remove the Update() call, like so:

 

piK2ProcessInstance = conn.CreateProcessInstance(strK2ProcessName);

// Assign data fields here

conn.StartProcessInstance(piK2ProcessInstance );
procInstID=piK2ProcessInstance.ID;

I would suspect what is happening is that the K2 engine is sending the email before your code updates the process data fields.

 

Unfortunately, the K2 engine isn't tied into MSDTC or some other transaction coordinator.  This presents a couple of challenges:

 

1. If your method starts a process but then performs other time-consuming operations (for example, saving additional data to a database), the K2 engine launches the process immediately without waiting for your other operations to complete.

 

2. There is no ability to rollback a transaction if the other operations associated with starting a process fail.  This can leave you with process instances that shouldn't exist because of the failed "transaction."

 

The first issue can be worked around by adding a brief delay to the start rule for your process (we have had to do this in a couple of our processes).  There isn't a good workaround for the second option.

Reply