Skip to main content
raulg0001
Inspiring
February 26, 2025
Solved

query record result

  • February 26, 2025
  • 11 replies
  • 0 views

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.

Best answer by andrewh6762

Apologies, it needs a third custom record team to do the comparison.

11 replies

mathieud0001
February 26, 2025

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.

karumurua531442
February 26, 2025

hi [mention:a2a33cb01839432998847be591819973:e9ed411860ed4f2ba0265705b8793d05]  I agree with what [mention:f5e81b8502fa4e77ad12697686a4fc56:e9ed411860ed4f2ba0265705b8793d05]  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.

February 26, 2025

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 [mention:f5e81b8502fa4e77ad12697686a4fc56:e9ed411860ed4f2ba0265705b8793d05]  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
)

raulg0001
raulg0001Author
Inspiring
February 26, 2025

I've thought about your solution and I think I have another way to do it.

stefanhelzle0001
February 26, 2025

What about one or two custom fields that hold the number of these states?

raulg0001
raulg0001Author
Inspiring
February 26, 2025

I have not achieved it through custom fields, if you can think of a way, please share it

andrewh6762
February 26, 2025

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`.

raulg0001
raulg0001Author
Inspiring
February 26, 2025

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