What it is:
It’s a way to execute an action (or several) on specific or several columns in a table. You can do all sorts of stuff with it like copying contents from one column to another column, copying contents from one column from one table to another table’s column. The caveat is that by default it will execute the command on the entire table, so specify the data set you want to interact with by using filters.
Why and when to use it:
You might use this feature if you want to copy table records from one table to another, or duplicating data within the same table and editing them. You can also use this method to get the sum of an entire table’s column, or modify each record in a column all at once.
How:
First off, this is code that will be used in the Advanced > Custom Command section in the response set.
(There’s also a bug here where if you leave and come back to this page, or refresh and return back to this response set the Custom Commands will all look blank and you’ll have to toggle them back on to edit them - but they’re there still!)
Once selected, you now have space to enter the code. For this example we’re going to do the following:
-
Add contents from multiple rows to a Single Item Client Variable (so a variable similar to my.loggedin_user or my.users, etc.) so we end up with the sum of the row.
-
We’re also going to filter the row to only add contents that are less than 1440 (arbitrary number).
For reference, these are the variables we’ll be working with:
Once Custom Command is selected, you now have space to enter the code:
{ date_test.filter("dif1m", _less_than("1440")).each do |item| difference.avg = item.dif1m.to_i + difference.avg end }
To break this down,
date_test = the collection app variable table we’re looking at.
dif1m = the variable column we’re going to target.
difference.avg = the single item client variable we’re going to hold our calculations in.
So what the code is saying here is:
-
Look in the date_test variable table, and look at the dif1m column within that table.
-
Check to see if there are numbers less than "1440" and if so, grab the first record.
-
Then, in the single item client variable called difference.avg take that first record item.dif1m then convert that to an integer (.to_i) and add it to the number that is stored in difference.avg
-
Once complete, repeat the process excluding the record you’ve already ran through .each do (this piece of the code is before |item|)
-
At the end of the table, complete the process end
The primary principle of this is that the first half of the code before .each do |item| looks at the base table. This is the table that you want to either copy contents from, modify the contents of, etc.
Everything after .each do |item| is the action you want Chorus to take.
difference.avg = item.dif1m.to_i + difference.avgIn the above example we told Chorus to look at the single item client variable and essentially add applicable rows’ content to the variable.
Comments
0 comments
Article is closed for comments.