Appian Community
Site
Search
Sign In/Register
Site
Search
User
DISCUSS
LEARN
SUCCESS
SUPPORT
Documentation
AppMarket
More
Cancel
I'm looking for ...
State
Not Answered
Replies
7 replies
Subscribers
7 subscribers
Views
5372 views
Users
0 members are here
Share
More
Cancel
Related Discussions
Home
»
Discussions
»
AI and Rules
If a query entity returns an empty set (due to the filters), will that empty set
Jason Ruvinsky
Certified Senior Developer
over 9 years ago
If a query entity returns an empty set (due to the filters), will that empty set be automatically cast to the type of the Data Entity, or do we need to perform this cast in our rules to ensure the query entity is null-safe (it always returns the expected type, whether it is an empty array or not. Otherwise using dot-notation to access fields of a CDT will result in errors, although the property() function would also let us be null-safe)....
OriginalPostID-131791
OriginalPostID-131791
Discussion posts and replies are publicly visible
0
abhi.jana
Appian Employee
over 9 years ago
queryEntity always returns a datasubset type so you can use the dot notation to access the fields even when they have no values
Cancel
Vote Up
0
Vote Down
Sign in to reply
Verify Answer
Cancel
0
Jason Ruvinsky
Certified Senior Developer
over 9 years ago
Abhi, in my testing I've found that not to be the case. Yes, you can use dot notation to get the .data, but beyond that you can't necessarily treat the .data as the CDT type. You need to check if it is length()=0, and use cast(CDT_TYPE,{}) to create a valid empty CDT of the expected type to make it null-safe.
Cancel
Vote Up
0
Vote Down
Sign in to reply
Verify Answer
Cancel
0
abhi.jana
Appian Employee
over 9 years ago
Yes, but the result of queryEntity as a whole (which includes paginginfo in addition to data) is a datasubset. As for the .data part, the type is shown to be "List of Variant" (CDT) even when the data is null, so casting may not be required. You can check using typename(typeof(<data>)) and see that it returns the same type in both cases.
Cancel
Vote Up
0
Vote Down
Sign in to reply
Verify Answer
Cancel
0
Jason Ruvinsky
Certified Senior Developer
over 9 years ago
To follow up on this: I have created the following practice, which prevents an empty array that would not allow indexing into it with the dot notation:
=rule!ARR_replaceEmpty(
a!queryEntity(
<snip>
).data
,cast('type!{_namespace_}_TYPE_',{})
)
Where ARR_replaceEmpty is defined as:
=if(rule!APN_isEmpty(ri!nullableValue), ri!replacementValue, ri!nullableValue)
(Basically APN_replaceNull from the common objects app, except for empty arrays).
This works for us. (You are then able to assume that whatever you got from the query entity is the expected data type, even if it's an empty set).
Cancel
Vote Up
0
Vote Down
Sign in to reply
Verify Answer
Cancel
0
pavithrac
over 9 years ago
Hi,
If a query entity is returning an empty set, we can also use index function to access the type fields with dot notation
Then the code will be,
index(
a!queryEntity(
<snip>
).data, "fieldName" , {}
)
To prevent accessing the fields with dot notation, then we can do casting as mentioned above as casting wont allow indexing.
Cancel
Vote Up
0
Vote Down
Sign in to reply
Verify Answer
Cancel
0
Jason Ruvinsky
Certified Senior Developer
over 9 years ago
Hi pavithrac,
I did mention the property() function in the original post (which is the same as the index() function). However, I was seeking suggestions where it wouldn't be necessary to wrap every access to a CDT field with property/index. Checking if the query entity returns an empty set, and returning a cast of {} to the CDT type seems to do the trick.
Cancel
Vote Up
0
Vote Down
Sign in to reply
Verify Answer
Cancel
0
Jason Ruvinsky
Certified Senior Developer
over 9 years ago
Note that my above suggestion ends up giving you an array of a single element of your CDT type (which has all fields null). This is not equal to a null or an empty list, so certain things won't work properly when you do this.
Cancel
Vote Up
0
Vote Down
Sign in to reply
Verify Answer
Cancel