BPM, Workflow, and Case

 View Only
Expand all | Collapse all

How to get instance variable values within a human task after it is started?

  • 1.  How to get instance variable values within a human task after it is started?

    Posted Wed February 10, 2021 03:55 PM
    Hi All,

    We can set process instance variables within a task using tw.system.updateCurrentInstanceVariablesByCurrentTaskOwner. But how can we get their values from code when the human task is running yet? I mean, not as Input parameter, but the actual values from code while the task is running, or postponed but later continued? Is it possible at all?

    Thx,



    ------------------------------
    Laszlo
    ------------------------------


  • 2.  RE: How to get instance variable values within a human task after it is started?

    Posted Wed February 10, 2021 04:59 PM
    The instance variables can be read using the ReST API. I'd assume they pick up on the changes made via that JS call (but I've not tested that)

    ------------------------------
    Andrew PaierAndrew Paier
    ------------------------------



  • 3.  RE: How to get instance variable values within a human task after it is started?

    Posted Thu February 11, 2021 02:31 AM
    Good Idea, thanks Andrew!

    ------------------------------
    Laszlo
    ------------------------------



  • 4.  RE: How to get instance variable values within a human task after it is started?

    Posted Thu February 11, 2021 03:47 AM

    Hi Andrew, and All!

    Finally I found the solution using JS API:
    1. Get the process instance by its ID
      var pi = tw.system.findProcessInstanceByID(instanceId);
    2. Get a business data of the process instance by its name
      var value = pi.businessData.get("variableName");
    More info can be found here: https://www.ibm.com/support/pages/how-access-process-instance-data

    I tested and it returns the current value of the process variables, so it returns the new values right after a call of tw.system.updateCurrentInstanceVariablesByCurrentTaskOwner(...);


    ------------------------------
    Laszlo
    ------------------------------



  • 5.  RE: How to get instance variable values within a human task after it is started?

    Posted Thu February 11, 2021 10:47 AM
    Excellent find! I vaguely remember that perhaps in the past there were some problems with those calls. This might be bad memory on my part of a bug that has since been fixed. I want to say that there was a problem retrieving complex/nested types that way. Can you share if you are pulling Business Objects or individual data?

    ------------------------------
    Andrew Paier
    ------------------------------



  • 6.  RE: How to get instance variable values within a human task after it is started?

    Posted Thu February 11, 2021 11:00 AM
    Well, using this we cannot get complex objects, only simple type variables and lists.
    Simple type variables are returned as is (boolean as boolean, date as date, etc.).
    Lists are returned as serialized XML  string, so we must deserialize them into a variable with type list of object.

    I use this code to get process level variables:

    tw.local.name (String) input paramer: the name of the variable to return
    tw.local.values (ANY*) output parameter: the returned list if the variable is a list of object
    tw.local.value (ANY) output parameter: the returned value of the variable if the variable isn't a list

    var pi = tw.system.findProcessInstanceByID(tw.system.currentProcessInstanceID);
    var value = pi.businessData.get(tw.local.name);

    if (value != null) {
    if (/^<\s*variable\s*type\s*=\s*".*\[\]"\s*>.*/.test(value)) {
    // List in XML
    tw.local.values = tw.system.serializer.fromXml("" + value);
    } else {
    // Single value
    if (typeof value === "string") {
    tw.local.value = "" + value;
    } else {
    tw.local.value = value;
    }
    }
    }


    ------------------------------
    Laszlo
    ------------------------------



  • 7.  RE: How to get instance variable values within a human task after it is started?

    IBM Champion
    Posted Thu February 11, 2021 05:06 PM
    That's nice! Good technique to work with process variables at CSHS/service flow level.

    You wouldn't need to pull complex objects from BPD anyway because it's a bad bad idea to have complex objects as instance variables. Your production servers won't hold it with some tens of thousands of instances with complex variables in it.

    My rule of thumb is always keep process variables primitive, just a bunch of ids, not even lists!

    Also on the point of getting data and updating data inside a coach, I've always preferred to use REST API inside coach views. Because that gives your users Ajax experience aka modern web experience. I guess you can do that with boundary events and JS API but I've always found them limited and troublesome.

    ------------------------------
    Thong Huynh
    Sydney NSW
    ------------------------------



  • 8.  RE: How to get instance variable values within a human task after it is started?

    Posted Sun July 17, 2022 09:44 PM

    I came across this and remembered pulling complex variables from process instance data.
    - will only work if all attributes in the complex type are primitive (no further nesting)
    + should safeguard you against changes in the complex type between snapshots (fills only attributes in the "current type")

    Sample code:

    // Get complex object from process instance data and assign it to obj
    function setNestedObject(obj, instanceObjName, instanceData) {
    	var props = obj.propertyNames;
    	for (var i = 0; i < props.length; i++)
    		obj.setPropertyValue(props[i],instanceData.get(instanceObjName + "." + props[i]));
    }
    
    var pi = tw.system.findProcessInstanceByID(tw.local.pid);
    if (pi) {
    	var instanceData = pi.businessData;	
    	setNestedObject(tw.local.pruefung.kfmPruefer, "pruefung.kfmPruefer", instanceData);
    }

    In this sample there is a complex object pruefung on BPD-level and kfmPruefer is a nested complex object in pruefung.
    kfmPruefer only has Strings. It should also work with Numbers. However, probably not with Dates as this is not a primitive type in JS (I have not tested this).
    I guess you could make this work for arbitrary complex object, but it would get complicated as you will need recursion, type checking/handling etc.



    ------------------------------
    Frank Hillebrecht
    ------------------------------



  • 9.  RE: How to get instance variable values within a human task after it is started?

    Posted Fri August 19, 2022 10:55 AM
    Thank you Andrew! so helpful

    ------------------------------
    Karen Onnabit
    ------------------------------