How can I use MS Flow to select an individual object, by value for a specified property, from an array?
Example array:
[
{
item_id: '1234'
},
{
item_id: '4567'
}
]
In the example above, I may only want to work with the first object and the rest of its available properties.
Happy to use the Workflow Definition Language and/or any of the Data Operations actions.
I solved this by using the "Data operations - Filter" action.
Ignore the error in red - it is an array.
My left-hand expression for "item_id" is:
item()?['item_id']
And then I statically enter the item ID I wish to access in the right-hand input.
DocumentNo Item will then be an array itself with only 0 or 1 elements and can be used like so:
body('DocumentNo_Item')?[0]?['label']
Related
I've seen the JSON array questions here and I'm still a little lost, so could use some extra help.
Here's the setup:
My Flow calls a sproc on my DB and that sproc returns this JSON:
{
"ResultSets": {
"Table1": [
{
"OrderID": 9518338,
"BasketID": 9518338,
"RefID": 65178176,
"SiteConfigID": 237
}
]
},
"OutputParameters": {}
}
Then I use a PARSE JSON action to get what looks like the same result, but now I'm told it's parsed and I can call variables.
Issue is when I try to call just, say, SiteConfigID, I get "The output you selected is inside a collection and needs to be looped over to be accessed. This action cannot be inside a foreach."
After some research, I know what's going on here. Table1 is an Array, and I need to tell PowerAutomate to just grab the first record of that array so it knows it's working with just a record instead of a full array. Fair enough. So I spin up a "Return Values to Virtual Power Agents" action just to see my output. I know I'm supposed to use a 'first' expression or a 'get [0] from array expression here, but I can't seem to make them work. Below are what I've tried and the errors I get:
Tried:
first(body('Parse-Sproc')?['Table1/SiteConfigID'])
Got: InvalidTemplate. Unable to process template language expressions in action 'Return_value(s)_to_Power_Virtual_Agents' inputs at line '0' and column '0': 'The template language function 'first' expects its parameter be an array or a string. The provided value is of type 'Null'. Please see https://aka.ms/logicexpressions#first for usage details.'.
Also Tried:
body('Parse-Sproc')?['Table1/SiteconfigID']
which just returns a null valued variable
Finally I tried
outputs('Parse-Sproc')?['Table1']?['value'][0]?['SiteConfigID']
Which STILL gives me a null-valued variable. It's the worst.
In that last expression, I also switched the variable type in the return to pva action to a string instead of a number, no dice.
Also, changed 'outputs' in that expression for 'body' .. also no dice
Here is a screenie of the setup:
To be clear: the end result i'm looking for is for the system to just return "SiteConfigID" as a string or an int so that I can pipe that into a virtual agent.
I believe this is what you need as an expression ...
body('Parse-Sproc')?['ResultSets']['Table1'][0]?['SiteConfigID']
You can see I'm just traversing down to the object and through the array to get the value.
Naturally, I don't have your exact flow but if I use your JSON and load it up into Parse JSON step to get the schema, I am able to get the result. I do get a different schema to you though so will be interesting to see if it directly translates.
I'm using free jqgrid, I implemented custom search operands using "customSortOperations: {}", I want to change the search field input to a select when I choose one of my custom operands, is there a way to do that? if not is there an event that fires when the operand is chosen to listen for it and implement my custom search field change? here is a sample of how I'm using "customSortOperations: {}"
$.extend($.jgrid.defaults, {
customSortOperations: {
tst: {
operand: "T",
text: "Test",
}
},
});
You posted only short fragment of your code. Thus, I'm not sure that I full understand your problem. To use new custom search operand you should define filter method inside of customSortOperations.tst and include the operation "tst" inside of searchoptions.sopt of column definition in colModel. If you want to use the new operation "tst" as default searching operation then the operation "tst" should be the first in searchoptions.sopt array. See for example https://jsfiddle.net/OlegKi/f3gLde6m/2/ created for the old answer for more details.
I have this scenario where I want to check if user's email is already been used when trying to signup and throw an error “user already exists” in that case.
here’s what my sequence looks like:
Input action -> CloudnAnt EXE QUERY-Find -> Validate email action -> CloudnAnt create User/Throw user already exists error action
my json payload looks like this :
{email: 'blahblah#domain.com', pass: "pass"}
the problem is that I have no control over the output of the cloudnant predefined actions, and by that I lose the payload after the 2nd action "CloudnAnt EXE QUERY-Find User with email action"
Is there a way to keep my input all the way through the sequence? and could the forward combinator be the solution for this issue?
I'd try /whisk.system/combinators/forwarder, which lets you pass arguments to an action and then specify arguments that get sent to the next one. Ie, I've got 5 arguments, let's pass 1 and 2 on to the next item in the sequence, and when done, pass 3, 4, 5 to the next one. I believe the output from the first item goes along as well.
The solution is very simple, just replace the cloudant action with a forward combinator which will invoke the cloudant action and forward the parameters around it.
to invoke a cloudant action you need get the full path for your action and that includes the namespace and the database name like the following:
/namespace/dbname/actionName
in my case it was /sansan/users/exec-query-find
so if you want to check whether the email exists or not in your cloudant db, and keep the original params just pass the following payload to your cloudant action
{
"data":{...},// some data you want to keep after the cloudant query
"query": {
"selector": {
"email": "email#domain.com"
}
},
"$actionName": "/sansan/users/exec-query-find",
"$forward": [// list of params you want forward
"data"
],
"$actionArgs": [ // list of params you want to feed the action
"query"
]
}
after running the sequence, the output should look like this:
{
"data": {...}, // your data
"docs": [...]// cloudant query results
}
the forward combinator is poorly documented, but you can find more details about it in Raymond Camden's blog post
look at Raymond's answer here too
Is it possible to search an account's custom data to find a value contained in an array?
Something like:
?customData.[arrayName].{key}=value
The Stormpath docs don't mention array searching.
Yes, with Stormpath it is totally possible to search for custom data even if the values are stored as an array!
Please note that the field names are simple names, and the values are what are different data types like array, map, string etc... so the query is not as complex as one would think :-)
For example, if I want to store custom data called favoriteColors, which is an array like
"favoriteColors": [ "red", "black", "blue", "white" ]
Notice the field name is just like any other field name. The value is the array.
To search for accounts which have a value red in the favoriteColors array, you just need the normal query syntax:
?customData.favoriteColors=red
The full request (if searching a Directory of accounts), might look like this:
https://api.stormpath.com/v1/directories/<directory_uid>/accounts?customData.favoriteColors=red
You could also do the same search on the Tenant resource to search tenant-wide (across all accounts):
https://api.stormpath.com/v1/tenants/<tenant_uid>/accounts?customData.favoriteColors=red
This query would match an account that contains red in the favoriteColors array. If I changed the query to ?customData.favoriteColors=yellow it would not match unless yellow was also added to the array.
Searching for custom data in an array can definitely be done. The syntax is: customData.{fieldName}\[{index}\]=value where {index} can be the specific index you are looking for, or * if you want to find it anywhere in the array. (Note that the [] characters are escaped with a backslash or the query interpreter gets it confused with a range query.)
If you leave off the index entirely, then \[*\] is implied. More precisely, Stormpath will check for either the value in the fieldName or the value as an element in an array of fieldName. However, syntactic sugar can only work if the array field is the last element in your search. Since you can put literally any JSON object into your custom data, Stormpath cannot check every single possibility. Imagine something like customData.foo.bar.baz.qux=bingo. Stormpath would not try to guess that maybe foo is an array, maybe bar is an array or not, maybe baz is an array or not - only maybe qux is an array or not. So, if you want to search an array of objects, you cannot leave out the \[*\].
Here is an example. I have an account with the custom data:
{
"favoriteThings": [
{
"thing": "raindrops",
"location": "on roses"
},
{
"thing": "whiskers",
"location": "on kittens"
},
{
"thing": "snowflakes",
"location": "on my nose and eye lashes"
}
],
"favoriteColors": [
"blue",
"grey"
]
}
The following queries will yield the following results:
customData.favoriteColors=blue will include this account.
customData.favoriteColors\[1\]=blue will not include this account because blue is not at index 1.
customData.favoriteThings\[*\].thing=whiskers will include this account
customData.favoriteThings\[*\].thing=ponies will not include this account because it does not list ponies as one of his favorite things, but may include other accounts with custom data in the same structure.
customData.favoriteThings.thing=whiskers would not include this account or any other accounts with the same custom data structure because in that case, Stormpath would be looking for a single nested JSON favoriteThings object, not an array.
I am new to programming and Ruby both. In some existing code, it says something like this:
kid_raising_btn=query("switch marked:'KIDRAISING'",:isOn).first
From this, I understand that a variable kid_raising_btn is defined, which will query and return true or false, then call the method first (but this is confusing).
How can I find what first does?
".first" is kind of convenient method.
Answer for the UI query comes as an array.
So the ".first" represent the first element of the array.
There are few methods like ".count"
Ex:
your Query :
ans = query("button",:accessibilityLabel)
Assume You will get result like this
[
[0] "icon rewards new",
[1] "icon my receipts new",
[2] "icon my account",
[3] "icon order#2x",
[4] "icon check in"
]
if you use ".first" like this
ans2 = query("button",:accessibilityLabel).first
you will get a String with first element as a result instead of an array.
"icon rewards new"
Now you can see you get the first element as the answer
From your description, it sounds like the method chaining here might be contributing to your confusion, so first let's rewrite that line of code like this:
query_response = query("switch marked: 'KIDRAISING'", :isOn)
kid_raising_btn = query_response.first
Now, it's easier to see that the #query method returns an object that has a #first method. You can take a look at this object to see what class it is (query_response.class), and then either look up the appropriate documentation for the class or find the method definition in your codebase. For example, if #query returns an array, you can find the documentation here: http://ruby-doc.org/core-2.0.0/Array.html#method-i-first