Recursively get db entries

Hi Everyone, 

I've got a CDT that has an Id as a Primary Key and then a Numeric field that is a reference to a previous same-cdt entry id.

Starting from an ID I need to back-track all the way to the first "father" entry. All should be done using Expression Rules.

To give you an example I would like to start from contract ID:4 and backtrack all the way to ID 1 (that has no father id)

I managed to achieve the backtracking with a process but I can't make it work with a reduce() or a forEach() function.

Has someone found something like this in his history?

Best regards, Luca.

  Discussion posts and replies are publicly visible

Parents
  • 0
    Certified Lead Developer

    Hi Luca,

    I wouldn't recommend using this type of recursion in Appian as it's not a best practice. But, I tried it using a recursive call to the main expression rule to backtrack through the list mainly using the displayvalue function. Here's the code snippet and it returns a list of strings that u can edit as per your requirement, for this case i passed the ri value as 4 -

     "FID value is 3 FID value is 2 FID value is 1 Depth reached "(Text)

    a!localVariables(
      local!cdtset: {
        'type!{urn:com:appian:types}test_cdt_1'('ID': 1, 'fID': null),
        'type!{urn:com:appian:types}test_cdt_1'('ID': 2, 'fID': 1),
        'type!{urn:com:appian:types}test_cdt_1'('ID': 3, 'fID': 2),
        'type!{urn:com:appian:types}test_cdt_1'('ID': 4, 'fID': 3)
      },
      local!newvalue: displayvalue(
        ri!value,
        local!cdtset.'ID',
        local!cdtset.'fID',
        0
      ),
      if(
        or(
          a!isNullOrEmpty(local!newvalue),
          local!newvalue = 0
        ),
        { "Depth reached " & local!newvalue },
        {
          "FID value is " & local!newvalue & char(10) & rule!test_recursiveEr(local!newvalue)
        }
      )
    )

Reply
  • 0
    Certified Lead Developer

    Hi Luca,

    I wouldn't recommend using this type of recursion in Appian as it's not a best practice. But, I tried it using a recursive call to the main expression rule to backtrack through the list mainly using the displayvalue function. Here's the code snippet and it returns a list of strings that u can edit as per your requirement, for this case i passed the ri value as 4 -

     "FID value is 3 FID value is 2 FID value is 1 Depth reached "(Text)

    a!localVariables(
      local!cdtset: {
        'type!{urn:com:appian:types}test_cdt_1'('ID': 1, 'fID': null),
        'type!{urn:com:appian:types}test_cdt_1'('ID': 2, 'fID': 1),
        'type!{urn:com:appian:types}test_cdt_1'('ID': 3, 'fID': 2),
        'type!{urn:com:appian:types}test_cdt_1'('ID': 4, 'fID': 3)
      },
      local!newvalue: displayvalue(
        ri!value,
        local!cdtset.'ID',
        local!cdtset.'fID',
        0
      ),
      if(
        or(
          a!isNullOrEmpty(local!newvalue),
          local!newvalue = 0
        ),
        { "Depth reached " & local!newvalue },
        {
          "FID value is " & local!newvalue & char(10) & rule!test_recursiveEr(local!newvalue)
        }
      )
    )

Children
No Data