Skip to main content
July 28, 2022
Solved

Is it possible to fetch a CDT field by numerical index?

  • July 28, 2022
  • 11 replies
  • 1 view

Hi all,

I want to get fields in a CDT based on their numerical index, i.e. if I have a CDT like:

{'type!{urn:com:appian:types:TEST}TEST_Employee'('firstName': "Frank", 'secondName': "Smith", 'age': 29)}

To get the first field, is there a way to index it by number? Something like index(local!testCdt, 1, null) would return "Frank". 

Thanks for your help!

    Best answer by harshitb6843

    Change the value of the local!index variable to your desired index. 

    a!localVariables(
      local!data: 'type!{urn:com:appian:types:DA}DA_Student'(
        firstName_txt: "Harshit",
        lastName_txt: "Bumb"
      ),
      local!keys: a!keys(local!data),
      local!index: 3,
      local!selectedkey: index(local!keys,local!index,{}),
      index(local!data,local!selectedkey,{})
    )

    11 replies

    harshitb6843
    July 28, 2022

    Change the value of the local!index variable to your desired index. 

    a!localVariables(
      local!data: 'type!{urn:com:appian:types:DA}DA_Student'(
        firstName_txt: "Harshit",
        lastName_txt: "Bumb"
      ),
      local!keys: a!keys(local!data),
      local!index: 3,
      local!selectedkey: index(local!keys,local!index,{}),
      index(local!data,local!selectedkey,{})
    )

    peter.lewis
    Employee
    July 28, 2022

    Keep in mind that this is dependent on the original structure and ordering of the CDT, not necessarily on what data is provided. So in your example even if "firstName" is the first key you provide in your expression, if the CDT has a different first field (e.g. ID), then it will always return the ID instead.

    Like Stefan mentioned below, I'm curious how you plan to use this - there may be other approaches that could achieve what you're looking for more consistently.

    johng0005Author
    July 28, 2022

    Hey Peter, I've replied to Stefan's comment below. 

    stefanhelzle0001
    Brainy
    July 28, 2022

    Is there a specific reason to do that? While the order is typically fix, I would definitely not rely on it.

    johng0005Author
    July 28, 2022

    We have a list of attribute sections that a user can click through, and I need to keep track of which sections have been opened for editing. I've created a CDT that contains the list of attribute sections as the keys and booleans as the values, but I can't make the field names match the attribute section names exactly (one of them has a special character) and I can't make the field names the attribute index because Appian doesn't allow numerical values as field names. 

    What I was planning to do was to get the attribute section index, and use that to index the CDT to fetch the corresponding value.

    So for example:

    Attribute sections indexes and names that a user sees: 

    1. Name

    2. Age & Address

    3. Occupation

    CDT structure:

    'type!{urn:com:appian:types:TEST}TEST_isSectionOpenForEditing'(
    name: 1,
    ageAndAddress: 0,

    occupation: 1
    )

    If the user clicks the second attribute section, Age & Address, I want to fetch the corresponding value from the CDT to verify whether that section is open for editing. 

    Let me know if that makes sense. 

    stefanhelzle0001
    Brainy
    July 28, 2022

    Why not store this in a key-value fashion? Then you can store the section technical names in constants and easily refer to them in UI and in the key-value store.