Skip to main content
Kinetic Community

Using Loop Values

This article describes and gives examples of how the results of nodes contained within a Task Tree loop can be used.

Details

A loop is a repeated execution of nodes within Kinetic Task.  The nodes in loop will repeat an amount of times equal to the amount of input variables provided to the loop.  As an example, a list of managers can be provided to a loop which will iterate through and create an approval for each of them.

Within each loop is typically additional nodes used to access and/or build values for a process.  For example node can be used retrieve additional information such as email address for each manager in the loop.  Accessing results of these nodes inside the loop, return different results if the nodes which are accessing the results are inside or outside of the loop.  The method of accessing the results is also different.  For information on creating a loop please see Loops and Joins.

The examples in this article frequently use the Ruby inspectMethod in a "Echo" Node to help show results of nodes in the Task Tree. The inspect Method returns a string containing a human-readable representation of the node results.  Looking at the results in each of the examples you will see it returns a Hash(Similar to an Array).  Using the inspect method is a useful tool during development to see the node results but not useful for nodes in the final Task Tree.

In the Echo Node "output" is the only result returned by the node.  Each handler may return a set of results and they likely have a name other than 'output'.

An example Service Item is included to help show the results of some of these nodes in Kinetic Request.  You may download it an install it on a local Kinetic Request installation.

Accessing Node Results From Within the Loop

In the Task Tree, results from a node can be accessed and used by another node or a connector.  If both nodes are within a loop, the results of the source node will return a value from only within the context of that same loop iteration. 

 

Content of Echo Node Parameter Result

<%=@results['Get Email'].inspect%>

{“output”=>”Bob@acme.com”}

<%=@results['Get Email']['output']%>

Bob@acme.com

Accessing Node Results From Outside the Loop

When accessing the results from outside to the loop the, the same code that was used inside the loop, the source node returns a more complex Hashof values.  The hash now contains the values from each loop iteration.

 

Content of Echo Node Parameter Result

<%=@results['Get Email'].inspect%>

{0=>{"output"=>"Bob@acme.com"}, 1=>{"output"=>""}, 2=>{"output"=>"Brian@acme.com"}, 3=>{"output"=>"Charles@acme.com"}}

<%=@results['Get Email']['output']%>

empty

It is also important to note that if you have a routine inside the loop, it's output isn't quite the same as a handler's. Outside the loop, a routine's results would also seem empty without the inspect, but with the inspect they look as follows:

Content of Echo Node Parameter Result
<%=@results['Individual Approval Routine'].inspect%> {0=>{"Run Id"=>16430, "Source Id"=>nil, "Tree Id"=>278, "Trigger Id"=>33544}, 
1=>{"Run Id"=>16431, "Source Id"=>nil, "Tree Id"=>278, "Trigger Id"=>33545, "Approval Response"=>"Approve", "Denial Comments"=>nil, "Approver Login ID"=>"aramey"}, 
2=>{"Run Id"=>16432, "Source Id"=>nil, "Tree Id"=>278, "Trigger Id"=>33546}}

So they must be access slightly differently (as referenced below).

Using the Results

In the examples from above the only immediately usable results are returned from <%=@results['Get Email']['output']%> and only when accessing a result from within the loop.  When used outside the loop no value is returned.

Additional efforts are needed to access the results from outside the loop but there is also increased usefulness. Knowing that it returns a hash we can use Ruby methods to evaluate it and manipulate it as we would any other Ruby hash.  Now other Ruby Methods can be used to manipulate the result set.

All nodes return a string.  Therefore, an Array will be converted to a string as the result of a node.  The inspect method, used frequently below, converts the array into a string representation which is identifiable as an array before the node naturally converts it to a string.  However the quotes, less than and greater than are encoded when viewed in the Task Management Console.  In order to better display the results, the examples have decoded the symbols back to the orignal values.

 

Ruby Method Content of Echo Node Parameter Description Result
inspect <%=@results['Get Email'].inspect%> Using the inspect method revels that @results['Get Email'] returns a hash which combines all values for the Get Email node from within the loop.

{0=>{"output"=>"Bob@acme.com"}, 1=>{"output"=>""}, 2=>{"output"=>"Brian@acme.com"}, 3=>{"output"=>"Charles@acme.com"}}

collect

<%=
   @results['Get Email'].collect{|key,values|
      values['output']
   }.inspect

%>

The collect method invokes block once for each element of self. Creates a new array containing the values returned by the block.  The contents of the block is "values['output']" and there for returns the "output" value for each element. ["Charles@acme.com", "Brian@acme.com", "", "Bob@acme.com"]
collect with out inspect

<%=
   @results['Get Email'].collect{|key,values|
      values['output']
   }

%>

Without the inspect to create a string representation of the array the values in the array are combined into one value w/o and separator. Charles@acme.comBrian@acme.comBob@acme.com
compact <%=
    @results['Get Email'].collect{|key,values|
        if (values['output'] != "")
            values['output']
        end
   }.compact.inspect
%>
By using an if statement in the block to ignore the empty array objects and then using the compact method any empty results are removed from the results. ["Charles@acme.com", "Brian@acme.com", "Bob@acme.com"]
compact (from subtree or Task 4) <%=
    @results['Get Email'].collect{|key,values|
        if (values.has_key?('output'))
            values['output']
        end
   }.compact.inspect
%>
By using a has_key? you can successfully only look at the items that have that result. From a routine, if you do not use has_key, you will get an index error if your loop isn't an all loop. ["Charles@acme.com", "Brian@acme.com", "Bob@acme.com"]
join <%=
    @results['Get Email'].collect{|key,values|
        if (values['output'] != "")
            values['output']
        end
   }.compact.join('; ')
%>
The inspect's results are not useful in a Task tree to other node.  By using the join method a list of all results joined with a value of "; ". Bob@acme.com; Brian@acme.com; Charles@acme.com
first <%=
    @results['Get Email'].collect{|key,values|
        if (values['output'] != "")
            values['output']
        end
   }.compact.first
%>
Returns the first value in the hash.  If a results come from a deferred node this can be used to get the value of the node which was first completed. Bob@acme.com
include? <%=
    @results['Get Email'].collect{|key,values|
        if (values['output'] != "")
            values['output']
        end
   }.compact.include?('Charles@acme.com')
%>

include? can be used to
evaluate is a specific value is included in the results.

true

count <%=
    @results['Get Email'].collect{|key,values|
        if (values['output'] != "")
            values['output']
        end
   }.compact.count('Charles@acme.com')
%>
The count method can be used to count the amount of times which a specific values exists in an array 1
length <%=
    @results['Get Email'].collect{|key,values|
        if (values['output'] != "")
            values['output']
        end
   }.compact.length
%>
The length method can be used display the length of the array. 3

Links

There are other methods which could be used to meet other needs.  Documentation of the Ruby events used above and other ones cab be found at the following links.

http://www.ruby-doc.org/core-1.9.3/String.html

http://www.ruby-doc.org/core-1.9.3/Array.html

http://www.ruby-doc.org/core-1.9.3/Hash.html