Saving document from the interface into CDT

This is the case:

- I have drop down field of categories

- Based on selected category corresponding image is shown

- Images are stored in constant which is array of documents

- The corresponding image is calculated by this rule (nothing really smart :) )

choose(
where(
{
ri!employee.category = "Boss",
ri!employee.category = "Team Leader",
ri!employee.category = "Business Analyst",
ri!employee.category = "Data Miner",
ri!employee.category = "Developer",
true
}
)[1],
a!documentImage(
document: cons!EEDM_CATEGORY_AVATARS[1]
),
a!documentImage(
document: cons!EEDM_CATEGORY_AVATARS[2]
),
a!documentImage(
document: cons!EEDM_CATEGORY_AVATARS[3]
),
a!documentImage(
document: cons!EEDM_CATEGORY_AVATARS[4]
),
a!documentImage(
document: cons!EEDM_CATEGORY_AVATARS[5]
),
null
)

- I would like to capture the corresponding image and store it into rule input based on CDT field categoryIcon which is of document type

I was trying to capture it into local variable with with() function (in image component expression rule) and then save it on SUBMIT button into ri!categoryIcon.

This was my shot:

1) expression for image component saves it into local variable:

with(
local!categoryIcon,
choose(
where(
{
ri!employee.category = "Boss",
ri!employee.category = "Team Leader",
ri!employee.category = "Business Analyst",
ri!employee.category = "Data Miner",
ri!employee.category = "Developer",
true
}
)[1],
a!documentImage(
document: cons!EEDM_CATEGORY_AVATARS[1]
),
a!documentImage(
document: cons!EEDM_CATEGORY_AVATARS[2]
),
a!documentImage(
document: cons!EEDM_CATEGORY_AVATARS[3]
),
a!documentImage(
document: cons!EEDM_CATEGORY_AVATARS[4]
),
a!documentImage(
document: cons!EEDM_CATEGORY_AVATARS[5]
),
null
))

2) save on SUBMIT button:

a!buttonWidget(
label: "Submit",
saveinto: a!save(ri!employee.categoryIcon, local!categoryIcon),
submit: true,
style: "PRIMARY"
)

This is the error I'm getting:

I believe I'm capturing this local variable in a wrong place (image component expression)?

UPDATE: Found out better solution in the meantime:

There was no need for local variable at all.

Rule input ri!employee.categoryIcon is calculated based on ri!employee.category in the Category dropdown component code:

a!dropdownField(
                      label: "Category",
                      labelPosition: "ABOVE",
                      placeholderLabel: "--- Select a Value ---",
                      choiceLabels: cons!EEDM_EMPLOYEE_CATEGORY_LIST,
                      choiceValues: cons!EEDM_EMPLOYEE_CATEGORY_LIST,
                      value: ri!employee.category,
                      saveInto: {
                        ri!employee.category,
                        a!save(
                          ri!employee.categoryIcon,
                          choose(
                            where(
                              {
                                ri!employee.category = "Boss",
                                ri!employee.category = "Team Leader",
                                ri!employee.category = "Business Analyst",
                                ri!employee.category = "Data Miner",
                                ri!employee.category = "Developer",
                                true
                              }
                            )[1],
                            cons!EEDM_CATEGORY_AVATARS[1],
                            cons!EEDM_CATEGORY_AVATARS[2],
                            cons!EEDM_CATEGORY_AVATARS[3],
                            cons!EEDM_CATEGORY_AVATARS[4],
                            cons!EEDM_CATEGORY_AVATARS[5],
                            null
                          )
                        )
                      },
                      required: true,
                      validations: {}
                    )

Key point is a!save block in the saveInto property of Category dropdown. This is the place where my rather simple case-when rule calculates category icon depending on "just selected" category value and stores it into rule input for category icon.

From this point I can use wanted value from the rule input instead from local variable as i initially intended to.

UPDATE2: Read Stewart's answer for more advanced solution!

  Discussion posts and replies are publicly visible

Parents
  • So, there are a few things worth commenting on here:

    1. the pattern to use a constant to hold a list of values, and then reference them individually using the index - e.g. [1] - is not a good pattern for two reasons:
      1. it's fragile to change - if you change the order of the items in the constant your code breaks
      2. it makes the code unreadable without having to navigate to the constant to see what instance 5 is, for example
    2. It's better to create one constant for each item in your list, and then use an expression rule to generate the list. In this way you address both of the above issues each of which has a corresponding 
    3. From the screenshot you've provided it looks like you have a list of labels ({"Boss","Team Leader",...etc.}) each of which has a corresponding icon/image. You could implement a second set of constants and an expression rule to generate the list of labels to go alongside the list of documents that are the corresponding icons/images...but this is also fragile to change e.g. the lists get out of step, or are in the wrong order in relation to each other...so now (since you have two lists that need to be kept synchronised) I think you're better off holding this data in a database table which will have a primary key, a column to hold the text of the label and an integer column to hold the value of the corresponding document. Now you have a solution that you can simply add a new row to and no code changes are required, which is more elegant and less fragile

    Once you have the list in a database table you can use an a!queryEntity to retrieve the full list, extract the text column to be the list of 'choiceLabels' for your dropdown, and extract the integer column for the list of 'choiceValues', and your save() and value() for your dropdown simply saves the relevant value to your CDT's attribute.

    And that should be it.

Reply
  • So, there are a few things worth commenting on here:

    1. the pattern to use a constant to hold a list of values, and then reference them individually using the index - e.g. [1] - is not a good pattern for two reasons:
      1. it's fragile to change - if you change the order of the items in the constant your code breaks
      2. it makes the code unreadable without having to navigate to the constant to see what instance 5 is, for example
    2. It's better to create one constant for each item in your list, and then use an expression rule to generate the list. In this way you address both of the above issues each of which has a corresponding 
    3. From the screenshot you've provided it looks like you have a list of labels ({"Boss","Team Leader",...etc.}) each of which has a corresponding icon/image. You could implement a second set of constants and an expression rule to generate the list of labels to go alongside the list of documents that are the corresponding icons/images...but this is also fragile to change e.g. the lists get out of step, or are in the wrong order in relation to each other...so now (since you have two lists that need to be kept synchronised) I think you're better off holding this data in a database table which will have a primary key, a column to hold the text of the label and an integer column to hold the value of the corresponding document. Now you have a solution that you can simply add a new row to and no code changes are required, which is more elegant and less fragile

    Once you have the list in a database table you can use an a!queryEntity to retrieve the full list, extract the text column to be the list of 'choiceLabels' for your dropdown, and extract the integer column for the list of 'choiceValues', and your save() and value() for your dropdown simply saves the relevant value to your CDT's attribute.

    And that should be it.

Children
  • I agree with you. This was rather primitive test while I was making babysteps (or crawling) learning Appian Slight smile

    While I was waiting for this post to be approved I've run into some "best practices" topics and learned about stuff you're talking about.

    Now' I'm aware that generally speaking we should make codebook tables in database and then use some rules to fetch wanted values instead of creating temporary "in-code" decision logic. I believe decision objects are also valid choice sometimes?

    In my case codebook table with category ID, category label and category Icon would be the best solution. Then I could fetch whole record from that table in my Category dropdown and then use its columns to populate choice labels, choice values, ri!category and ri!categoryIcon.