I need to make a record-like query for a 1 to N relationship, in which it only returns the values of the parent record if all the values in the N relationship meet a condition. Example :
I have a record called "Need" and it is related to another called "needchangestate" through a relationship 1 to N, I need it to return only the needs where all the needchangestate meet a condition, if all the elements of the relationship N do not meet the condition, the element Need should not appear
Currently it returns me an investment where the condition is met in at least one of the records of the relationship N but this is not what I am looking for.
Discussion posts and replies are publicly visible
I think the best way to accomplish this is via a View unfortunately.
There is a way to do it via queryRecordType but it involves excluding IDs from a previous query but this approach has limitations.
hi Raúl Gómez Moya I agree with what Mathieu Drouin has mentioned; there will be many limitations. However, I'm not entirely sure. Could you try implementing a custom field in your main record type? This custom field should incorporate conditions based on the child records, such as counting the statuses from the child records. You could then filter these in your query. Please give this a try.
Hello. As I understand it, to implement what you need, you can use the following code as an example. But it requires iterating the list to be able to filter the data, and if your case is to bring a large batch of data this can affect performance. Also, you can use Mathieu Drouin suggestion as a reference and query directly to the view. Regarding pagination, if you need to show it in an interface you could use pagination and pass it as a parameter to your rule and bring only what is necessary per page.
a!localVariables( local!data: a!queryRecordType( recordType: 'recordType!SB Need', fields: { 'recordType!SB Need.fields.name', 'recordType!SB Need.relationships.needDetail.fields.decision' }, pagingInfo: a!pagingInfo( startIndex: 1, batchSize: 10, sort: a!sortInfo( field: 'recordType!SB Need.fields.id', ascending: false ) ) ).data, local!filteredData: a!forEach( items: local!data, expression: a!localVariables( local!relatedData: fv!item['recordType!SB Need.relationships.needDetail.fields.decision'], if( and( a!isNotNullOrEmpty(local!relatedData), count(local!relatedData) = length(wherecontains(false, toboolean(local!relatedData))), ), fv!item, {} ) ) ), local!filteredData )
What about one or two custom fields that hold the number of these states?
Thank you all for the answers, we are testing to see what is the best option, I am surprised that Appian does not have a simple way to get values of the 1 to N ratio that meet the specific conditions. I think a is a very typical case that wanting to retrieve records only if the relationship N all records meet a condition. For example: Give me all the investments that have all the approved amounts
I have not achieved it through custom fields, if you can think of a way, please share it
I've thought about your solution and I think I have another way to do it.
This does depend on how dynamic you want this functionality but if there is one (or not many) specific things you want then custom record fields do work. One way to do this is to have two "Aggregate Related Record Fields" custom record fields in the parent record (in your case, the "Need" record) that looks at the child record. One counts the number of children where decision is FALSE (`countOfNoDecision`) and the other counts the number of child records (`countOfAllDecisions`).
Then in your query you ask for only Need records where `countOfNoDecision` = `countOfAllDecisions`.
I can't do your solution due to an error in the expression
Apologies, it needs a third custom record team to do the comparison.