How to access CDT field by number ?

Certified Senior Developer

Hi,

I'm surprised to have to ask this question, but could you tell me how to access the value of a CDT field giving a number please ?

I would need to use this kind of code :

local!value: field(local!vehicle, 1) /* where "1" is the first field of the row */

a!localVariables(
  local!vehicle: rule!CJT2_GetVehiclesWithFilters(
    id: 1
  ).data[1],

  /* Here I just display some data with fieldname */
  property(local!vehicle, "id") & " - " & property(local!vehicle, "model")
  
  /* Now I would need to do the same thing, without using the fieldname, so I need a function that can do that
  I just tried to use the "property" function as an example but maybe is there any other function like "field" 
  or other ? */ 
  
  property(local!vehicle, 1) & " - " & property(local!vehicle, 2)
  field(local!vehicle, 1) & " - " & field(local!vehicle, 2)

)

Regards

  Discussion posts and replies are publicly visible

Parents
  • Never seen this use case but give this a go. Just out of interest, why access the field by a number? Not saying it's wrong I've just never come across a need to do this.

    a!localVariables(
      local!data: {
        id: 31,
        make: "Ford",
        model: "F150",
        year: 2019,
        country: "Australia",
        insured: true
      },
      local!keys: a!keys(local!data),
      local!fieldNumbersToDisplay: {2,3,4,6}, /*Can play around with this for different field numbers*/
      local!fieldsToDisplay: index(
        local!keys,
        local!fieldNumbersToDisplay,
        {}
      ),
      concat(
        a!forEach(
          items: local!fieldsToDisplay,
          expression: concat(
            proper(fv!item),
            ": ",
            index(
              local!data,
              fv!item,
              {}
            ),
            if(
              fv!isLast,
              {},
              " | "
            )
          )
        )
      )
    )

  • 0
    Certified Senior Developer
    in reply to ajhick

    That's great Ajhick, it is exactly what I was looking for.

    I was looking for such a way because sometimes, for business need, I have to compare all the fields accross the rows of a CDT.

    For example, keeping the common values accross many lines.

    Today, I compare theses lines using the fieldnames, but because the table in DB can evolve, using the fieldnames can introduce some lake of maintenability.

    With the method you've suggested me, if one day, somebody will add a field in the Table/CDT, the code will continue to work.

    (the developpers will not have to look in all Appian ER to ad this new field)

  • Ok, I'm not sure I fully understand but if you're comparing all fields you don't need the field numbers, you can just iterate through the keys. Also, if you're not comparing all fields and the field numbers are required I feel like field names are more robust as someone could add a column towards the start of a table and break your numbering.

    Again, I'm not across your exact use case but something is telling me there is a better way to achieve what you're after.

  • 0
    Certified Senior Developer
    in reply to ajhick

    I did not know that we could iterate through the keys. So if I can use only keys, that's perfect for me.

    Your code works with a Map but when I try with a queryEntity result (data), it does not work. Am I missing something ?

    ERROR : Expression evaluation error at function a!keys [line 4]: The passed parameter(s) are of the wrong type. Received the type List of Variant.

    a!localVariables(
      local!vehicle: rule!CJT2_GetVehiclesWithFilters(id: 1).data,
      local!keys: a!keys(local!vehicle),
      local!keys
    )

  • It's a bit of a guess but try this:

    a!localVariables(
      local!vehicle: a!flatten(rule!CJT2_GetVehiclesWithFilters(id: 1).data),
      local!keys: a!keys(local!vehicle),
      local!keys
    )

Reply Children